Understanding Tree Data Structures
Understanding Tree Data Structures
Tree is a non-linear data structure which organizes data in hierarchical structure and this is a recursive
definition.
A treeT is a set of nodes storing elements such that the nodes have a parent-child relationship
that satisfies the following
• If T is not empty, That is special tree called the root that has no parent.
• Each node v of Tdifferent than the root has a unique parent node w; node with parent w is a
child of w.
1
Terminology of Tree: In a tree data structure, we use the following terminology...
1. Root
In a tree data structure, the first node is called as Root Node. Every tree must have root [Link]
can say that root node is the origin of tree data structure. In any tree, there must be only one root
node. We never have multiple root nodes in a tree.
2. Edge
In a tree data structure, the connecting link between any two nodes is called as EDGE. In a tree
with 'N' number of nodes there will be a maximum of 'N-1' number of edges.
3. Parent
In a tree data structure, the node which is predecessor of any node is called as PARENT NODE. In
simple words, the node which has branch from it to any other node is called as parent node. Parent
node can also be defined as "The node which has child / children".
2
4. Child
In a tree data structure, the node which is descendant of any node is called as CHILD Node. In
simple words, the node which has a link from its parent node is called as child node. In a tree,
any parent node can have any number of child nodes. In a tree, all the nodes except root are child
nodes.
5. Siblings
In a tree data structure, nodes which belong to same Parent are called as SIBLINGS. In simple
words, the nodes with same parent are called as Sibling nodes.
3
6. Leaf
In a tree data structure, the node which does not have a child is called as LEAF Node. In simple
words, a leaf is a node with no child.
In a tree data structure, the leaf nodes are also called as External Nodes. External node is also a
node with no child. In a tree, leaf node is also called as 'Terminal' node.
7. Internal Nodes
In a tree data structure, the node which has at least one child is called as INTERNAL Node. In
simple words, an internal node is a node with at least one child.
In a tree data structure, nodes other than leaf nodes are called as Internal Nodes.
The root node is also said to be Internal Node if the tree has more than one node. Internal
nodes are also called as 'Non-Terminal' nodes.
8. Degree
In a tree data structure, the total number of children of a node is called as DEGREE of that
Node. In simple words, the Degree of a node is total number of children it has. The highest
degree of a node among all the nodes in a tree is called as 'Degree of Tree'
4
9. Level
In a tree data structure, the root node is said to be at Level 0 and the children of root node are at
Level 1 and the children of the nodes which are at Level 1 will be at Level 2 and so on... In
simple words, in a tree each step from top to bottom is called as a Level and the Level count
starts with '0' and incremented by one at each level (Step).
10. Height
In a tree data structure, the total number of edges from leaf node to a particular node in the
longest path is called as HEIGHT of that Node. In a tree, height of the root node is said to be
height of the tree. In a tree, height of all leaf nodes is '0'.
5
11. Depth
In a tree data structure, the total number of edges from root node to a particular node is called as
DEPTH of that Node. In a tree, the total number of edges from root node to a leaf node in the
longest path is said to be Depth of the tree. In simple words, the highest depth of any leaf node
in a tree is said to be depth of that tree. In a tree, depth of the root node is '0'.
12. Path
In a tree data structure, the sequence of Nodes and Edges from one node to another node is called
as PATH between that two Nodes. Length of a Path is total number of nodes in that path. In
below example the path A - B - E - J has length 4.
13. SubTree
In a tree data structure, each child from a node forms a subtree recursively. Every child node will
form a subtree on its parent node.
6
14. Forest-A forest is a set of disjoint trees
Ancestor and Descendent: If there is a path from node A to node B, then A is called an
ancestor of B and B is called a descendent of A.
• Example
7
Properties of Binary Trees:
3. Since a binary tree can contain at most one node at level 0(the root),it can contain at most2 l
node at level l.
4. The total number of edges in a full binary tree with n node is n-1.
Example: Consider we want to draw all the binary trees possible with 3 unlabeled nodes. Using the above
formula, we have-
Number of binary trees possible with 3 unlabeled nodes
=2x3C3/ (3+1)
=6C3/ 4
=5
8
Thus,
With 3 unlabeled nodes,5 unlabeled binary trees are possible.
These unlabeled binary trees are as follows-
[Link] Binary Tree: A binary tree is labeled if all its nodes are assigned a label.
Example: Consider we want to draw all the binary trees possible with 3 labeled nodes. Using the above
formula, we have-
Number of binary trees possible with 3 labeled nodes
={ 2x3C3/ (3 +1) }x 3!
={ 6C3/4 } x 6
=5 x 6
=30
Thus,
With 3 labeled nodes, 30 labeled binary trees are possible.
9
Each unlabeled structure gives rise to 3!=6 different labeled structures.
10
2. Full/Strictly Binary Tree-
A binary tree in which every node has either 0 or 2 children is called as a Full binary tree.
Full binary tree is also called as Strictly binary tree or Proper Binary Tree or 2-Tree
Here,
First binary tree is not a full binary tree.
This is because node C has only 1 child.
11
Here,
First binary tree is not a complete binary tree.
This is because all the leaf nodes are not at the same level.
An almost complete binary tree is a binary tree that satisfies the following2 properties-
All the levels are completely filled except possibly the last level.
The last level must be strictly filled from left to right.
Here,
First binary tree is not an almost complete binary tree.
This is because the last level is not filled from left to right.
When we wanted to display a binary tree, we need to follow some order in which all the nodes of
that binary tree must be displayed. In any binary tree, displaying order of nodes depends on the
traversal method.
Displaying (or) visiting order of nodes in a binary tree is called as Binary Tree Traversal.
Tree Traversal refers to the process of visiting each node in a tree data structure exactly
12
once.
[Link] First Traversal: Following three traversal techniques fall under Depth First Traversal-
1. Pre-Order Traversal
2. In-Order Traversal
3. Post-Order Traversal
1. Pre-order Traversal-
Algorithm-
1. Visit the root
2. Traverse the left subtree i.e. call Pre-order (left sub tree)
3. Traverse the right subtree i.e. call Pre-order (right subtree)
Root→Left→Right
Consider the following example-
13
Preorder Traversal Shortcut
Traverse the entire tree starting from the root node keeping yourself to the left.
Applications-
Pre-order traversal is used to get prefix expression of an expression tree.
Pre-order traversal is used to create a copy of the tree.
2. In-order Traversal-
Algorithm-
1. Traverse the left subtree i.e. call In-order(left subtree)
2. Visit the root
3. Traverse the right subtree [Link] In-order (right subtree)
Left→Root→Right
14
In-order Traversal Shortcut
Keep a plane mirror horizontally at the bottom of the tree and take the projection of all the nodes.
Application-
In-order traversal is used to get infix expression of an expression tree.
3. Post-order Traversal-
Algorithm
1. Traverse the left sub tree i.e. call Post- order(left sub tree)
2. Traverse the right subtree i.e. call Post order (right subtree)
3. Visit the root
Left→Right→Roo
Example-
15
Consider the following example-
Applications-
Post order traversal is used to get postfix expression of an expression tree.
Post-order traversal is used to delete the tree.
This is because it deletes the children first and then it deletes the parent.
Application-
16
Level order traversal is used to print the data in the same order as stored in the array
representation of a complete binary tree.
Consider the following binary tree...
Same technique can be applied repeatedly to form sub-trees. It can be noted that,
for the purpose mentioned, two traversal are essential out of which one should be
inorder traversal and another preorder or postorder; alternatively, given preorder
and postorder traversals, binary tree cannot be obtained uniquely.
Example1:
Preorder: A B D G C E H I F Inorder: D G B A H E I C F
17
18
Example2:
Let us consider the below traversals:
Inorder sequence: D B E A F C
Preorder sequence: ABDEC F
19
Example3:
Construct a binary tree from a given postorder and inorder sequence:
20
Example 4:
Inorder: n1 n2 n3 n4 n5 n6 n7 n8 n9 Preorder:n6 n2 n1 n4 n3 n5 n9
21
n7 n8
Example 5: Construct a binary tree from a given postorder and inorder sequence:
Inorder: n1 n2 n3 n4 n5 n6 n7 n8 n9
Postorder:n1 n3 n5 n4 n2 n8 n7 n9 n6 Solution:
The inorder and preorder traversal of a binary tree are dbeafcgandabdecfg, respectively. The
postorder traversal of the binary tree is:
(A) d ebf gc a
Explanation: Since given tree is binary search tree, so inorder traversal will be always sorted order, i.e.,
2, 6, 7, 8, 9, 10, 12, 15, 16, 17, 19, 20.
Now we can draw that binary search tree using given postorder and inorder traversal. Final tree will be:
23
Tree Representations
A tree data structure can be represented in two methods. Those methods are as follows...
1. Array(Sequential)
2. Linked list Representation
Let us consider that we have a tree T. let our tree T is a binary tree that us complete binary tree.
Then there is an efficient way of representing T in the memory called the sequential
representation or array representation of T. This representation uses only a linear array TREE as
follows:
24
Case 2: starting ‘i’ index is 1
A B C D E F G H I
1 2 3 4 5 6 7 8 9
In this representation, every node's data field stores the actual value of that node. If that node has
left child, then left reference field stores the address of that left child node otherwise stores
NULL. If that node has right sibling, then right reference field stores the address of right sibling
node otherwise stores NULL.
25
A binary tree has the following time complexities...
Binary Search Tree is a binary tree in which every node contains only smaller values in its left
subtree and only larger values in its right subtree. (or)
A Binary search tree follows some order to arrange the elements. In a Binary search tree, the value of
left node must be smaller than the parent node, and the value of right node must be greater than the
parent node. This rule is applied recursively to the left and right subtrees of the root. Let's understand
the concept of Binary search tree with an example.
o First, we have to insert 45 into the tree as the root of the tree.
o Then, read the next element; if it is smaller than the root node, insert it as the
root of the left subtree, and move to the next element.
o Otherwise, if the element is larger than the root node, then insert it as the root
of the right subtree.
Now, let's see the process of creating the Binary search tree using the given
data element. The process of creating the BST is shown below –
26
Step 1 - Insert 45.
As 15 is smaller than 45, so insert it as the root node of the left subtree.
As 79 is greater than 45, so insert it as the root node of the right subtree.
90 is greater than 45 and 79, so it will be inserted as the right subtree of 79.
27
Step 6 - Insert 55.
55 is larger than 45 and smaller than 79, so it will be inserted as the left
subtree of 79.
12 is smaller than 45 and 15 but greater than 10, so it will be inserted as the
right subtree of 10.
20 is smaller than 45 but greater than 15, so it will be inserted as the right
subtree of 15.
Now, the creation of binary search tree is completed. After that, let's move
towards the operations that can be performed on Binary search tree.
1. Search
2. Insertion
3. Deletion
10,12,5,4,20,8,7,15and13
Above elements are inserted into a Binary Search Tree as follows...
31
// C program to insert a node in a Binary Search
Tree
#include <stdio.h>
#include <stdlib.h>
// Given Node
struct node {
int data;
struct node *left, *right;
};
// Driver Code
int main()
{
/* Let us create following BST
50
/ \
30 70
/ \ / \
20 40 60 80
*/
struct node* root = NULL;
// inserting value 50
root = insert(root, 50);
33
// inserting value 30
insert(root, 30);
// inserting value 20
insert(root, 20);
// inserting value 40
insert(root, 40);
// inserting value 70
insert(root, 70);
// inserting value 60
insert(root, 60);
// inserting value 80
insert(root, 80);
return 0;
}
OUTPUT:
20 30 40 50 60 70 80
34
}
// Inorder Traversal
void inorder(struct node *root) {
if (root != NULL) {
// Traverse left
inorder(root->left);
// Traverse root
printf("%d -> ", root->key);
// Traverse right
inorder(root->right);
}
}
// Insert a node
struct node *insert(struct node *node, int key) {
// Return a new node if the tree is empty
if (node == NULL) return newNode(key);
// Traverse to the right place and insert the node
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}
// Find the inorder successor
struct node *minValueNode(struct node *node) {
struct node *current = node;
// Find the leftmost leaf
while (current && current->left != NULL)
current = current->left;
return current; }
35
// Deleting a node
struct node *deleteNode(struct node *root, int key) {
// Return if the tree is empty
if (root == NULL) return root;
// Find the node to be deleted
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else {
// If the node is with only one child or no child
if (root->left == NULL) {
struct node *temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct node *temp = root->left;
free(root);
return temp;
}
// If the node has two children
struct node *temp = minValueNode(root->right);
// Place the inorder successor in position of the node to be deleted
root->key = temp->key;
// Delete the inorder successor
root->right = deleteNode(root->right, temp->key);
}
return root;
}
int main() {
struct node *root = NULL;
36
root = insert(root, 8);
root = insert(root, 3);
root = insert(root, 1);
root = insert(root, 6);
root = insert(root, 7);
root = insert(root, 10);
root = insert(root, 14);
root = insert(root, 4);
printf("Inorder traversal: ");
inorder(root);
printf("\nAfter deleting 10\n");
root = deleteNode(root, 10);
printf("Inorder traversal: ");
inorder(root);
}
OUTPUT:
In order traversal: 1 -> 3 -> 4 -> 6 -> 7 -> 8 -> 10 -> 14 ->
After deleting 10
In order traversal: 1 -> 3 -> 4 -> 6 -> 7 -> 8 -> 14 ->
The AVL tree was introduced in the year 1962 by G.M. Adelson, Velsky and E.M. Landis.
AVL tree is a height balanced binary search tree. That means, an AVL tree is also a binary
search tree but it is a balanced tree. A binary tree is said to be balanced if, the difference between
the heights of left and right subtrees of every node in the tree is either -1, 0 or +1.
In other words, a binary tree is said to be balanced if the height of left and right children of
every differs by either-1, 0 or +1.
In an AVL tree, every node maintains an extra information known as balance factor.
An AVL tree is defined as follows...
An AVL tree is a balanced binary search tree . In an AVL tree, balance factor of every node is
either-1,0 or +1.
37
Balance factor of a node is the difference between the heights of left and right subtrees of that
node.
The balance factor of an order is calculated either height of left sub tree-height of right
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 above tree is a binary search tree and every node is satisfying balance factor condition. So
this tree is said to be an AVL tree.
38
NOTE: Every AVL Tree is a binary search tree but every Binary Search Tree need not
be AVL tree.
Complexity
Algorithm Average case Worstcase
Depending upon the type of insertion, the Rotations are categorized into four categories.
SN Rotation Description
1 LL Rotation The new node is inserted to the left sub-tree of left sub-tree of critical
node A.
2 RR Rotation The new node is inserted to the right sub-tree of the right sub-tree of the
critical node A.
3 LR Rotation The new node is inserted to the right sub-tree of the left sub-tree of the
critical node A.
4 RL Rotation The new node is inserted to the left sub-tree of the right sub-tree of the
critical node A.
39
The node whose balance factor doesn't lie between -1 and 1,is called critical node.
In order to rebalance the tree, LL rotation is performed as shown in the following diagram.
The node B becomes the root, with A and T3 as its left and right child. T1 and T2 becomes the
left and right sub-trees of A.
While the rotation, the node B becomes the root node of the tree. The critical node A will be
moved to its left and becomes the left child of B.
The sub-tree T3 becomes the right sub-tree of A. T1 and T2 becomes the left and right sub-tree
of node A.
40
[Link]-Right Rotation(LR Rotation)
LRrotationistobeperformedifthenewnodeisinsertedintotherightoftheleftsub-treeof node A.
In LR rotation, node C (as shown in the figure) becomes the root node of the tree, while the node
B and A becomes its left and right child respectively.
T1 and T2 becomes the left and right sub-tree of Node B respectively where as, T3 and T4 becomes the
left and right sub-tree of Node A.
Let T1 be the left sub-tree of the critical nodeA,T2and T3 be the left and right sub-tree of Node
C respectively, sub-tree T4 be the right sub-tree of Node B.
Since, RL rotation is the mirror image of LR rotation. In this rotation, the node C becomes the
root node of the tree with A and B as its left and right children respectively. Sub-trees T1 and T2
41
becomes the left and right sub-trees of A whereas, T3 and T4 becomes the left and right sub-trees
of B. The process of RL rotation is shown in the following image.
1. Insertion
2. Deletion
3. Search
Step 1- Insert the new element into the tree using Binary Search Tree insertion logic.
Step2-Afterinsertion, check the Balance Factor of every node.
Step3 - If the Balance Factor of every node is 0 or 1 or-1 then go for next operation.
Step 4 -If the Balance Factor of any node is other than 0 or 1 or -1 then that tree is said
to be imbalanced. In this case, perform suitable Rotation to make it balanced and go for
next operation.
All the elements are inserted in order to maintain the order of binary search tree.
42
43
[Link] Operation in AVL Tree:
The deletion operation in AVL Tree is similar to deletion operation in BST. But after every
deletion operation we need to check with the Balance Factor condition. If the tree is balanced
after deletion go for next operation otherwise perform suitable rotation to make the tree
Balanced.
Deleting a node from an AVL tree is similar to that in a binary search tree. Deletion may disturb
the balance factor of an AVL tree and therefore the tree needs to be rebalanced in order to
maintain the AVL ness. For this purpose, we need to perform rotations. The two types of
rotations are L rotation and R rotation. Here, we will discuss R rotations. L rotations are the
mirror images of them.
If the node which is to be deleted is present in the left sub-tree of the critical node, then L
rotation needs to be applied elseif, the node which is to be deleted is present in the right sub-tree
of the critical node, the R rotation will be applied.
Let us consider that, A is the critical node and B is the root node of its left sub-tree. If node X,
present in the right sub-tree of A, is to be deleted, then there can be three different situations:
The critical node A is moved to its right and the node B becomes the root of the tree with T1 as
its left sub-tree. The sub-trees T2 and T3 becomes the left and right sub-tree of the node A. the
process involved in R0 rotation is shown in the following image.
LL rotation
44
R1 Rotation(Node B has balance factor 1)
R1 Rotation is to be performed if the balance factor of Node B is 1. In R1 rotation, the critical
node A is moved to its right having sub-trees T2 and T3 as its left and right child respectively.T1
is to be placed as the left sub-tree of the node B.
The process involved in R1 rotation is shown in the following image.
LL Rotation
The sub-trees T1, T2 becomes the left and right sub-trees of B whereas, T3, T4 become the left
and right sub-trees of A. The process involved in R-1 rotation is shown in the following image.
LR Rotation
45
Similarly, L0, L1, L-1
[Link] Operation in AVL Tree
In an AVL tree, the search operation is performed with O(log n)time complexity. The search
operation in AVL tree is similar to search operation in Binary search tree. We use the following
steps to search an element in AVL tree...
#include <stdlib.h>
// Create Node
struct Node {
int key;
int height;
};
// Calculate height
if (N == NULL)
return 0;
46
return N->height;
return (a > b) ? a : b;
// Create a node
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1;
return (node);
// Right rotate
x->right = y;
y->left = T2;
return x;
// Left rotate
47
struct Node *y = x->right;
y->left = x;
x->right = T2;
return y;
if (N == NULL)
return 0;
// Insert node
if (node == NULL)
return (newNode(key));
else
return node;
48
node->height = 1 + max(height(node->left),
height(node->right));
return rightRotate(node);
return leftRotate(node);
node->left = leftRotate(node->left);
return rightRotate(node);
node->right = rightRotate(node->right);
return leftRotate(node);
return node;
current = current->left;
return current;
// Delete a nodes
49
struct Node *deleteNode(struct Node *root, int key) {
if (root == NULL)
return root;
else {
if (temp == NULL) {
temp = root;
root = NULL;
} else
*root = *temp;
free(temp);
} else {
root->key = temp->key;
if (root == NULL)
return root;
50
// Update the balance factor of each node and
root->height = 1 + max(height(root->left),
height(root->right));
return rightRotate(root);
root->left = leftRotate(root->left);
return rightRotate(root);
return leftRotate(root);
root->right = rightRotate(root->right);
return leftRotate(root);
return root;
if (root != NULL) {
printPreOrder(root->left);
printPreOrder(root->right);
51
int main() {
printPreOrder(root);
printPreOrder(root);
return 0;
OUTPUT:
4213758
After deletion: 4 2 1 7 5 8
52