Trees Data Structure
Trees Data Structure
• Applications:
• –Organization charts
• –File systems
• –Programming environments
List Representation
A (A)
B C D ( A ( B, C, D ) )
E F G H I J ( A ( B ( E, F ), C ( G ), D ( H, I, J ) ) )
K L M ( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
K
E
B
L
F
A C G
H M
D I
J
28/11/2025 School of Computer Engineering, MIT, Manipal 10
2. FirstChild-NextSibling Representation (Left Child Right Sibling)
Element A
N
FirstChild NextSibling
B C D
A N
B C D
E F G H I J
E F G H I J N N N N N N N
K L M K L M
N N N N N
N
E
B C D
N K C
F
N
E F G H I J L G
N
D
N
NN NN N NN
N
N
N
H
N
K L M
N NN NN M I
N
N
Left J
N
Element
N
N
Left Right
Right
–position leftChild(p)
–position rightChild(p)
–position sibling(p)
Prove by induction.
k
2 i 1
2 k
1
i 1
A A 1 A
B B 2 B C
4 H I
E 5
A A
B C B C
D E F G D E F G
H I J K L M N O
H I
Complete binary tree Full binary tree of depth 4
Node Children Each node has 0 or 2 children Nodes can have 0, 1, or 2 children
Leaf Node Placement Can be at different levels Must be as far left as possible
Advantages:
1. This representation is very easy to understand.
2. This is the best representation for full and complete binary tree
representation.
3. Programming is very easy.
4. It is very easy to move from a child to its parents and vice versa.
Disadvantages:
5. Lot of memory area wasted.
6. Insertion and deletion of nodes needs lot of data movement.
7. This is not suited for trees other than full and complete tree.
data
Advantages
1. A particular node can be placed at any location in the memory.
2. Insertions and deletions can be made directly without data movements.
3. It is best for any type of trees.
4. It is flexible because the system take care of allocating and freeing of
nodes.
Disadvantage
5. It is difficult to understand.
6. Additional memory is needed for storing pointers
7. Accessing a particular node is not easy.
b c
a b c
b c
f
d e
g h i j
a b d g h e i c f j
b c
b a c
b c
f
d e
g h i j
g d h b e i a f j c
b c
f
d e
g h i j
g d h b e i a f j c
b c
b c a
b c
f
d e
g h i j
g h d i e b j f c a
b c
f
d e
g h i j
• Make a clone.
• Determine height.
•Determine number of nodes.
• It is a breadth-first search (BFS) technique where we visit all nodes at each level
from left to right before moving to the next level
B C
D E F G
H I
B C
D E F G
queue
H I
B C
D E F G
H I
A
B C
D E F G
H I
28/11/2025
A School of Computer Engineering, MIT, Manipal 53
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
BC
28/11/2025
A School of Computer Engineering, MIT, Manipal 54
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
C
28/11/2025
A B School of Computer Engineering, MIT, Manipal 55
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
CDE
28/11/2025
A B School of Computer Engineering, MIT, Manipal 56
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
DEFG
28/11/2025
A B C School of Computer Engineering, MIT, Manipal 57
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
EFGH
28/11/2025
A B C D School of Computer Engineering, MIT, Manipal 58
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
FGH
28/11/2025
A B C D E School of Computer Engineering, MIT, Manipal 59
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
GHI
28/11/2025
A B C D EF
School of Computer Engineering, MIT, Manipal 60
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
HI
28/11/2025
A B C D E School
F Gof Computer Engineering, MIT, Manipal 61
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
I
28/11/2025
A B C D E School
F Gof Computer
H Engineering, MIT, Manipal 62
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
28/11/2025
A B C D E School
F Gof Computer
H I Engineering, MIT, Manipal 63
Level order Traversal
• void levelOrderTraversal(struct Node* root) {
• if (root == NULL) return;
• // Create a queue for level order traversal
• if (current->left != NULL)
• enqueue(queue, current->left);
• // Enqueue right child
• if (current->right != NULL)
• enqueue(queue, current->right);
• }}
28/11/2025 School of Computer Engineering, MIT, Manipal 64
Additional Binary Tree
Operations
Returns FALSE if the binary trees root1 and root2 are not equal
otherwise returns TRUE
int Equal( Nodeptr root1, Nodeptr root2)
{
return ((!root1 && !root2) || (root1 && root2 && (root1data ==
root2data) && Equal( root1->lchild,root2->lchild) && Equal
( root1->rchild,root2->rchild));
}
* /
- 4 9 2
5 3
(5 – 3) * 4 + 9 / 2
+ - (9 / 2 + 7) * (8 – 5)
/ 7 8 5
Evaluation is based on postorder traversal:
9 2 If root node is a leaf, return the associated value.
Recursively evaluate expression in left subtree.
Recursively evaluate expression in right subtree.
Perform operation in root node on these two values,
and return result.
+ -
/ 7 8 5
9 2
+ -
4.5 / 7 8 5
9 2
11.5 + -
4.5 / 7 8 5
9 2
11.5 + 3 -
4.5 / 7 8 5
9 2
34.5
*
11.5 + 3 -
4.5 / 7 8 5
9 2
5 3
28/11/2025 School of Computer Engineering, MIT, Manipal 85
Symbol Processing Step(s)
- op2 = pop
op1 = pop
t = newNode (-,op1, op2);
push(stack, t);
5 3
- 4
5 3
* op2 = pop
op1 = pop
t = newNode (*,op1, op2);
push(stack, t);
Expression Tree Stack (top at right)
- 4
5 3
* 9
- 4
5 3
+
End of the expression has
* 9 been reached, and the full
expression tree is the only
- 4 tree left on the stack
5 3
〖 Example 〗 ( a + b ) * ( c * ( d + e ) ) = a b + c d e + * *
+
a b *c d * +
e
T2 T2 a + b c T1 d * + e T1 T1
a bT2 T2 c d + e T1
d e
28/11/2025 School of Computer Engineering, MIT, Manipal 91
8/16
Threaded Binary Tree
Threads
• In a linked representation of a binary tree, there are more NULL
links than actual pointers.
Threading Rules
• A NULL RightChild field at node p is replaced by a pointer to
the node that would be visited after p when traversing the tree in
inorder. That is, it is replaced by the inorder successor of p.
• A NULL LeftChild link at node p is replaced by a pointer to the
node that immediately precedes node p in inorder (i.e., it is
replaced by the inorder predecessor of p).
B C
D E F G
H I
Inorder sequence: H, D, I, B, E, A, F, C,
G
28/11/2025 School of Computer Engineering, MIT, Manipal 94
Threaded Tree Corresponding to Given Binary Tree
B C
D E F G
H I
Inorder sequence: H, D, I, B, E, A, F, C,
G
28/11/2025 School of Computer Engineering, MIT, Manipal 95
Threads
• To distinguish between normal pointers and threads,
two boolean fields, LeftThread and RightThread, are
added to the record in memory representation.
• t->leftThread= TRUE
=> t->lchild is a thread
• t->leftThread= FALSE
Þ t->lchild is a pointer to the left child.
• t->rightThread= TRUE
=> t->rchild is a thread
• t->rightThread= FALSE
=> t->rchild is a pointer to the right child.
struct threadedTree{
short int leftThread;
threadedPointer lchild;
char data;
threadedPointer rchild;
short int rightThread;
};
TRUE FALSE
F - F
F A F
F B f F C F
F D F T E T T F T T G T
T H T T I T
3 8
1 5 7 11
9 13
Start at leftmost node, print it
3 8
1 5 7 11
9 13
Follow thread to right, print node
3 8 5
1 5 7 11
9 13
Follow link to right, go to
leftmost node and print
3 8 5
6
1 5 7 11
9 13
Follow thread to right, print node
3 8 5
6
7
1 5 7 11
9 13
Follow link to right, go to
leftmost node and print
3 8 5
6
7
1 5 7 11 8
9 13
Follow thread to right, print node
3 8 5
6
7
1 5 7 11 8
9
9 13
Follow link to right, go to
leftmost node and print
3 8 5
6
7
1 5 7 11 8
9
11
9 13
Follow thread to right, print node
3 8 5
6
7
1 5 7 11 8
9
11
9 13 13
s s
r r
before after
28/11/2025 School of Computer Engineering, MIT, Manipal 113
Insertion of r As A Right Child of s in A Threaded Binary Tree
(Cont.)
s s
r r
before after
28/11/2025 School of Computer Engineering, MIT, Manipal 114
Advantages
• Efficient Traversal: Threaded binary trees allow for linear and fast traversal of nodes in the
tree without the need for a stack. This eliminates the memory and time overhead associated
with stack-based traversal methods.
• Reduced Memory Consumption: Since threaded binary trees do not require a stack for
traversal, they consume less memory compared to other traversal methods that rely on
recursion or iterative approaches with stacks.
30 60
20
5 40 70
15 25
65 80
14 10 22 2
8 15 8 15
5 9 12 17 5 9 12 17
13
7 15
7 15
5 9 12 17
5 9 12 17
30 30
5 40 2
5 40
2 35 80 2 80
20 60 20 60
10 30 50 70 10 30 50 70
45 55 55
52 52
Before deleting 40 After deleting 40
if (*root== NULL){
printf("Empty Tree\n"); return;
}
//traverse the tree until the item is found or entire tree is traversed
parent = NULL;
cur = *root;
while(cur && (cur->data!= item)){
parent = cur;
if (item<cur->data)
cur = cur->lchild;
else
cur = cur->rchild;
}
if (cur==NULL) {
printf("Item Not Found\n");
return;
28/11/2025 } School of Computer Engineering, MIT, Manipal 132
Function for BST Delete ( Replaced by the smallest element in its right
subtree)
//item found and check for case 1
if (cur->lchild == NULL) //node to be deleted has empty left subtree
q= cur->rchild; //get the address of right subtree
else if (cur->rchild == NULL) //node to be deleted has empty right subtree
q = cur->lchild; //get the address of left subtree
else //interior node
{
//find inorder successor->smallest element in the right subtree
parent = cur;
succ = cur->rchild; //get address of rightchild of node to be deleted*/
if (parent == NULL){
free(cur);
*root = q;
return;
}
if (cur== parent->lchild)
parent->lchild = q;
else
parent->rchild = q;
free(cur);
return;
}
• Binary tree.
• If T is a nonempty binary tree with TL and TR as its left and right sub trees,
then T is an AVL tree iff
•|hL – hR| <= 1 where hL and hR are the heights of TL and TR, respectively
7 1
40
1
0 3 0 8 1 30 -
415
0 -1
0 0 0
1 5 20 35 60
25
• To insert an element into an AVL search tree, the result may not be an AVL tree.
(i.e.) the tree may become unbalanced
• If the tree becomes unbalanced, we must adjust the tree to restore balance - this
adjustment is called rotation
• There are four different cases when rebalancing is required after insertion of
new node.
• left sub tree of left child (LL )
• right sub tree of left child (RL )
• left sub tree of right child (LR )
• right sub tree of right child (RR )
28/11/2025 School of Computer Engineering, MIT, Manipal 148
Definition of Rotation
• Any modification done on an AVL tree in order to rebalance it is called a rotation of an AVL
tree.
1. Single Rotation
• LL rotation(Left-Left)
• RR rotation(Right Right)
• Create new node and insert a new node as leaf node as in ordinary
binary search tree.
• Now track the path from insertion point towards root.
• Checks each node of heights of left (n) and right (n) differ by at most 1.
If yes, move towards parent(n)
Otherwise restructure by doing either a single or double rotation.
LL rotation
Insert Node 1 1
LL rotation 0
0
0
BF=0
Balanced Tree
Unbalanced Tree
RR rotation
BF=-1
-2 0
Insert Node 3 RR rotation
-1
BF=0 0 0
0
RR rotation LL rotation
BF=1 0
2 2
Insert Node 2 RR rotation
LL rotation
-1 1
0
0 0
0
BF=0
Unbalanced Tree Unbalanced Tree
Balanced Tree
LL rotation RR rotation
1
20 Insert 5, 40
0 1
10 30
0 0
25 35
0
1
20 20
1 0 0 1
10 30 10 30
0 0 0 -1
0 0
5 25 35 5 25 35
0
40
Now Insert 45
2
1
20 20
1 -2 1 -1
10 30 10 30
0 0 -2
0 0
5 25 35 5 25 40 0
0 0
35 45
Imbalance 1 40
0 45
Now Insert 34
-2
-1
20 20
1 -2 1 0
10 30 10 35
0 0 1
0 0
5 Imbalance 25 40 5 30 40 -1
0
1 0 25 34 45
35 45 0
Insertion of 34 0
34
3 2
3
3
2 1
Fig 1 2 3
Fig 4
Fig 2
2 1 2
Fig 3
1 3
1 3
Fig 5 Fig 6 4
4 5
1 4
1 4
3 5
3 5
Fig 8
Fig 7 6
4
4
2 5
2 5
1 3 6
1 3 6
4
Fig 10 7
Fig 9
2 6
1 3 7
5 Fig 11
28/11/2025 School of Computer Engineering, MIT, Manipal 165
4 4
2 6 2 6
1 3 7 1 3 7
5 16 5 16
Fig 12
Fig 13
15
4
2 6
1 3 15
5
16
Fig 14 7
2 2 7
6
15 1 3 15
1 3 5 6
16
7 5 14 16
Fig 15
14 Fig 16
171
28/11/2025 School of Computer Engineering, MIT, Manipal
Implementation
// Insert element into AVL tree
else if (x > (*t)->element) {
void insert(int x, AvlNode **t) {
insert(x, &((*t)->right));
if (*t == NULL) {
if (height((*t)->right) - height((*t)->left) == 2) {
*t = (AvlNode*)malloc(sizeof(AvlNode));
if (x > (*t)->right->element)
(*t)->element = x;
rotateWithRightChild(t);
(*t)->left = (*t)->right = NULL;
else
(*t)->height = 0;
doubleWithRightChild(t);
}
}
else if (x < (*t)->element) {
}
insert(x, &((*t)->left));
// Duplicate: do nothing
if (height((*t)->left) - height((*t)->right) == 2) {
if (x < (*t)->left->element)
(*t)->height = MAX(height((*t)->left), height((*t)->right)) + 1;
rotateWithLeftChild(t);
}
else
doubleWithLeftChild(t);
}
}
172
28/11/2025 School of Computer Engineering, MIT, Manipal
Implementation
// Test the AVL tree
int main() {
• // In-order traversal AvlNode *root = NULL;
• void inorder(AvlNode *t) {
• if (t != NULL) { int arr[] = {30, 20, 40, 10, 25, 35, 50, 5};
int n = sizeof(arr)/sizeof(arr[0]);
• inorder(t->left);
• printf("%d ", t->element); for (int i = 0; i < n; i++)
• inorder(t->right); insert(arr[i], &root);
• }
printf("Inorder traversal of AVL tree:\n");
• } inorder(root);
printf("\n");
return 0;
}
Delete 11
Delete 12
Delete 14
1 0 0 -1
0 0 0 0
• Every simple path from a node to a descendant leaf contains the same
number of black nodes=black-height(x).
17 41
NIL NIL
30 47
NIL 38 NIL 50
h=4
26 bh =
2
h=1 h=3
bh = 1 17 41 bh =
2
NIL h=2 h=2
30 bh =
47 bh =
h=1 1
NIL 1 bh = 1
h=1
NIL 38 NIL 50 bh =
1
NIL NIL
NIL NIL
The tree insert routine has just been called to insert node
"4" into the tree.
This is no longer a red-black tree - there are two successive
red nodes on the path
11 - 2 - 7 - 5 - 4
Mark the new node, x, and it's uncle, y.
y is red, so we have case 1 ...
28/11/2025 School of Computer Engineering, MIT, Manipal 1
8
3
Change the colors of nodes 5, 7 and 8.
• x's parent (2) is still red, so this isn't a red-black tree yet.
Still not a red-black tree... the uncle is black, but x's parent is
to the left .