TREE
TREE
A tree is a non-linear hierarchical data structure that follows a Parent-
Child relationship. It is a collection of nodes where one node is defined
as ROOT node, and this root can have zero or more children.
Tree Structure
Path:
A Path can be defined as the sequence of edges
from the source node to the destination node.
It can also be defined as the sequence of nodes
from the source node to the destination node.
Subtree:
Node with any number of children is called
subtree.
Binary Tree
A tree T is called a binary tree if T has either 0, 1,
or 2 children.
It can not have more than two children.
There is a unique path from the root node to every
other node.
Types of Binary Tree
• Full/Strictly Binary Tree.
• Perfect Binary Tree.
• Complete Binary Tree.
• Almost Complete Binary Tree.
Full/Strictly Binary Tree:
• Each node has either 0 or 2 children.
Full/Strictly Binary Tree
Each node has either 0 or 2 children.
Perfect Binary Tree:
Each node has either 0 or 2 children.
All the leaf nodes should be at the same level.
Complete Binary Tree:
All levels are completely filled except possibly the last level.
Also last level might or might not be filled completely.
If the last level may not be full then all the nodes should be filled from the left.
Almost Complete Binary Tree:
All levels are completely filled except the last level.
Also, the last level must not be filled completely.
All the nodes in the last level, should be filled from the left.
Skewed Binary Tree:
• If each node in a binary tree has only one child (except leaf nodes)
then this type of binary tree is called skewed trees.
• If each node has only a left child then it is called left-skewed trees.
• Similarly, If each node has an only right child, then it is called left-
skewed trees.
Representation of binary Tree
There are two ways to represent the binary tree in memory:
1. Using Array (Static Implementation)
2. Using Linked List(Dynamic Implementation)
Using Array (Static
Implementation)
The binary tree is represented using one-dimensional array in which each
element or node is numbered sequentially level by level from left to right.
Also, empty nodes are numbered.
For any node with index i:
parent(i) =
left child(i)= 2i
right child(i)=2i+1
Using Linked List(Dynamic
Implementation)
In the linked representation, the binary tree is a collection of node
where each node has at least three fields:
• Data
• Address of the left child
• Address of the right child
Left Child Right Child
Address field Data Address Field
• Here Left address field contains the address of the left child and the
right address field contains the address of right child.
• If node has no left child, then the left address field contains NULL.
• If node has no right child, then the right address field contains NULL.
Tree Traversals
1. Pre-order Traversing
2. Post-order Traversing
3. Inorder Traversing
Tree Traversals
1. Pre-order Traversing
Pre-order traversing is done by using the following steps. These are: -
• 1) Visit the Root Node and print the data part.
• 2) Recursively Traverse the left subtree in Pre-order.
• 3) Recursively Traverse the Right subtree in pre-order.
2. Post-order Traversing
3. Inorder Traversing
ALGORITHM
Preorder_traversal(Root)
BEGIN:
• IF root == NULL THEN
• RETURN 0
• ELSE
• WRITE (root Data)
• Preorder_Traversal(root Left)
• Preorder_Traversal(root Right)
END;
Inorder Traversing
In Inorder traversing, we compute the following steps. These are
• 1) Recursively visit Left Subtree in Inorder.
• 2) Visit Root Node. Write data part.
• 3) Recursively call Right Subtree in Inorder.
ALGORITHM
Inorder_traversal(Root)
BEGIN:
• IF root == NULL THEN
• RETURN 0;
• ELSE
• Inorder_traversal(rootLeft)
• WRITE (rootData)
• Inorder_traversal(rootRight)
END;
Post order Traversing
In Post order traversing, we compute the following steps. These are
• 1) Recursively visit Left Subtree
• 2) Recursively call Right Subtree
• 3) Visit Root Node. Write data part.
ALGORITHM
Postorder_traversal( Root)
BEGIN:
• IF root == NULL THEN
• RETURN 0;
• ELSE
• Postorder_traversal(rootLeft)
• Postorder_traversal(rootRight)
• WRITE (rootData)
END;
Find:
Iorder
Preorder
PostOrder
Find:
Iorder
Preorder
PostOrder
Find:
Iorder
Preorder
PostOrder
Create a tree
Example1:
Preorder A B D H E C F G
Inorder D H B E A F C G
Create a tree
Example1: Example3:
Preorder A B D H E C F G Postorder H D I E B J F K L G C A
Inorder D H B E A F C G Inorder HDBIEAFJ CKGL
Example 4 :
Example2:
Postorder D F E B G L J K H C A
Preorder F A E K C D H G B Inorder DBFE AGCLJHK
Inorder E A C K F H D B G
Create a tree
Example1:
Preorder A B D H E C F G
Inorder D H B E A F C G
Create a tree
Example2:
Preorder F A E K C D H G B
Inorder E A C K F H D B G
Create a tree
Example3:
Postorder H D I E B J F K L G C A
Inorder HDBIEAFJ CKGL
Example 4 :
Postorder D F E B G L J K H C A
Inorder DBFE AGCLJHK
Binary Tree Algorithms
Finding Count of Nodes in the
Tree
If the node does not exist, its counting is updated as zero. If the node
exists, then the number of nodes considering that as root node will be the
1 (the count of that node) plus the nodes in the Left subtree and Right
subtree of that node computed recursively.
ALGORITHM CountNodes(Root)
BEGIN:
IF Root == NULL THEN
RETURN 0
ELSE
RETURN 1+CountNodes(Root Left) + CountNodes(Root Right)
END;
Finding the count of Leaf Nodes
• If the node does not exist, the count of the leaf will be 0. If the left and right of a node are NULL,
that will be the leaf node, and its counting is updated as 1. If the node exists and it is not the leaf
node, then the number of leaf nodes considering that as root node will be the sum of leaf nodes on
the Left subtree and Right subtree of that node computed recursively.
Algorithm CountLeafNodes(Root)
BEGIN:
IF Root == NULL THEN
RETURN 0
ELSE
IF RootLeft==NULL AND RootRight ==NULL THEN
RETURN 1
ELSE
RETURN CountLeafNodes(RootLeft)+CountLeafNodes(Root Right)
END;
Binary Search Tree
A binary Search tree is the extended form of binary tree in which if any
node N has value K then It should have the following properties:
All values in the left subtree of node N will be less than its value K.
All values in the right subtree of node N will be greater than its value K.
(Assume that BST contains distinct values.)
Construct BST
Example1: 80 90 60 120 100 75 25 45
Construct BST
Example1: 80 90 60 120 100 75 25 45
Example2: J R D T G E A M H F Q U B
Example 3: A B C D E F G H
Example 4: Z Y X W V T R
Linked list Representation of
BST:
In linked representation, BST is a collection of nodes where each node has at
least four fields.
Left address Field Address to Parent node Data Field Right address Field
•
• Left address Field contains the address of the left child of the node.
• Address to the Parent node contains the address of the parent node.
• Data Field contains the actual value to be stored in the node.
• The right address Field contains the address of the right child of the node.
Searching: Binary Search Tree
ALGORITHM SEARCH_BST_I(ROOT, ITEM)
BEGIN:
WHILE ROOT! = NULL DO
IF ITEM ==ROOTDATA THEN
Return ROOT
ELSE IF ITEM< ROOTDATA
ROOT = ROOTLEFT
ELSE
ROOT = ROOTRIGHT
RETURN NULL
END;
Searching: Binary Search Tree
ALGORITHM SEARCH_BST_I(ROOT, ITEM)
BEGIN: Complexity of Operation
Time Complexity: In general, the time complexity of Search
WHILE ROOT! = NULL DO
operation in BST is O(h), where h is the height of the BST.
IF ITEM ==ROOTDATA THEN In worst-case, iF BST is either Right Skewed BST or Left
Return ROOT Skewed BST, then the time complexity of Search operation in
BST is O(N) complexity where N is the number of nodes in
ELSE IF ITEM< ROOTDATA BST.
ROOT = ROOTLEFT
In the Best or Average case, BST is either complete BST or
ELSE Almost complete BST, so the height of BST will be either log2N
ROOT = ROOTRIGHT or almost log2N. So time complexity is order of log2N.
Space Complexity = O(1)
RETURN NULL
END;
Deletion in BST
There will be three cases to delete the given node:
Case 1: If the node is deleted with zero child i.e., leaf node then return NULL to its parent. This
means, make the corresponding child pointer NULL.
Case 2: If the node to be deleted has one child, then simply link the current node’s child to its
parent.
Case 3: If the node to be deleted has two children, then
• first find either its inorder successor (left most of right child).
• Then replace the value of the node to be deleted by its inorder successor.
• Simply delete that inorder successor using either case 1 or case 2 because inorder successor
node always has either one child or no child.
Create BST
50,45, 80,95, 26, 43, 105, 2
then delete 50, 26, 45
Inser BST Algo
Why AVL Tree?
Consider the elements 6,4,3,2,1 to be inserted into a binary search tree
turns out to be left-skewed and again the insertion of elements
1,2,3,4,6 in that order in the binary search tree results in a right-
skewed binary search tree .
Definition of AVL
• One of the balanced tree was introduced in 1962 by GM Adelson -
Velsky and EM Landis in 1962 and is known as the AVL tree.
• AVL Tree can be defined as height-balanced binary search tree in
which each node is associated with a balance factor which is
calculated by subtracting the height of its right sub-tree from that of
its left sub-tree.
• A tree is said to be balanced if the balance factor of each node is in
between -1 to 1; otherwise, the tree will be unbalanced and need to
be balanced.
Balance Factor (k) = height of the right subtree- height of the left subtree
• If balance factor of any node is 1, it means that the right sub-tree is one
level higher than the left sub-tree.
• If the 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 right sub-tree is one
level lower than the left sub-tree.
• We can see that, balance factor associated with each node is in between -
1 and +1. Therefore, it is an example of an AVL tree.
Operations on AVL Tree
• Because, AVL tree is also a binary search tree, therefore all the operations are
performed in the same way as they are performed in a binary search tree. Searching
and traversing do not lead to the violation of the property of the AVL tree. However,
insertion and deletion are the operations that can violate this property, and
therefore, they need to be revisited.
Insertion:
• Insertion in the AVL tree is performed similarly as it is performed in a binary search
tree. However, it may lead to the violation in the AVL tree property, and therefore
the tree may need balancing. The tree can be balanced by applying rotations.
Deletion:
• Deletion can also be performed in the same way as it is performed in a binary
search tree. Deletion may also disturb the balance of the tree therefore, various
types of rotations are used to rebalance the tree. We perform rotation in the AVL
tree only if Balance Factor is other than -1, 0, and 1. There are four rotations, and
they are classified into two types.
Rotations
- - clockwise rotation
Logic :
left (a) = right (b)
right (b) = a
-1
50
0 0 Insert
30
50 90
10
0 0
20 35
Logic :
left (a) = right (b)
right (b) = a
a -2
-1
50 50
0 0 Insert -1 b 0
30
50 90 30
50 90
10
0 0 -1 0
20 35 20 35
0
10
RR Rotation
• When BST becomes unbalanced, a node is inserted into the right
subtree of the right subtree of A, then we perform RR rotation,
RR rotation is an anticlockwise rotation, which is applied on edge
below a node having balance factor 2.
a Logic:
right (a) = left (b)
Left (b) = a
b
Logic:
right (a) = left (b)
Left (b) = a
1
50
0 0 Insert
30
50 55
85
0 0
52 70
Logic:
right (a) = left (b)
Left (b) = a
a
1 2
50 50
0 b1
0 0 Insert
30
50 55 30
50 55
85
0 0 1
0 52 70
52 70
0
85
Logic:
right (a) = left (b)
Left (b) = a
a 0
1 2 55
50 50
0 b1 RR 0 1
0 0 Insert 50
30
50 30
50 55 70
55 Rotation
85
0 0
0 0 0
0 1 30 52 85
52 70 52 70
0
85
LR Rotation
-1
30
0 0
20 50 Insert
29
0 0 0
50
12 25 40 60
0 0 0
11 24 27
-1 -2
a
30 30
0 0 +1 0
20 50 Insert b 20 50
29 c
0 0 0 +1 0
50
12 25 40 60 50
12 25 40 60
0 0 0 0 0 1
11 24 27 11 24 27
0
29
-2 -3
a a
30 30
+1 0 -1 0
b 20 50 b 25 50
RR
c Rotation -1
+1 0 +1 0
50
12 25 40 60 50
20 27 40 60
0 0 1 0 0 1
11 24 27 12 24 29
0 0
29 11
-3 1
a
30 25
-1 0 -1 1
b 25 50 20 30
LL
-1 +1 0 Rotation -1 0
0 1
50
20 27 40 60 50
12 24 27 50
0 0 1 0 0 0 0
12 24 11 29 40 60
11
RL Rotation
1
50
0 1
40 60 Insert
65
0 0 0
50
10 49 56 80
0 0
70 85
1 +2
50 50
a
0 1 0 +2
40 60 Insert 40 60
65 b -1
0 0 0 0 0
50
10 49 56 80 10
50 49 56 80
0 0 -1 c 0
70 85 70 85
0
65
+2 +2
50 50
a a
0 +2 0 +2
40 60 40 60
LL
b -1 Rotation b +1
0 0 0 0
50
10 49 56 80 10
50 49 56 70
-1 c 0 0 0
70 85 65 80
0 0
65 85
+2 1
50 50
a
0 0 0
40 60 +2 40 70
RR
b +1 0
0 0 Rotation 0 0 1
50
10 49 56 70 10
50 49 60 80
0 0 0 0 0
65 80 56 65 85
0
85
Construct AVL TREE
1,2,3,4,8,7,6,5,11,10,12
2 a
1
b1
2
0
3
Construct AVL TREE
1,2,3,4,8,7,6,5,11,10,12
Insert 1
Tree is balanced
Insert 2
- As 2 > 1, so insert 2 in 1’s right sub tree.
Insert 3
- As 3 > 1, so insert 3 in 1’s right sub tree.
- As 3 > 2, so insert 3 in 2’s right sub tree.
Tree is balanced after RR Rotation
Insert 4
- As 4 > 2, so insert 4 in 2’s right sub tree.
- As 4 > 3, so insert 4 in 3’s right sub tree.
Tree is Balanced
Insert 8
- As 8 > 2, so insert 8 in 2’s right sub tree.
- As 8 > 3, so insert 8 in 3’s right sub tree.
- As 8 > 4, so insert 8 in 4’s right sub tree
Tree is Balanced after RR Rotation
Insert 7
- As 7 > 2, so insert 7 in 2’s right sub tree.
- As 7 > 4, so insert 7 in 4’s right sub tree.
- As 7 < 8, so insert 7 in 8’s left sub tree
Tree is Balanced after RR Rotation
Insert 6
- As 6 > 4, so insert 6 in 4’s right sub tree.
- As 6 < 8, so insert 6 in 8’s left sub tree.
- As 6 < 7, so insert 6 in 7’s left sub tree
Tree is Balanced after LL Rotation
Insert 5
- As 5 > 4, so insert 5 in 4’s right sub tree.
- As 5 < 7, so insert 5 in 7’s left sub tree.
- As 5 < 6, so insert 5 in 6’s left sub tree
Tree is Balanced
Insert 11
- As 11 > 4, so insert 11 in 4’s right sub tree.
- As 11> 7, so insert 11 in 7’s right sub tree.
- As 11> 8, so insert 11 in 8’s right sub tree.
Tree is Balanced
Insert 10
- As 10 > 4, so insert 10 in 4’s right sub tree.
- As 10 > 7, so insert 10 in 7’s right sub tree.
- As 10 > 8, so insert 10 in 8’s right sub tree
- As 10 < 11, so insert 10 in 11’s left sub tree
Tree is Balanced after RL Rotation
Construct AVL tree
March, May, Nov, Aug, April, Jan, Dec, July, Feb, Jun
May
March Nov
Jan April July Dec
Feb June Aug
Construct AVL Tree
50, 45, 80, 95, 26, 43, 105, 2
50
43 95
26 45 80 105
Delete 50 from above tree
80
43 95
26 45 105