Unit III - Data Structures - CSBS
Unit III - Data Structures - CSBS
Trees Graphs
2-way Multiway
Trees Trees
Definition of Tree
• A tree is a finite set of one or more elements such that there is a specially designated
node called the root. The remaining nodes are partitioned into n > = 0 disjoint sets
T1……Tn where each of these sets is a tree T1……Tn are called the subtrees of the
root.
• A tree consists of a finite set of elements called nodes and a finite set of directed lines
called branches that connect the nodes.
A 0
• Referring to Figure,
• Root – A
B C D • Parents – A,B,C,D,E,H
1
• Children –
B,C,D,E,F,G,H,I,J,K,L,M
E F
G H I J • Siblings – {B,C,D}, {E,F}, {K,L},
2
{H,I,J}
• Leaves – K,L,F,G,M,I,J
K
L
M 3 • Internal Nodes – B,C,D,E,H
Binary Trees
• A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the
left and right subtrees
A A A
B B
A A A A
B C B C B B
D E C
C
First Tree is a null tree and rest all are binary tree examples…
Properties of Binary Trees
The height of the binary tree can be mathematically predicted
Maximum Height : Given that we need to store ‘N’ nodes in a binary tree the maximum height h max ,
Hmax = N
Minimum Nodes : Given the height of the binary tree H, minimum number of nodes in the tree is H
Nmin = H
Maximum Nodes
Nmax = 2H-1
The balance factor for a binary tree is the difference of height between the left and the right sub trees.
B = Hl-Hr
Where Hl is the height of the left sub tree and Hr is the height of the right sub tree. In a balanced binary tree height of
sub trees differ by no more than one.
Complete Trees and Nearly Complete Trees
• A Complete Tree has the maximum number of entries for its
height.
• Maximum number is reached when last level is full.
• A tree is said to be nearly complete when if it has
maximum height for its nodes and all nodes in the last level
A
are found
A on the Aleft. Complete Trees at Levels 0,1
B C and 2
B C
D E F G
A A A
D D E D E F
Binary Tree Traversals
• A binary tree traversal requires processing of
every node once and only once in a
predetermined sequence.
• 2 approaches
• Depth First Traversal
• PreOrder
• InOrder
• PostOrder
• Breadth First Traversal – Each level is completed
first horizontally.
PreOrder Traversal
2 3
4 5 6
7 8
Inorder : D B E A F C
Pre Order: A B D E C F
A
B C
D E F
InOrder: 40 20 50 10 60 30
Pre Order: 10 20 40 50 30 60
1
0
2 3
0 0
4 5 6
0 0 0
Pre Order: 1 2 4 5 3 6
InOrder : 4 2 5 1 6 3
1
2 3
4 5 6
PreOrder : 1 2 4 5 7 3 6 8
InOrder: 4 2 7 5 1 8 6 3
1
2 3
5 6
4
8
7
Inorder: 4 2 1 7 5 8 3 6
Pre Order: 1 2 4 3 5 7 8 6
1
2 3
4 5 6
7 8
Binary Search Trees
- A peek into its operations and Implementation
What are Binary Search Trees??
• Binary Trees with a Special Property
• Also called ordered trees or sorted binary trees
• Aid in Fast Lookup, addition and Removal of Items
• Fast Lookup – Example – Finding Phone Number based
on Name
Understanding Binary Search Tree
Nod
e
with
value K
Left Right
Subtre Subtre
e e
Contains
Contains
only
only Larger
smaller
values
values
All values <= K All values > K
Properties of Binary Search Trees
• Node Binary Tree Data Structure
• The left sub tree of a node contains only nodes with
keys less than the node’s key
• The right sub tree of a node contains only nodes with
keys more than the node’s key.
• The left and the right sub tree each must also be a
binary search tree.
• Ideally there must be no duplicate nodes
Example
8
3 10
1
1 6
4
1
4 7
3
Addres Value
s
1000 1008
1002 10 100 10 200
8 0
1006 2000
1008 1016
1010 5 101 5 102 200 25 201
6 4 8 6
1014 1024
1016 NULL
1018 2 NUL 2 NUL NUL 8 NUL NUL 20 NUL NUL 40
L L L L L L L
1022 NULL
1024 NULL
1026 8
1030 NULL
2000 2008
2002 25
2006 2016
2008 NULL
2010 20
1008 10 2000
1000 1002 1006
10,12,5,4,20,8,7,15 and 13
Construction….
1 1
5
0 2
2
4 8
0
1 1
7
5 3
Deletion of a Node in BST
• Case 1: Target Node has no Children
Lets Say..Node to be removed is 13
10
10
55 12
12
8
4
20
15
7
13
After Deletion of Node 13 Tree looks
like this..
10
5 12
8
4
20
15
7
Case 2: Consider Deletion of Node
20
10
5 12
8
4
20
7 15
After Deletion of Node 20 Tree looks
like..
10
10
5
5 12
8
8
4
4 15
7
Case 3: Target Node has 2 children
• Lets Say we Need to Delete Node 5
10
5 12
Minimum
Node in
8 Right Sub
4 15
Tree is 7
7 Find Minimum of
Right Sub Tree
10
7 12
8
4 15
10
7 12
8
4 15
Let Us Now Implement Binary Search
Tree in C Using RECURSION
Structure Declaration for BST
typedef struct Node {
int data;
struct Node *left, *right;
}NODE;
Allocate Memory for the Node
NODE* create(int ele){
}
Delete Function..Cotd..
//Two Children
NODE *temp = find_minimum(root->right);
root->data = temp->data;
root->right = delete(root->right, temp->data);
}
return root;
Search Function..
NODE* search(NODE *root, int x)
{
if(root==NULL || root->data==x) //if root->data is x then the
element is found
return root;
else if(x>root->data) // x is greater, so we will search the right
subtree
return search(root->right, x);
else //x is smaller than the data, so we will search the left subtree
return search(root->left,x);
}
Traversals- Inorder
void inorder(NODE *root){
if(root!=NULL) // checking if the root is not null
{
inorder(root->left); // visiting left child
printf(" %d ", root->data); // printing data at root
inorder(root->right);// visiting right child
}
}
Traversals - PreOrder
void preorder(NODE *root){
if(root != NULL) {
printf(" %d ", root->data);
preorder(root->left);
preorder(root->right);
}
}
Traversals - PostOrder
void postorder(NODE *root){
if(root != NULL){
postorder(root->left);
postorder(root->right);
printf(" %d ", root->data);
}
}
AVL Trees
Height Balanced Binary Search Trees
• If the height of a binary tree is always O(log
n), we can guarantee O(log n) performance
for each search tree operation
• Trees with a worst-case height of O(log n) are
called balanced trees
• An example of a height balanced tree is AVL
(Adelson-Velsky and Landis) tree
AVL Tree
Definition
• Binary tree.
• If T is a nonempty binary tree with TL and
TR as its left and right subtrees, then T is an
AVL tree iff
1. TL and TR are AVL trees, and
2. |hL – hR| 1 where hL and hR are the heights of
TL and TR, respectively
AVL Tree with Balance Factors
10 -1
1 7 1
40
0 3 1 30 -1
8 45
1 5 -1 20 35 0 60
0
0
0
0 25
1,0,-1
Balance factor=left subtree height-right subtree right
AVL Tree with Balance Factors
-1 10
1 7 1 40
0 3 0 8 1 30 45 -1
0
0 1 0 5 -1 20 0 35 60
0
25
Construct BST for 1,2,3,4,5,6,7,8
1
2
3
4
5
6
8
Construct AVL for 1,2,3,4,5,6,7,8
4
2 6
3 5 7
1
8
Construct BST for 1,2,3,2,1,4,5,6,7,8
3
2 4
1 5
6
7
8
Construct BST for 3,2,1,5,4,6,7,8
3
2 5
1 6
4
7
8
Properties of AVL Tree
1. The height of an AVL tree with n nodes is O(log n)
2. For every value of n, n 0, there exists an AVL tree
3. An n-node AVL search tree can be searched in
O(height) = O(log n) time
4. A new node can be inserted into an n-node AVL
search tree so that the result is an n+1 node AVL
tree and insertion can be done in O(log n) time
5. A node can be deleted from an n-node AVL search
tree, n>0, so that the result is an n-1 node AVL tree
and deletion can be done in O(log n) time
Balance Factor
1 7 1 40
0 3 0 8 1 30 45 -1
0
0 1 0 5 -1 20 0 35 60
0
25
Inserting into an AVL Search Trees
• If we use the strategy of Program 14.5 to insert an
element into an AVL search tree, the result may not
be an AVL tree
• That is, the tree may become imbalanced
• If the tree becomes unbalanced, we must adjust the
tree to restore balance - this adjustment is called
rotation
Insertion Operation in AVL Tree
In an AVL tree, the insertion operation is performed with O(log n) time complexity. In AVL Tree,
new node is always inserted as a leaf node. The insertion operation is performed as follows...
Step 1:Find the place to insert the new element by following path from the
root ,then insert the new node and set the balance factor of new node as
zero(0).During this process , keep track of most recently visited node with
Say “A”
with balance factor as either 1 or -1.
Step 2:If there is no node “A” ,then make another pass from the root ,
updating balance factors .Then terminate, Otherwise go to step 3
Step 3: If bf( A) =1 and the new node was inserted on the right side of “A”
or if bf(A)=-1 and the new node was inserted on the left side of “A” ,then
set the bf(A)=0 and terminate. Otherwise go to step 4.
Step 4: classify the imbalance at “A” and perform the appropriate rotations
.Change balance factors as required by the rotations.
Inserting into an AVL Search Tree
Insert(9) -1 10
1 7 1 40
0 3 0 8 1 30 45 -1
0 2 0 5 0
-1 20 0 35 60
0
25
0 7 1 40
0 3 8 -1 1 30 45 -1
0
0 2 0 5 0
9 -1 20 0 35 60
0
25
1 7 1 40
0 3 0 8 1 30 45 -1
0 2 0 5 0
-1 20 0 35 60
0
25
1 2 0 5 0
-1 20 0 35 60
0
1 25
0
LR Imbalance
• Where is 9 going to be inserted into?
• After the insertion, is the tree still an AVL search tree?
(i.e., still balanced?)
Inserting into an AVL Search Tree
Insert(27) -1 10
1 7 1 40
0 3 0 8 1 30 45 -1
A 0
0 2 0 5 -1 20 0 35 60
15
0 25 B
Inserting into an AVL Search Tree
RR Imbalance
Insert(27) -1 10 RR rotation(Left Rotation)
1 7 1 40
0 3 0 8 1 30 45 -1
A 0
0 2 0 5 -2 20 0 35 60
B -1
25
27 0
9 A 15
Left Rotation
4 15 9 22
12 22 4 12
Right Rotation
Definition
• In a binary search tree, pushing a node A down and
to the right to balance the tree.
• A's left child replaces A, and the left child's right
child becomes A's left child.
A 15 9
Right Rotation
9 22 4 15
4 12 12 22
Single and Double Rotations
• Single rotations: the transformations done to correct LL and RR imbalances
• Double rotations: the transformations done to correct LR and RL imbalances
• The transformation to correct LR imbalance can be achieved by an RR rotation followed by an LL
rotation as shown below
LR Rotation
Figure An LL Rotation
An LL Rotation
Figure An LL Rotation
1 1
100
1100 100
A 2
A 2 0 0 1
1 50 200 -1
50 200 100
-1 0
50 0
200 0 0 0
0 B 25
075 150 3000 40 075 150 3000 40
0 -1 200 0
25 075015030000 1 25 50
0
10 40 25 150 300
B 0
0 0 0
10 40 10 35 75 0
35 0 0
10 35
1
0
An LR Rotation
Figure An LR Rotation
1 100 1 1
100
100
1 A2 0 0 0
50 0200 50 200 40
- 200
0 B 0 0 -1 0
1 1 075 150 3000 25 50 0
25 0750150 300
0 40 150 300
0 0 0
0 25 35 035
0 0 10 75
10 40
10
An LR Rotation
Figure An LR Rotation
1 100 1
1 100 100
1 A2 0 0 0
50 0200 50 200 40 200
0 - 0 -1 0
B25 75 0 0
25 0750150 0 1 0 150 3000 25 50
150
300 300
0 0
0 0 10 140 0 035
75
10 40 10
0 35
LR Rotation
-2 A B
-1 A 1
B B
ALh 0
ALh -1
A 0
RR Rotation BRh
RR imbalance
Inserting into an AVL Search Tree
-2 A B
-1 A 1
B B
ALh 0
ALh -1
A 0
RR Rotation BRh
BRh BL h BR` h+1 ALh BLh
BLh
Insert(27) -110
17 140
0 3 08 130 45 -1
0 1 05 0
-120 035 60
0
25
Inserting into an AVL Search Tree
-1 10
1 7 1 40
0 3 0 8 1 30 45 -1
0 1 0 5 0
20 0 35 60
-2
25 -1
27 0
After RR Rotation
-1 10
1 7 1 40
0 3 0 8 1 30 45 -1
0 1 0 5 0
0 25 0 35 60
0 0
20 27
•
RL Imbalance
C
-1 A -2 A -2 A
0
AL h B A
0
B AL h 1 C B
AL h
C B
BL h BR h BR h CL AL h CL CR BR h
CL CR CR
BR h
C
-1 A -2 A -2 A
A 0
B
0
B ALh 1 C B
ALh
ALh C B
BLh BRh BRh CL CL CR BRh
CL CR
CR BRh
Insert 15
0
5 -1 5
-1 -1 -1
7 2 7
2
-1 -2
1 1 4 6 0 9
1 1 4 6 0 9
0 0
16 0 3 0 16 1
3 0
0
15
0
0
-1 5
-1 -1 5
7 -1
2 7
2
A 0
1 1 4 6 0 9 1 1 4 6 0 C
C 15
0
0 0 B
3 0 15 A
B 3 0 9 16 0
16
A=2 B=1 LL Imbalance –LL rotation( Right
Insert following elements into an rotation)
AVL 3,2,1,4,5,6,7,16,15 A=2 B= -11 LR Imbalance –LR
rotation( Double rotation
Step 1:insert(3) A= -22 B= -11 RR Imbalance –RR
rotation( Left rotation
0 3
A= -2 B= 1 RL Imbalance –RL
Step 2:insert(2) rotation( Double rotation
A
1 3
2
0
Step 3:insert(1)
A
2
3
0 B
1 LL imblanace . 2
B
2 So Apply LL rotation
0 0 A
1 0 3
1
Insert following elements into an A=2 B=1 LL Imbalance –LL rotation( Right
rotation
AVL 3,2,1,4,5,6,7,16,15
A=2 B= -11 LR Imbalance –LR
rotation( Double rotation
Step 3:insert(4)
A= -2 B= -1 RR Imbalance –RR rotation( Left
-1 rotation
2
A= -2 B= 1 RL Imbalance –RL
0 rotation( Double rotation
1 -1 3
0 4
Step 4:insert(5)
-1 -1
2 2
0 A
0
1 -2 3 0 4 B
1
B RR Imbalace.so
A 0
-1 4 apply RR rotation 3 0 5
5
0
Insert following elements into an A=2 B=1 LL Imbalance –LL rotation( Right
rotation
AVL 3,2,1,4,5,6,7,16,15
A=2 B= -1 LR Imbalance –LR rotation( Double
rotation
Step :insert(6)
A A= -2 B= -1 RR Imbalance –RR rotation( Left
-2 rotation B
2 0
A= -2 B= 1 RL Imbalance
4 –RL
B -1 A 0
0 RR Imbalace.so
rotation( Double rotation -1
1 4 2
apply RR rotation 5
-1 0 6
3 0 5 1 3 0
0
0 6
Step :insert(7)
0
0 4
4 0
0 0
-2 A RR Imbalace.so 2
2 B
5 apply RR rotation 6
B A
0
0 6 1 0 3 0 5 7
0
1 3 0
-1
0
7
A=2 B=1 LL Imbalance –LL rotation( Right
rotation
A=2 B= -11 LR Imbalance –LR
rotation( Double rotation
A= -2 B= -11 RR Imbalance –RR rotation( Left
Insert 16 rotation 4
-1
0
A= -2 B= 1 RL 2Imbalance-1–RL 0
-1
4 rotation( Double rotation
A 6
0 A 1
-1
2 0
1 0 3 0 5 7 -2
6
B
0
0 3 7 -1
C 15
1 0 5
.so at i on
e
c rot -1
l a 16
0
4
ba ight
16
Insert 15 Im R
0
-1
L
R ply 2
-1
0
4
ap A 6
C
-1 0
2 1 0 3 0 5 15 0
A 6 B
A
0 A 16
1 0 3 0 5 7 -2 0 7 0
B
C 16 1
0
15
Insert following elements into an 20,10,5,30,40,3,4,25,23,27,56
A=2 B=1 LL Imbalance –LL rotation( Right
rotation)
Step 1:insert(20)
A=2 B= -11 LR Imbalance –LR rotation( Double
0 20 rotation)
A= -2 B= -11 RR Imbalance –RR rotation( Left
Step 2:insert(10)
rotation)
1 20
A= -2 B= 1 RL Imbalance –RL rotation( Double
0 rotation)
10
Step 3:insert(5)
A
2 20
1 B
10 0 B
LL Imbalace.so 10
0 apply LL rotation A
5 0 0
5 20
Insert following elements into an 20,10,5,30,40,3,4,25,23,27,56
Step 4:insert(30)
A=2 B=1 LL Imbalance –LL rotation( Right
-1 rotation)
10 A=2 B= -11 LR Imbalance –LR rotation( Double
0 rotation)
5 20 -1
A= -2 B= -11 RR Imbalance –RR rotation( Left
0 rotation)
30
A= -2 B= 1 RL Imbalance –RL rotation( Double
Step 5:insert(40) rotation)
-1
-1
10
A 10
0 B
-2
5 20
RR Imbalace.so 0 5 30 0
-1B apply RR rotation
30 0
0 A 20 0 40
40
Insert following elements into an 20,10,5,30,40,3,4,25,23,27,56
Step 6:insert(3)
A=2 B=1 LL Imbalance –LL rotation( Right
0 rotation)
10 A=2 B= -1 LR Imbalance –LR rotation( Double
rotation)
1 30 0
5 A= -2 B= -11 RR Imbalance –RR rotation( Left
rotation)
0 20 0 40 0
3 A= -2 B= 1 RL Imbalance –RL rotation( Double
rotation)
Step 7:insert(4)
0
0
10 0
10
2 30 0 10
A 5 LR Imbalace.so -2 0
30 c
apply LR rotation(first A 5 0 0
-1 0 4 30
20 0 40 0L rotation then R rotation)
c
3 A
B 20 0 40
4 0 0
c 4 B 3 B 5 20 40
0 0
3B 0
Insert following elements into an 20,10,5,30,40,3,4,25,23,27,56
A=2 B=1 LL Imbalance –LL rotation( Right
rotation)
Step 7:insert(25)
A=2 B= -11 LR Imbalance –LR rotation( Double
-1 rotation)
10 A= -2 B= -1 RR Imbalance –RR rotation( Left -1
0 30 1rotation) 10
4
A= -2 B= 1 RL Imbalance –RL rotation( Double
rotation) 0 4 1
3 5 20 -1 0 40 30
0 0 c 0
25 0 3 5 23 0 40
Step 7:insert(23) 0 0
A 25
B
-1 -1 20
0 0
10 10
0 30 1 0 1
4 4 30
RL Imbalace.so
5 20 -2 0 40 apply RL rotation(first
20 -2 0 40
3 3
R rotation then L rotation) 5
0 A B A
0 0 0 B
25 1
c c 23
0 23
25
Insert following elements into an 20,10,5,30,40,3,4,25,23,27,56
Insertion of 27 -1 -1
-1 10 0 c
10
A 10 0 25
0 30 2 A 4
4
0 2 B 0
4 30 1
B 3 5
23 30 A
3 5 23-1 0 40
0
25 -1 40 0
0 0 c 3 5
c 40
-1 0 27
25
0 0
20
20
23 27 0 0
0 B Then R rotatin
27
0
20
LR Imbalance. Apply L rotation
Insertion of 56
B0
A --2 25
-1
A
10 -1B 0 30
10
0 25 -1
4 0 1 40
-1 4 23 27
1 RR imbalance. 0
3 5
23 30 Apply RR rotation 0
56
0 3 5 20
0 -1 0
40 0 0
20 27
0 0 56 0
Example for insertion into an AVL
Example for insertion into an AVL
0
0 FEB 0 FEB MAR
0 0 0
JUL AUG JUL 0 JUL
LR imbalance
7)Insert “OCT”
6)Insert “SEP”
-1 JAN
0 0
AUG MAR
0 1 -1 0
0
APR FEB JUN MAY
0 0
JUL SEP
RL imbalance
RL rotation Insert NOV
-1 JAN
-2 JAN
0 0 0 -1
AUG MAR AUG MAR
1 0 1 1
0 0
APR FEB JUN OCT APR FEB JUN OCT
0 0 -1 0
0 0
JUL MAY SEP JUL MAY SEP
0
NOV
RR Imbalance
RR Rotation
0
MAR
1
0 JAN OCT
0 1 -1
0
AUG JUN
MAY SEP
0 0 0
APR FEB JUL 0 NOV
RR Imbalance
Insert “DEC”
1
MAR
1
1 JAN OCT
-1 1 -1 0
AUG JUN
MAY SEP
0 1
APR FEB JUL 0 NOV
0 DEC
Deletion from an AVL
Deletion of 3
1 10 q 0 10 q
1 7 0 40 0 7 0 40
0
3
q q
Deletion of 30 0 10
-1 10
0 7 0 40
0 7 1 40
0 q
q 30
0 10 Deletion of 7 -1 10
0 7 0 40 0 40
Deletion from an AVL Search Tree:
Algorithm Deletion from AVL
Step 1:Search for the node say Q to be deleted. Let PQ is the parent of Q
Step 2:Delete Q by using similar procedure whichever is used for deletion of node from binary
search tree , then go to step 3.
Step 5: if updated balance factor of PQ is 1 or -1 means that the height of PQ is not changed, so
no need of changing balance factors of its ancestors. Simply terminate.
Step 6: if updated balance factor of PQ is 2 or -2 means the tree is imbalanced at PQ. Possible
imbalances are R0 , R1, R-1 , L0,L-1 and L1. So perform appropriate rotation to make the tree
as balanced and then terminate. In case of R1 , R-1 ,L1 and L-1 imbalances, after performing
concerned rotations, ancestors balance factors also to be updated.
Imbalances in the AVL deletion:
After deletion of a node Q from AVl tree , balance factor changes may propagate up the tree
along the path from Q to the root .During this process ,it is possible for the balance factor of a
node on this path to become -2 or 2. Let A be the first such node on this path. To restore balance
at node A, we classify the type of imbalance.
L imbalance occurs , if the deletion takes place from the left subtree of A.
R imbalance occurs , if the deletion takes place from the right subtree of A.
R imbalance is further classified into three types such as R0, R1 and R-1 imbalances depends on
balance factor of B, where B is left child of A.
The Ro refers to the case when the deletion took place from the right subtree of A and bf(B)=0 .
The R1 refers to the case when the deletion took place from the right subtree of A and bf(B)=1.
The R-1 refers to the case when the deletion took place from the right subtree of A and bf(B)=-1.
L imbalance is further classified into three types such as L0, L1 and L-1 imbalances depends on
balance factor of B, where B is the right child of A.
The Lo refers to the case when the deletion took place from the left subtree of A and bf(B)=0 .
The L1 refers to the case when the deletion took place from the left subtree of A and bf(B)=1.
The L-1 refers to the case when the deletion took place from the left subtree of A and bf(B)=-1.
A=2 B=1 LL Imbalance –LL rotation( Right
rotation
A=2 B= -11 LR Imbalance –LR
rotation( Double rotation
A= -2 B= -11 RR Imbalance –RR rotation( Left
rotation 1 10
0 10 Deletion of 40
A= -2 B= 1 RL Imbalance –RL
rotation( Double rotation
0 7 0 40 0 7
A
-1 10 Deletion of 7 -2 10
B
0 7 1 40 1 40
35 35
0
0
A=2 B=1 LL Imbalance –LL rotation( Right
rotation
A=2 B= -11 LR Imbalance –LR
rotation( Double rotation
A= -2 B= -11 RR Imbalance –RR
A rotation( Left
rotation
Deletion 2 10
1 10 A= -2 B= 1 RLof 40
Imbalance –RL
rotation( Double rotation
-1 7 0 40 7 -1
0 9 0
9
Deletion from an AVL Search Tree
• Deletion of a node may also produce an imbalance
• Imbalance incurred by deletion is classified into
the types R0, R1, R-1, L0, L1, and L-1
• Rotation is also needed for rebalancing
• Read the observations after deleting a node from an
AVL search tree
A=-2 B=0 R0 Rotation
An R0 Rotation
Delete 5
A
-1 10 -2 10
B
0 40 0 7 0 40
17
-1 3 03 08 130 45 -1
08 130 45 -1
-1 -1
05 025 035 43 60 025 035 43 60
0 0
0 0 650 0 0 650
20 27 20 27
-2 A B
-1 A 0
B B
ALh -1
Alh-1 -1
A 0
RR Rotation BRh
025 27 43 60-1
0 0
0 65
B0
40
A 10 -1
0 45
0 -1
07 30 430 60
30 0 8 25 27 65
0 0 0
An L1 Rota-
tion
-2 A -2 A
-1 A
B c
B Alh-1 Alh-1
ALh -1
1 B
1
c
CL
c BRh-1 BR h-1
CR
BR h-1
CL CR
CL CR
Alh-1 CL
CR
BR h-1
A=-2 B=0 R0 Rotation
Red Black
Trees
Red-Black Trees
• Most of the BST operations (e.g., search, insert, delete.. etc) take
O(h) time where h is the height of the BST.
• The cost of these operations may become O(n) for a skewed
Binary tree, where n is the number of nodes. If we make sure
that height of the tree remains O(Logn) after every insertion
and deletion, then we can guarantee time complexity of
O(Logn) for all these operations.
• The height of a Red-Black tree is always O(Logn) where n is the
number of nodes in the tree.
Comparison with AVL Tree
Red Property or Color Property: If a red node has children then, the
children are always black nodes. (There cannot be two consecutive Red
nodes on a path)
Depth Property: For each node, any simple path from this node to any of
its descendant leaves has the same number of black nodes (also known
as black-depth ).
EXAMPLE OF A RED-BLACK TREE
Example
Following is a Red-Black Tree which is created by inserting numbers from 1 to
9.
Insertion into RED BLACK
Tree
• The following notation is used to name the keys while defining cases of
balancing
• u is the newly inserted node.
• p is the parent node of u.
• g is the grandparent node of u.
• Un is the uncle node of u. (Child of g, sibling of p)
• Case b): Colour of Un node is Black or there is no Un node. This case has
4 subcasses depending on positions of u,p, and g. Rotations are first
done and then, recoloring has to be done if necessary.
• LL, LR, RR, RL
1) LRr imbalance : In this Red Black Tree violates its property in
such a manner that p and u have red color at left and right
positions with respect to grandparent g respectively. Therefore it
is termed as Left Right imbalance. r in the case name represents
red color for node Un.
18 5 18
Insert(15)
8
Recolour 8
1 parent
5 8 and 1
parents 5
8
1 sibling
5 1
5
Insert into Red-Black
8,18,5,15,17,25,40,80
Insert 2, 1, 4, 5, 9, 3, 6, 7
Insert 3 Insert 6
2 2
2 RECOLO
R 5 5
1 1
5
1 9
4 9
4
9
4
3 3 6
3
Insert 7
2 2
LR ROTATION
2
5 RECOLO 5
5 1 R 1
1 7
7 4
9 4
4
9 3 6 9
3 6
3 6
7
Construct Red-Black Binary Search Tree for the
following elements:20, 10, 5, 30, 40, 57, 3,
2,4, 35, Insert(10,
Insert(20)
25, 18 ,22 ,21
5)
2 2 LL 1 Recolo 1
0 0 Rotation 0 r 0
1 2 2
5 5 0
0 0
Insert(30)
1 1
0 Recol
or 0
2
5 2
0 5 0
3 3
0 0
Insert(40)
RR Recolor
1 Rotation 1
0 0 1
0
2
5 0 5 3
0 5 3
3 0
0 2 4
0 0 2 4
4
0 0
0
5 3 5 3
0 0
2 4 2 4
0 0 0 0
5 5
7 7
Construct Red-Black Binary Search Tree for the following
elements:20, 10, 5, 30, 40, 57, 3, 2,4, 35, 25, 18 ,22 ,21
Insert(3) 1 1
0 0
5 3 3 3
0 0
3 2 4 2 5 2 4
0 0 0 0
5 5
Insert(2) 7
7 or
l
1
LL 1 co
Rotation 0 Re
0
3 3 3
5
0 0
3 2 4 2 5 2 4
0 0 0 0
5 5
2 7 7
Construct Red-Black Binary Search Tree for the following
elements:20, 10, 5, 30, 40, 57, 3, 2,4, 35, 25, 18 ,22 ,21
1
0
3 3
0
2 5 2 4
0 0
5
Insert(4) 7
1
1 Recolor 0
0
3
3 3 0
3
0
2 5 2 4
2 5 2 4 0 0
0 0
5
5 4 7
4 7
Construct Red-Black Binary Search Tree for the following
elements:20, 10, 5, 30, 40, 57, 3, 2,4, 35, 25, 18 ,22 ,21
Insert(35) 1
0
3
3 0
2 5 2 4
0 0
5
4 3
7
5
Insert(25) Insert(18)
1 1
0 0
3 3
3 0 3 0
2 2
2 5 4 2 4
0 5 0
0 0
2 5 5
4 3 1 2 3
5 7 4 7
5 8 5 5
Construct Red-Black Binary Search Tree for the following
elements:20, 10, 5, 30, 40, 57, 3, 2,4, 35, 25, 18 ,22 ,21
1
0
Insert(22)
Recolor 3
1 3
0 0
3 2
2 5 4
3 0 0
0
1 5
2 4 2 3
2 5 4 8 7
0 5 5
0
1
1 3 5 0 2
4 2
8 5 7 3 2
5
3 0
2
2 4 Recolor
2 2
5 0
0
1 2 3 5
4 8 5 7
5
2
2
Construct Red-Black Binary Search Tree for the following
elements:20, 10, 5, 30, 40, 57, 3, 2,4, 35, 25, 18 ,22 ,21
Insert(21)
1 1
LL Rotation
0 0
3 3
3 0 3 0
4 4
2 2 2
2 5 0 5 0
0 0
1 5 1 2 5
2 3 3
4 8 4 8 2 7
5 5 7 5
1
2
2 0 2
1
2 3 5
3 0
2
1
4 Recolor
2 2
5 0
0
1 2 5
3
4 8 2 7
5
2 2
1 5
Relation between Red-Black Trees and
AVL Trees
All AVL Trees are Red Black Trees. But not all Red Black Trees are AVL Trees.
Insert 20 :
Insert 5 : × 5 10 20
10
5 20
• Insert 6:
10
5 6 20
Insert 12:
10
5 6 12 20
Example
• Insert the following keys into B Tree of order 4.
• 5,3,21,9,13,22,7,10,11,14,8,16
Solution
9
5 13 16
3 7 8 10 11 14 21 22
Example
• Construct B Tree of order 3 for the following sequence:
7,8,9,10,11,16,21,18
Solution
10
8 16
7 9 11 18 21
B Tree Deletion - Algorithm
• When performing deletion operation on a B Tree. There are 3 main
cases for deletion.
Case 1:
The key to be deleted lies in the leaf. There are two cases for it.
1. The deletion of the key does not violate the property of the
minimum no of keys a node should hold.
2. The deletion of the key violates the property of the minimum no of
keys a node should hold. In this case, we borrow a key from its
immediate neighbouring sibling node in the order of left to right.
First, visit the immediate left sibling. If the left sibling node has more
than a minimum number of keys, then borrow a key from this node.
Else, check to borrow from the immediate right sibling node.
if both the immediate sibling nodes already have a minimum no
of keys, then merge the node either with the left sibling node or the
right sibling node. This merging is done through the parent node.
Deletion – cotd,.
Case 2:
If the key to be deleted lies in the internal node, the following
cases occur.
1. The internal node, which is deleted, is replaced by an inorder
predecessor if the left child has more than the minimum no of
keys.
2. The internal node, which is deleted, is replaced by an inorder
successor if the right child has more than the minimum no of
keys.
3. If either child has exactly a minimum no of keys then, merge
the left and right children.
After merging, if the parent node has less than the minimum no
of keys then, look for the siblings in case 1.
Deletion – cotd..
Case 3:
In this case, the height of the tree shrink. If the target key lies in
an internal node, then the deletion of the key leads to a fewer
number of keys in the node(i.e less than the minimum required),
then look for the inorder predecessor and the inorder successor.
If both children contain a minimum no of keys, then borrowing
cannot take place. This leads to case 2(3). i.e. merging the
children.
Again look for the sibling to borrowa key. But if the sibling node
also has minimum no of keys then merge, the node with the
sibling node along with the parent.
Arrange the children accordingly ( increasing order)
Deletion Complexity
• Best Case : O(log n)
• Average Case : O(n)
• Worst Case : O(n)