8 - Trees
8 - Trees
(CS3401)
DATA STRUCTURES
Dr Somaraju Suvvari
2
NITP -- CS3401
TREES
&
GRAPHS
Dr Somaraju Suvvari
3
NITP -- CS3401
UNIT V: Trees & Graphs
Binary tree, Binary search tree, Threaded binary tree, AVL Tree, B Tree, Tries, Heaps, Hash tables.
Graph and its implementation, Graph traversals: Breadth First Search, Depth First Search, Union-find data
structure and applications, Spanning Tree – Prim’s algorithm and Kruskal’s algorithm, Shortest path- Dijkstra's
algorithm and Bellman Ford algorithm., Topological sorting for Directed Acyclic Graph.
Dr Somaraju Suvvari
4
NITP -- CS3401
Trees
• One way to define tree is recursively. “Tree is a non-linear data structure and it is a
collection of nodes. The collection may be empty ; otherwise, a tree consists of a
distinguished node called root, r, and zero or more non empty (sub) trees T1, T2, ….Tk, each
of whose roots are connected by a directed edge from r”.
• Node in a tree data structure stores the actual data and a link to next element in hierarchical
structure.
Dr Somaraju Suvvari 5
NITP -- CS3401
• Root Trees Terminology
Root
• Edge
Top most node in the tree. Root node is the top
• Parent If Root is NULL means that tree is empty. most node in the tree. If
Tree contains only one Root node. Root is NULL means
• Child
that tree is empty.
• Siblings TOP
T A
• Leaf Edge Edge
The connecting link between any two
• Internal Nodes nodes in a tree is called as EDGE. T1 B F T2
In a tree with 'N' number of nodes there
• Degree
will be a maximum of 'N-1' number of
T3 T4 T5 T7 T8
• Level edges. C D E G I
• Height
• Depth T6 T9
J
H
• Path BOTTOM
• Sub-Tree There are totally 10 nodes (in general n
nodes), 9 edges (in general n-1 edges).
• In-degree T1 and T2 are sub-trees of Root node T,
• Out-degree and T3, T4, T5 are the subtrees of T1, ……
Dr Somaraju Suvvari 6
NITP -- CS3401
• Root Tree - Terminology A
• Edge
B C
• Parent Parent
• Child o The node which is a predecessor of any node D E F G H
• Siblings is called as PARENT NODE.
• Leaf o The node which has child / children I J K
• Height
D, E and F are siblings. D E F G H G and H are siblings.
• Depth
• Path
I and J are Siblings I J K
• Sub-Tree
• In-degree
Dr Somaraju Suvvari 9
• Out-degree NITP -- CS3401
Tree - Terminology
• Root A
• Edge Leaf
B C
• The node which does not have a child is called
• Parent as LEAF Node.
• Child • In simple words, a leaf is a node with no child. D E F G H
• Siblings
• Leaf I J K
F, I, J, K and H are Leaf Nodes.
• Internal Nodes
A
• Degree
• Level Internal Node B C
• The node which has at least one child is called
• Height
as INTERNAL Nodes.
• Depth • In simple words, an internal node is a node with at least D E F G H
one child.
• Path
I J K
• Sub-Tree
• In-degree A, B, C, E and G are the Internal Nodes
Dr Somaraju Suvvari 10
• Out-degree NITP -- CS3401
Tree - Terminology
• Root Degree of tree is 3
A
• Edge Degree Degree of A is 2
• The Degree of a node is the total number B C
• Parent Degree B is 3
of children it has. Degree C is 2
• Child • The highest degree of a node among all the Degree of E is 2
D E F G H
• Siblings nodes in a tree is called as 'Degree of Tree' Degree of G is 1
Degree of D, F, H and
• Leaf I J K K is 1
• Internal Nodes
• Degree
• Level Level A Level 0
• The root node is said to be at Level 0 and the
• Height children of root node are at Level 1 and the Level 1
B C
• Depth children of the nodes which are at Level 1 will
be at Level 2 and so on...
• Path D E F G H Level 2
• Sub-Tree
• In-degree I J K Level 3
Dr Somaraju Suvvari 11
NITP -- CS3401
Tree - Terminology
3
• Root Height of tree :3
Height A
• Edge • The total number of edges from leaf node to a particular 2
2
node in the longest path is called as HEIGHT of that B C
• Parent node.
• Height of the root node is said to be height of the tree. 0 1 0 0
• Child 1
• height of all leaf nodes is '0'. D E F G H
• Siblings
• Leaf 0 I J 0 K 0
• Internal Nodes
A Depth of A is 0
• Degree Depth
Depth of C is 1
• The total number of edges from root node to a Depth (B)=1
• Level particular node is called as DEPTH of that node B C
• Height • The total number of edges from root node to a leaf Depth (D) = 2
node in the longest path is said to be Depth of the D E F G H
• Depth tree.
• Path • Depth of root node is 0.
I J K Depth(K) = 3
• Sub-Tree
• In-degree
Dr Somaraju Suvvari 12
NITP -- CS3401
• Root Tree - Terminology
• Edge Path between A and J is A-B-E-J and path
Path length is 4.
• Parent • Sequence of nodes and edges from one node
to another node is called as path. A
• Child • Number of nodes in a path is the Path Length.
• Siblings B C
• Leaf
D E F
• Internal G H
Nodes Sub-tree A J
• Degree
B C Sub-tree
• Level Sub-Tree
• Each child from a node forms a
• Height D E F G H
sub-tree recursively.
• Depth • Every child node will form a sub-
tree on its parent node.. Sub-tree
• Path I J K
• Sub-Tree Sub-tree
• Indegree Dr Somaraju Suvvari 13
NITP -- CS3401
• Root Tree - Terminology
• Edge
In-degree-
• Parent • It is the number of edges arriving at a node.
• Child • The root node is the only node that has an in-degree equal to zero.
Out-degree
• Siblings • Similarly, out-degree of a node is the number of edges leaving that
node. In-degree(A)=0,
• Leaf
• Usually, leaf nodes out-degree is zero. Out-degree of (A) is 2.
A
• Internal
Nodes B C
• Degree
D E F G H
• Level
• Height
I J K
• Depth
• Path In-degree(B)=1, Out-degree of (B) is 3.
• Sub-Tree
• In-degree Dr Somaraju Suvvari 14
NITP -- CS3401
Representation of Trees
One way to represent tree is using the List representation.
Node Child-1 Child-2 …… …….. ……… ………. …….. ……… Child-n
data Link Link Link
C
D E F
G H G H
B J
D E F
The dis advantage of this approach is that it requires too many pointers
I
Dr Somaraju Suvvari 15
NITP -- CS3401
Binary Tree
Any tree can be called as Binary Tree if every node in the tree has either zero or one
or two children only (A Tree where no node has more than two children).
B C
D E
G H
J
Binary Tree
Dr Somaraju Suvvari 16
NITP -- CS3401
Types of Binary Trees
1. Strict Binary tree – It is a binary tree where each node has exactly two children
A
or no children.
B C
G H
Strict Binary Tree
2. Full Binary Tree - It is a binary tree where each non leaf node has exactly two
A
children and all the leaf nodes are at the same level.
B C
D E G H
Dr Somaraju Suvvari 17
NITP -- CS3401
Types of Binary Trees
3. Complete Binary tree – In a complete binary tree, all the levels of a tree are filled entirely
except the last level. In the last level, nodes might or might not be filled fully and also all the
nodes should be filled from the left.
A
B C
D E G
Dr Somaraju Suvvari 18
NITP -- CS3401
Properties of Binary Trees
A
In a binary tree B C
• The number of nodes in a full binary tree of height h is 2h+1 – 1, since there are h
levels and at each level i there are exactly 2i nodes, so 20 + 21 + 22 + … +2h = 2h+1 – 1.
• The number of nodes in a complete binary tree is in between 2h (minimum) and 2h+1 -
1 (maximum).
Dr Somaraju Suvvari 19
NITP -- CS3401
Representation of Binary Trees
Binary trees can be represented using two ways
1. We can store binary trees in arrays, and specially if the tree is a complete binary tree. In this
method, if a node has an index i, its children are found at indices 2*i+1 and 2*i+2, while its
parent (if any) is found at index floor((i-1)/2) (assuming the root of the tree stored in the array at
A
an index zero). B C
This method wastes 2h+1 – n – 1 for a tree of height h with n nodes.
D E G
Consider the complete tree shown here and it can be represented using arrays as follows:
A B C D E G
0 1 2 3 4 5 6
Dr Somaraju Suvvari 20
NITP -- CS3401
Representation of Binary Trees
Binary trees can be represented using two ways
2. The second alternative method is Linked Representation (pointer based one), where each node is partitioned into
three parts, one is used to store the actual data of that node, another one is used to store the left node address,
another one is used to store right node address. In the absence of child it stores NULL value. A
B C
node *right;
} *root = NULL;
D E G
Dr Somaraju Suvvari
21
NITP -- CS3401
Operations on Binary Tree
Basic Operations
1. Inserting an element into a tree
Auxiliary Operations
5. Finding the size of the tree
Dr Somaraju Suvvari 22
NITP -- CS3401
Binary Tree Traversal Techniques
The problem of visiting the nodes of a tree is called tree traversal. There are mainly
three different traversal techniques,
1. Inorder traversal
2. Preorder Traversal
3. Postorder Traversal
One more traversal technique called level order traversal is also used.
Dr Somaraju Suvvari 23
NITP -- CS3401
Inorder Tree Traversal Technique
In inorder traversal the vising of the nodes are defined as follows A
1. Traverse the Left subtree in Inorder B C
For example if we apply the Inorder traversal in the tree shown here results the
following order:
DBE AGC
Dr Somaraju Suvvari 24
NITP -- CS3401
Inorder Tree Traversal Technique (Recursive code)
void Inorder(node *r)
{ if(r)
{Inorder(r left);
output r data;
Inorder(r right); }
}
Dr Somaraju Suvvari
25
NITP -- CS3401
Inorder Tree Traversal Technique (Recursive code)
root 1010
For example if we apply the code on the shown tree: A
1 1010 1016 B C 1060
Inorder(1010) 2
{if(1010) { Inorder(1010left); Inorder(1016) 3 1002 D
Output(1010data); { if(1016) { Inorder(1016left); Inorder(1002) 4
Inorder(1010right); Output(1016data); { If(1002) { Inorder(1002left); Inorder(NULL)
} Inorder(1016right); Output (1002data); { if(NULL){}
} Inorder(1002right); }
3
6 }
7 Inorder(NULL) 5
Inorder(1060) 8 {if(NULL) Inorder(NULL)
{ if(1060) { Inorder(1060left); // NULL {} { if(NULL){}
Output(1060data); }
Inorder(1060right); // NULL 2
1
} 9
4
B D
C A
Dr Somaraju Suvvari
26
NITP -- CS3401
Preorder Tree Traversal Technique
In preorder traversal the vising of the nodes are defined as follows A
1. Process the root B C
For example if we apply the preorder traversal in the tree shown here results the
following order:
ABDECG
Dr Somaraju Suvvari 27
NITP -- CS3401
Postorder Tree Traversal Technique
In post order traversal the vising of the nodes are defined as follows A
1. Traverse the Left subtree in postorder B C
The visiting order in Inorder traversal is < Left Right Root >
For example if we apply the post order traversal in the tree shown here results the
following order:
DEBGCA
Dr Somaraju Suvvari 28
NITP -- CS3401
Level order Tree Traversal Technique
• In level order traversal technique the nodes are visited level by level starting from the root, and
going from left to right. The level order traversal requires a queue data structure.
For example if we apply the level order tree traversal in the tree shown here results
the following order:
ABCDEG A
B C
D E G
Dr Somaraju Suvvari 29
NITP -- CS3401
ASSIGNMENT
1. Write the pseudo code for constructing a complete binary tree with n nodes?
2. Write the non-recursive pseudo codes for the inorder, preorder, post order and
level order tree traversal techniques.
3. How to construct a binary tree if the inorder and post order sequence is given?
Dr Somaraju Suvvari 30
NITP -- CS3401
Expression Tree
A binary tree is called expression tree if the operands of the expression are stored at
leaves and the operators of the expression are stored at internal nodes (non-leaf
nodes).
+ -
a b c d
Dr Somaraju Suvvari 31
NITP -- CS3401
Construction of an Expression Tree
An expression tree can be constructed from an postfix expression or prefix expression
The following is the procedure for constructing the expression tree from the given postfix
expression:
(Create a stack to store the nodes)
1. Read the each symbol from the postfix expression from left to right
2. If the symbol read is an operand then create a new node and initialize its both left and right
pointers with null. Store the operand into the node. Now push this node into the stack.
3. If the symbol read is an operator (assume binary operator) then pop the top two nodes from the
stack, assume these are T1 (T1 at the top of the stack) and T2. Now create a new node, lets call it
as T3 and store the operator in it. Store T2 in T3’s left pointer and store T1 in T3’s right pointer.
Dr Somaraju Suvvari 32
NITP -- CS3401
Construction of an Expression Tree (Example)
ab+cd-*
After reading a
After reading b
a b
After reading +
a b
Dr Somaraju Suvvari 33
NITP -- CS3401
Construction of an Expression Tree (Example)
After reading c
+ c
a b
After reading d
+ c d
a b
Dr Somaraju Suvvari 34
NITP -- CS3401
Construction of an Expression Tree (Example)
After reading -
+ -
a b c d
After reading *
+ -
a b c d
Dr Somaraju Suvvari 35
NITP -- CS3401
Expression Tree
Notes:
1. If we run an preorder traversal on the expression tree constructed from the
postfix expression gives a prefix expression (postfix to prefix)
2. If we run an post order traversal on the expression tree constructed from the
prefix expression gives a postfix expression (prefix to postfix).
Dr Somaraju Suvvari 36
NITP -- CS3401
Binary Search Tree
The most widely used binary type of tree is Binary Search Tree
A binary tree is called binary search tree if it follows the following properties:
1. Every element has a key and no two elements have the same key’
2. The keys in the left sub tree are smaller than the key value of its root
3. The keys in the right subtree are greater than the key value of its root
4. The left and right sub trees are also binary search trees
Dr Somaraju Suvvari 37
NITP -- CS3401
Binary Search Tree - Example
Example
3 9
1 4 8 12
Dr Somaraju Suvvari 38
NITP -- CS3401
Binary Search Tree
typedef struct BinarySearchTree BST;
struct BinarySearchTree {
<datatype> data;
BST *left;
BST *right;
}*root = NULL;
Dr Somaraju Suvvari 39
NITP -- CS3401
Binary Search Tree
Finding an element in a Binary Search Tree:
1. The element to be searched is compared with the root element, if both are matches then we return that
node.
2. If searching element is less than the root element then we search in the left sub tree of the current node.
3. If the searching element is greater than the root element then we search in the right sub tree of the current
node.
BST *Find(BST *r, int s) // Recursive implementation
{ if(r != NULL)
{if (r data == s) return r; // Element node address
else if(r data > s) return(Find(r left, s));
else r = return(Find(r right, s)); } return NULL; // if element not found
}
Time Complexity (in worst case / when the tree is skewed one (All the nodes are in one side)) = O(n)
Time Complexity (in average case) = O(log n)
Space complexity = O(n), for recursive stack.
Dr Somaraju Suvvari 40
NITP -- CS3401
Binary Search Tree
Finding an element in a Binary Search Tree:
BST *Find(BST *r, int s) //Non recursive version
{ if(r == NULL) return NULL;
while ( r )
{if (r data == s) return r; // Element node address
else if(r data > s) r = r left;
else r = r right;
} return NULL; // if element not found
}
Time Complexity (in worst case / when the tree is skewed one (All the nodes are in one side)) = O(n)
Time Complexity (in average case) = O(log n)
Space complexity = O(1)
Dr Somaraju Suvvari 41
NITP -- CS3401
Binary Search Tree
Finding the minimum element in a Binary Search Tree:
BST *Find_Min(BST *r) // Recursive version
{ if(r == NULL) return NULL;
if (r left == NULL) return r; // Element node address
else return(Find_Min(r left));
}
Time Complexity (in worst case / when the tree is skewed one (All the nodes are in one side)) = O(n)
Time Complexity (in average case) = O(log n)
Space complexity = O(n)
Dr Somaraju Suvvari 42
NITP -- CS3401
Binary Search Tree
Inserting an element in a Binary Search Tree:
To insert a data into the binary search tree, find the location by using mechanism used in find() operations, if the
element is already there then simply neglect and exit from function. Otherwise insert the data at the last location
on the path traversed.
BST *insert(BST *r, int x)
{ if ( r == NULL) { r = (BST *) malloc (sizeof(BST));
if (r == NULL) { printf(“No memory….”); return;}
else { r data = x; r left = r right = NULL;}
}
else{
if (r data > x ) { r left = insert(r left, x); }
else if(r data < x) { r right = insert(r right, x); }
}
return r;
}
Time complexity (worst case) - O(n)
Time complexity (average case) - O(log n)
Dr Somaraju Suvvari
NITP -- CS3401 43
Binary Search Tree (Insertion Example)
Insert the following elements into a empty binary search tree: root
12, 6, 23, 39, 7 1010
2 root 3
1 1010
Initially root After inserting 12 After inserting 6
2004 12
NULL 12
1010
1010 6
root 2004
4 After inserting 23 1010
root
5 After inserting 39 & 7
2004 12 1016 1010
2004 1016
39
7
1030
2030
Dr Somaraju Suvvari 44
NITP -- CS3401
Binary Search Tree (Deletion)
Deleting an element in a Binary Search Tree
Case – 1
The deleted element is a leaf node then return NULL to its parent node.
Case 2
The deleted element has one child, then send the child node to the parent of the deleted node.
Case 3
The deleted node has both left and right child, then the preferred strategy is to replace key of this
node with the largest element of the left subtree and recursively delete that node.
Dr Somaraju Suvvari 45
NITP -- CS3401
Binary Search Tree (Deletion Example)
Consider the following binary search tree:
3 8
3 8
2 4 7 9 4 7 9
6
6
5
Delete the element 3 (Case – 2) 5 Delete the element 8 (Case – 3)
4 7
4 8
6 9
7 9
6 Suvvari
Dr Somaraju 46
NITP -- CS3401
Binary Search Tree (Deletion – pseudo code)
Deleting an element in a Binary Search Tree
BST *Delete(BST *r, int x)
{ if (r== NULL) printf(“Element Not found”); // if tree is empty or no nodes left to search x, then declare element not found
else if (r data > x) { r left = Delete(r left, x);} // if x is less than the element in r then traverse the left sub tree of r
else if (r data < x) {r right = Delete(r right, x); // if x is less than the element in r then traverse the right sub tree of r
else // if element found
{ BST *temp = r;
if( r left == NULL && r right == NULL) { return NULL;} // Case-1
else if(r left == NULL) { r = r right; free(temp);} // Case-2
else if(r right == NULL) { r = r left; free(temp);} // Case-2
else if( r left && r right) // case-3
{ temp = Find_Max( r left);
r data = temp data;
r left = Delete (r left, temp data);}
}
return r;
}
Time complexity (worst case – if binary search tree is a skewed one) = O(n)
Time Complexity (average case) = O (log n)
Space complexity = O(n) [stack] // if we implement non recursive one then the space complexity will be O(1)
Dr Somaraju Suvvari 47
NITP -- CS3401
Priority Queues
In some application we need to find the minimum/maximum element in a set of elements. We can do these
operations using Priority Queues.
Priority Queue is a Data Structure that allows at least the following two operations:
1. Insert
2. Delete Minimum (or Delete Maximum)
These operations are equivalent to Enqueue and Dequeue operations of a queue, but in priority queue the
order in which elements are inserted in the queue may not be the same order in which they were processed.
Operations
1. Insert (key, data) – Inserts data with key into the priority queue
2. Delete Min/ Delete Max
3. Find Minimum/ Maximum
Dr Somaraju Suvvari 48
NITP -- CS3401
Priority Queues
Applications
1. In data compression algorithms like, in Huffman coding algorithm
Dr Somaraju Suvvari 49
NITP -- CS3401
Priority Queues
Implementation of Priority Queues
1. Using the arrays - Insert the elements into the array and when deleting the element find the smallest
element and delete it.
2. Using Linked Lists – It is very similar to arrays, instead of arrays here we will use the linked lists.
3. Binary Search Tree Implementation – Both insertion and deletions will take O(logn ) in average case.
4. Using the binary Heap implementation – Gives O(log n) complexity in all the insertion, deletion and
searching for an element. Finding the maximum and minimum will take O(1) complexity.
Dr Somaraju Suvvari 50
NITP -- CS3401
Heap
Heap is a tree which must satisfy two properties
1. Structure Property - The tree must be completely filled, with the possible
exception of the bottom level, which is filled from left to right (complete binary
tree). All the leaves must be at level h or h-1.
2. Order Property – The key of parent node must be smaller (or larger ) than its
children.
Types of Heaps
1. Min heap – The value of a node must be less than or equal to its children.
2. Max heap - The value of a node must be greater than or equal to its children.
Dr Somaraju Suvvari 51
NITP -- CS3401
Binary Heap
Any heap is called binary heap if each node may have up to two children.
Representing Heaps – Heaps can be represented using arrays. For any element in array at
position i, the left child is in position 2*i+1 and the right child is in the position 2*i +2 and
the parent is in position floor((i-1)/2).
Inserting a element into the binary heap - Insert the new element in the next available
location in the array. If it violates the heap order property the new element is percolated up
(swapping with its parent) the heap until the correct location is found.
Deleting the element from the Min Heap – Replace the root element with the last element.
Slide down (heapify – swap with its children) to the minimum of the of the children till it
finds it correct position.
Dr Somaraju Suvvari 52
NITP -- CS3401
Binary Heap – Example Insertion
Insert the following elements into the empty binary min heap: 12, 1, 9, 5, 10, 23
1
Initially
0 1 2 3 4 5
2
12 12
Insert 12
0 1 2 3 4 5
3
12 1
Insert 1 12 1
0 1 2 3 4 5
1 12
Violating the order property - Percolated Up
1 12
Dr Somaraju Suvvari 53
NITP -- CS3401 0 1 2 3 4 5
Binary Heap – Example Insertion
Insert the following elements into the empty binary min heap: 12, 1, 9, 5, 10, 23
1 12 1
0 1 2 3 4 5
12
4
1
1 12 9
Insert 9
0 1 2 3 4 5 12 9
5 1 12 9 5
Insert 5 1
0 1 2 3 4 5 1
0 1 2 3 Dr 4 5
Somaraju Suvvari
54
NITP -- CS3401
Binary Heap – Example Insertion
Insert the following elements into the empty binary min heap: 12, 1, 9, 5, 10, 23
1 5 9 12
0 1 2 3 4 5 1
6
Insert 10 5 9
1
1 5 9 12 10
12
0 1 2 3 4 5
5 9
7 12
10
1
Insert 23
1 5 9 12 10 23 5 9
0 1 2 3 4 5
12
10 23
Dr Somaraju Suvvari
55
NITP -- CS3401
Binary Heap
Perform delete operation: 1
1 5 9 12 10 23
0 1 2 3 4 5 5 9
12
10 23
23 5 9 12 10
23
0 1 2 3 4 5
5 9
Heapify 12
10 5
5 23 9 12 10
23 9
0 1 2 3 4 5
12
10
Dr Somaraju Suvvari 56
NITP -- CS3401
Binary Heap
Perform delete operation: 5
5 23 9 12 10
0 1 2 3 4 5 23 9
Heapify 12
10
5 10 9 12 23
0 1 2 3 4 5
10 9
12
23
Dr Somaraju Suvvari 57
NITP -- CS3401
Heap Sort
Heap Sort (From Max Heap)
1. Construct the Max Heap from the given input
2. Remove the root and put it at the end of the array. Put the last element of the heap at
the vacant place (At root)
3. Reduce the size of the heap by one and heapify the root element again so that we
have highest element at the root.
4. The process is repeated until all the elements of the list are sorted.
Dr Somaraju Suvvari 58
NITP -- CS3401
AVL Trees
• We have seen many data structures whose worst case time complexity for searching an element is
O(n), where n is the number of elements.
• Even in the binary search trees it happens when the tree is skew one.
• We can improve the complexity of searching an element in the worst case to O(log n) using the
special data structure Height Balanced trees.
• In the height balanced trees the difference between the left subtree and right subtrees (represented
using k and some times it is called as balance factor) is considered in balancing the height of the
A
tree and is called as balance factor of the tree represented using the HB(k).
B C
• In full balanced binary search tree the value of k is ZERO.
D E G H
Dr Somaraju Suvvari 59
NITP -- CS3401
AVL Trees
• If the value of k is one, then such a tree is called as AVL tree, i.e., Any binary search tree is called
as AVL Tree if the difference between the left subtree height and the right subtree height is at most
one.
A k=2 A k=2
k=1 B C k=1 k=1 B C
D H D E
Dr Somaraju Suvvari 60
NITP -- CS3401
AVL Trees
• Maximum number of nodes in an AVL tree is possible if the height of the left subtree and right subtree is differed
by zero.
A
• Max(n(h)) = n(h-1) + n(h-1) + 1 log n = O(log n)
B C
D E
F
Dr Somaraju Suvvari 61
NITP -- CS3401
AVL Tree - Declaration
AVL Tree declaration
typedef struct AVLTree AVL;
struct AVLTree {
<data type> data;
AVL *left;
AVL *right;
int height; };
Insertion
AVL tree is similar to Binary Search Tree, so find the proper place for the new element as we do in the Binary
Search Tree and insert the new element. But after insertion the possibility is that it may violate the balance
factor.
So whenever after insertion there is a violation in balance factor then fix the size of the tree by rearranging the
nodes.
Note: Always fix the first node that is not satisfying the AVL property on the path from
the insertion point to the root.
Dr Somaraju Suvvari 62
NITP -- CS3401
AVL Tree - Insertion
Types of Violations
Let us assume the node that violates the AVL property on the path from the insertion
point to the root is X. The following are the four types of possible violations:
1. Inserting a node into the left subtree of the left child of X (Case-1).
2. Inserting a node into the right subtree of the left child of X (case -2).
3. Inserting a node into the left subtree of the right child of X (case -3).
4. Inserting a node into the right subtree of the right child of X (case - 4).
These violations can be fixed using single rotation or using the double rotation
Dr Somaraju Suvvari 63
NITP -- CS3401
AVL Tree - Rotations
Single Rotations
Left – Left Rotation (LL – rotation to solve case -1)
Consider the following AVL Tree
k=2
X
k=1 Y k=0
C
B
A
A new node is inserted into the left subtree of the left child of Y, Violation happened at node X;
That is height of the left subtree is h + 2 and height of the right subtree of X is h, so the balance
factor k = 2.
Dr Somaraju Suvvari 64
NITP -- CS3401
AVL Tree - Rotations
Single Rotations
Left – Left Rotation (LL – rotation to solve case -1)
Consider the following AVL Tree k=1 Y
k=2
X
X
k=1 Y k=0 A
C B C
B
A
Reduce the height of the left tree of X by one by increasing the height of right subtree of X to one.
Dr Somaraju Suvvari 65
NITP -- CS3401
AVL Tree - Rotations
Single Rotations
Left – Left Rotation (LL – rotation to solve case -1) Example
Consider the following AVL Tree (new element inserted is 1)
8 8
5 12 3 12
19
3 19 2 5
6
2 1 3 4 6
4
T right = X; C B C
X height = MAX(Xleftheight, Xrightheight) + 1; B
A
T height = MAX(Tleftheight, Xheight) + 1;
return T;
}; Time Complexity - O(1) and Space Complexity – O(1)
Dr Somaraju Suvvari 67
NITP -- CS3401
AVL Tree - Rotations
Single Rotations
Right – Right Rotation (RR – rotation to solve case - 4)
Consider the following AVL Tree
k=2
X
k=1 k=1
C Y
B A
A new node is inserted to the right subtree of the right child of X, violation happened at node X;
That is height of the right subtree is h + 2 and height of the left subtree of X is h, so the balance
factor k = 2 at node X.
Dr Somaraju Suvvari 68
NITP -- CS3401
AVL Tree - Rotations
Single Rotations
Right – Right Rotation (RR – rotation to solve case - 4)
Consider the following AVL Tree
Y
k=2
X
X
k=1 k=1
Y A
C
C B
B A
Reduce the height of the right tree of X by one by increasing the height of left subtree of X to one
Dr Somaraju Suvvari 69
NITP -- CS3401
AVL Tree - Rotations
Single Rotations
Right – Right Rotation (RR – rotation to solve case - 4) Example
Consider the following AVL Tree (new element inserted is 1) 8
8
6 19
6 12
12
3 21
19 11
3 11 17 23
17 21
23
Dr Somaraju Suvvari 71
NITP -- CS3401
AVL Tree - Rotations
Double Rotations
Left – Right Rotation (LR – rotation to solve case - 2) – Required two rotations
Consider the following AVL Tree (new node is inserted in the right subtree of the left child of X).
AVL violations at node X.
X
Do the right X Z
Y rotation at Y
B Do the Left
Z X
B rotation at Z Y
Z
A Y
D D B
A C
C D
A C
Dr Somaraju Suvvari 72
NITP -- CS3401
AVL Tree - Rotations
Double Rotations
Left – Right Rotation (LR – rotation to solve case - 2) – Example
8 6
Do the right 8
4 9 rotation at 4
Do the Left 4 8
6 9
rotation at 6
3 6
4 3 9
7
7
7
3
73
AVL Tree - Rotations
Double Rotations
Right – Left Rotation (RL – rotation to solve case - 3) – Required two rotations
Consider the following AVL Tree (new node is inserted in the left subtree of the right child of X).
AVL violations at node X.
Z
X X
Do the left X
Y Y
rotation at Y Z
B B Do the Right
rotation at Z B
Z A Y C A
C D
C D A
D
Dr Somaraju Suvvari 74
NITP -- CS3401
ASSIGNMENT
Write the pseudo code for constructing a AVL tree for a given n elements.
Dr Somaraju Suvvari 75
NITP -- CS3401
B-Trees
Acknowledgement – I took this explanation from the book “Data structures and algorithm analysis in
C”, by Mark Allenweis
B-Trees are the search trees which are not binary (BST and AVL are binary trees).
2. All non leaf nodes except the root node have between ceil(M/2) to M children.
All the internal nodes are pointers P1, P2, …., PM to the children, and values k1, k2, k3, …. kM-1, representing the
smallest key found in the subtrees P2, P3, ….., PM, respectively. Some of these pointers are NULL. All the key
values in the subtree P1 are smaller then the keys in the sub tree P2, and so on. . We will insist that the number
of keys in a (non root) leaf is also between ceil(m/2) and m.
Dr Somaraju Suvvari 76
NITP -- CS3401
B-Trees
There are many definitions of B-Trees that change the structure in minor ways but the
definition was one of the standard one.
P1 k1 P2 k2 P3
A B-Treen of order 4 is popularly known as 2-3-4 tree and a B-Tree of order 3 is known as
2-3 tree.
Dr Somaraju Suvvari 77
NITP -- CS3401
B-Trees
Consider the following 3 order B-Tree
(Internal nodes are represented using the ellipse and the leaf nodes are represented as rectangles)
22: -
16: -
41: 58
Insert the key 18 - Find the place for 18 and if it does not violate the B-Tree properties add it to it
Dr Somaraju Suvvari 78
NITP -- CS3401
B-Trees
After inserting 18,
22: -
16: -
41: 58
Insert 1 Its position is in the left most rectangle and is already filled with the maximum
number of keys.
Solution Make two nodes of two keys each and adjusting the information in the parent.
Dr Somaraju Suvvari 79
NITP -- CS3401
B-Trees
After inserting 1, 22: -
11 : 16 41: 58
Dr Somaraju Suvvari 80
NITP -- CS3401
B-Trees
After inserting 19, 22: -
41: 58
11 : - 18 : -
Dr Somaraju Suvvari 81
NITP -- CS3401
B-Trees
Deletion
• We can perform deletion by finding the key to be deleted and removing it.
• If this key was only one of the two keys in a node, then its removal leaves only one key.
• If the sibling has only two keys, we combine the two nodes into a single node with three keys.
• The parent of this node now looses a child, so we might have percolate this strategy all the way to
the top.
• If the root looses its second child, then the root is also deleted and the tree becomes one level
shallower.
• As we combine the nodes, we must remember to update the information kept at the internal nodes.
Dr Somaraju Suvvari
82
NITP -- CS3401
B-Trees
• The depth of the B-Tree is at most ceil(logceil(M/2) N).
• At each node on the path we perform O(log M) work to determine which branch to take,
but Insert or Delete could require O(M) work to fix up all the information at the node.
• The worst case running time for each of the insertion or deletion operation is thus
O(M logM N)
Dr Somaraju Suvvari
83
NITP -- CS3401
Thank You
Dr Somaraju Suvvari
84
NITP -- CS3401