Professional Documents
Culture Documents
03 Trees
03 Trees
Module 3
Trees
Root: topmost node serving as the starting point for the tree, having no parent
Child: node in a tree that has a parent node, situated lower in hierarchy than its parent
Siblings: nodes that share the same parent, at the same level in the tree
Leaf: node in a tree that has no children, at the bottom level of the hierarchy
Parent Pointer: reference or link from a child node to its parent node
Trees with some ordering e.g., Binary Search Trees (BST) provide
moderate access/search (quicker than Linked List and slower than arrays).
Like Linked Lists and unlike Arrays, Trees don’t have an upper limit on
number of nodes as nodes are linked using pointers.
Department of Electronics And Electrical Engineering, IIT Guwahati
Tree Data Structure: Applications
Store hierarchical data, like folder structure, XML/HTML data
Decision trees
• Here the height of a tree is the maximum number of nodes on the root
to leaf path.
• Height of a tree with a single node is considered as 1.
• A tree has maximum nodes if all levels have maximum nodes.
• So maximum number of nodes in a binary tree of height ℎ is 1 +
2 + 4 + . . . + 2ℎ. This is a simple geometric series with ℎ terms and
sum of this series is 2ℎ– 1.
Department of Electronics And Electrical Engineering, IIT Guwahati
Binary Tree: Properties
3) In a Binary Tree with 𝑁 nodes, minimum possible height
or the minimum number of levels is log 2(𝑁 + 1)
ACBT completes a level from left to right before starting next level
Structural Ordering
10 2
8 7 3 4
6 4 3 2 6 7 10 8
Max Heap
Insert every element as it is
24 12
11 25 8 35
11 25 8 35 11 24 8 12
11 24 8 12 11 24 8 12
35
25 14
11 24 8 12
11 24
7
8
BST behaves 8
like linked list 7 9
9
BST depends on the height of the tree and skewed structure is the
worst case leads to O(n) time complexity
Department of Electronics And Electrical Engineering, IIT Guwahati
AVL Tree
• Tree is said to be balanced if balance factor of each
node is in between -1 to 1, otherwise, the tree will be
unbalanced and need to be balanced.
• If balance factor of any node is 1, it means that the
left sub-tree is one level higher than the right sub-
tree.
• If balance factor of any node is 0, it means that the
left sub-tree and right sub-tree contain equal height.
• If balance factor of any node is -1, it means that the
left sub-tree is one level lower than the right sub-tree.
• The node whose balance factor doesn't lie between -1
and 1, is called critical node.