2.4 Trees
2.4 Trees
UNIVERSITY INSTITUTE OF
ENGINEERING
COMPUTER SCIENCE ENGINEERING
Bachelor of Engineering
Design and Analysis of
Algorithms(CSH-311/ITH-311)
Outcome:
• Student will understand
Tree and operations performed on tree
Different Tree Types
What is tree??
• It is an abstract model of a
hierarchical structure. Sales Manufacturing
R&D
• Applications:
– Organization charts
Europe Asia Canada
– File systems
– Programming environments
Terminologies
• Root: node without parent (A)
Terminologies (Contd…)
• Depth of a node: number of ancestors.
Tree representation
Tree traversal
• Recursive definition
• Preorder:
– visit the root
– traverse in preorder the children (subtrees)
• Postorder
– traverse in postorder the children (subtrees)
– visit the root
Binary tree
A A A
B B
B C
D E F G
D
E H I
Binary Tree
AVL Tree
• AVL tree is a self-balancing Binary Search Tree (BST)
where the difference between heights of left and right
subtrees cannot be more than one for all nodes.
• Why AVL Trees?
Most of the BST operations (e.g., search, max, min, insert,
delete.. etc) take O(h) time where h is the height of the
BST. The cost of these operations may become O(n) for a
skewed Binary tree. If we make sure that height of the
tree remains O(Logn) after every insertion and deletion,
then we can guarantee an upper bound of O(Logn) for all
these operations
Example
B Tree
REFERENCES
Text books:
•Cormen, Leiserson, Rivest, Stein, “Introduction to Algorithms”, Prentice Hall of
India, 3rd edition 2012. problem, Graph coloring.
•Horowitz, Sahni and Rajasekaran, “Fundamentals of ComputerAlgorithms”,
University Press (India), 2nd edition
Websites:
1.https://www.tutorialride.com/data-structures/trees-in-data-structure.htm
2.https://www.geeksforgeeks.org/avl-tree-set-1-insertion/
Summary
Introduction to Trees
• Basic terms
Types of Trees
• Binary tree
• B tree
• Balanced trees