0% found this document useful (0 votes)
1 views183 pages

Unit III - Data Structures - CSBS

The document provides an introduction to trees, defining them as non-linear data structures with a root node and subtrees. It covers various types of trees, including binary trees and binary search trees, along with their properties, traversal methods, and operations such as insertion and deletion. Additionally, it discusses the applications of binary search trees and provides examples of their implementation and operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views183 pages

Unit III - Data Structures - CSBS

The document provides an introduction to trees, defining them as non-linear data structures with a root node and subtrees. It covers various types of trees, including binary trees and binary search trees, along with their properties, traversal methods, and operations such as insertion and deletion. Additionally, it discusses the applications of binary search trees and provides examples of their implementation and operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 183

Introduction to Trees

• For large amounts of input, the linear access time of linked


lists is prohibitive.
• A non-linear list is a list in which each element can have
more than one successor.
• Non linear lists are divided into 2 categories : trees and
graphs
Non-
Linear
Lists

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.

Recursive Definition of Tree

A Tree is a set of nodes that either


1. Is empty or
2. Has a designated node, called the root from which hierarchically descend zero or
more subtrees which are also trees.
Terminology
• Leaf node – any node with an outdegree zero is called leaf node
• Internal node – node that is neither leaf node nor root is called an Internal Node
• Parent Node – Node that has successor nodes i.e if it has an outdegree greater than zero
• Siblings – Two or more nodes with the same parent are called siblings
• Ancestor – An Ancestor is any node in the path from root to node.
• Descendent - A descendent is any node in the path below the parent node.
• Path – A Path is a sequence of nodes in which each node is adjacent to the next one. Every node in the tree can be
reached by following a unique path starting from root.
• The height or depth of a tree is defined to be the max level+1 of any node in the tree.
 The number of branches associated with a node is called a degree
 When branch is directed towards the node, it is called indegree, when branch is directed away from node it is out degree.
 If the out degree is zero then it is known as leaf node.
 If the in degree is zero then it is known as root node
 The sum of the indegree and outdegree branches is the degree of the node.
 The degree of the tree is the max degree of the nodes in the tree.
Example
LEVEL

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 Height : Minimum Height of a binary tree is

Hmin = (log2 N)+1

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

B C B C Nearly Complete Trees


B C
at Level 2

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

1. Visit the root


2. Traverse the left subtree in preorder
3. Traverse the right subtree in preorder
InOrder Traversal

1. Traverse the left subtree in preorder.


2. Visit the root
3. Traverse the right subtree in preorder
PostOrder Traversal

1. Traverse the left subtree in preorder.


2. Traverse the right subtree in preorder
3. Visit the root
Examples
• Inorder Traversal : 4, 2, 1, 7, 5, 8, 3, 6
• Pre Order Traversal : 1, 2, 4, 3, 5, 7, 8, 6

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

101 5 1024 200 25 2016


6 8
1008 1010 1014 2000 2002 2006

NULL 2 NULL NULL 8 NULL NUL 20 NULL NULL 40 NULL


L
1016 1018 1024 1026 2008 2010 2016 2018
1022 1030 2014 2022
Applications of Binary Search Trees
• BST used in Unix kernels for managing a set of virtual
memory areas (VMAs)
• Calculating expression trees
• Expressing arithmetic expressions
• For dynamic sorting
• IP address indexing
• For Implementing Data Compression Algorithms like
Huffman Coding
Operations on Binary Search Tree
4 Basic Operations on a Binary Search Tree
• Traversal
• Inorder
• PreOrder
• PostOrder
• Search
• Insertion
• Deletion
Search Operation in BST
• Step 1 - Read the search element from the user.
• Step 2 - Compare the search element with the value of root node in the tree.
• Step 3 - If both are matched, then display "Given node is found!!!" and terminate the
function
• Step 4 - If both are not matched, then check whether search element is smaller or
larger than that node value.
• Step 5 - If search element is smaller, then continue the search process in left
subtree.
• Step 6- If search element is larger, then continue the search process in right subtree.
• Step 7 - Repeat the same until we find the exact element or until the search element
is compared with the leaf node
• Step 8 - If we reach to the node having the value equal to the search value then
display "Element is found" and terminate the function.
• Step 9 - If we reach to the leaf node and if it is also not matched with the search
element, then display "Element is not found" and terminate the function.
• Time Complexity – O(log n)
Insertion Operation in BST
• Step 1 - Create a newNode with given value and set
its left and right to NULL.
• Step 2 - Check whether tree is Empty.
• Step 3 - If the tree is Empty, then set root to newNode.
• Step 4 - If the tree is Not Empty, then check whether the value of
newNode is smaller or larger than the node (here it is root node).
• Step 5 - If newNode is smaller than or equal to the node then move to
its left child. If newNode is larger than the node then move to
its right child.
• Step 6- Repeat the above steps until we reach to the leaf node (i.e.,
reaches to NULL).
• Step 7 - After reaching the leaf node, insert the newNode as left child if
the newNode is smaller or equal to that leaf node or else insert it
as right child.
• Time Complexity – O(log n)
Deletion Operation in BST – 3
Cases
• Case 1: Deleting a leaf node

Step 1 - Find the node to be deleted using search operation


Step 2 - Delete the node using free function (If it is a leaf) and
terminate the function.
Deletion Operation in BST – 3
Cases – Cotd..
• Case 2: Deleting a node with one child

Step 1 - Find the node to be deleted using search


operation
Step 2 - If it has only one child then create a link
between its parent node and child node.
Step 3 - Delete the node using free function and
terminate the function.
Deletion Operation in BST – 3
Cases – Cotd..
Case 3: Deleting a node with two children
Step 1 - Find the node to be deleted using search operation
Step 2 - If it has two children, then find the largest node in its left
subtree (OR) the smallest node in its right subtree.
Step 3 - Swap both deleting node and node which is found in the
above step.
Step 4 - Then check whether deleting node came to case 1 or case
2 or else goto step 2
Step 5 - If it comes to case 1, then delete using case 1 logic.
Step 6- If it comes to case 2, then delete using case 2 logic.
Step 7 - Repeat the same process until the node is deleted from the
tree.
Example – Insertion into Binary
Search Tree
Sequence of Numbers to be inserted:

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

Now Remove Node 7


Here(Already Node
7 replicated in place of Node
to be Deleted i.e 5)
After Successful Deletion of Node 5 Tree
look like…

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){

NODE *nn = (NODE *)malloc(sizeof(NODE));


nn->data = ele;
nn->left=nn->right=NULL;
return nn;
}
Insert Node Operation
NODE* insert(NODE *root, int x){
if(root==NULL)
return create(x);
else if(x>root->data) // x is greater. Should be inserted to
right
root->right = insert(root->right, x);
else // x is smaller should be inserted to left
root->left = insert(root->left,x);
return root;
}
Find Minimum Node Function..
NODE* find_minimum(NODE *root)
{
if(root == NULL)
return NULL;
else if(root->left != NULL) // node with minimum value will
have no left child
return find_minimum(root->left); // left most element will
be minimum
return root;
}
Delete Function Implementation of
BST
NODE* delete(NODE *root, int x)
{
//searching for the item to be deleted
if(root==NULL)
return NULL;
if (x>root->data)
root->right = delete(root->right, x);
else if(x<root->data)
root->left = delete(root->left, x);
else
{
//No Children
if(root->left==NULL && root->right==NULL)
{
free(root);
return NULL;
}
Delete Function ..Cotd..
//One Child
else if(root->left==NULL || root->right==NULL)
{
NODE *temp;
if(root->left==NULL)
temp = root->right;
else
temp = root->left;
free(root);
return temp;
}

}
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

• AVL trees are normally represented using the linked


representation
• To facilitate insertion and deletion, a balance factor (bf) is
associated with each node
• The balance factor bf(x) of a node x is defined as
height(xleftChild) – height(xrightChild)
• Balance factor of each node in an AVL tree must be –1, 0, or
1
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
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

• 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(9) -1 10

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

• 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(1) -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

• 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
LL Imbalance
Insert(1) -1 10 LL Rotation(Right Rotatin)
A
2 7 1 40
B
1 3 0 8 1 30 45 -1

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

• Where is 9 going to be inserted into? RL Imbalance


• After the insertion, is the tree still an AVL search tree?
(i.e., still balanced?)
Imbalance Types
• After an insertion, when the balance factor of node A
is –2 or 2, the node A is one of the following four
imbalance types
1. LL: new node is in the left subtree of the left subtree
of A
2. LR: new node is in the right subtree of the left
subtree of A
3. RR: new node is in the right subtree of the right
subtree of A
4. RL: new node is in the left subtree of the right
subtree of A
Rotation
Definition
• To switch children and parents among two or
three adjacent nodes to restore balance of a
tree.
• A rotation may change the depth of some
nodes, but does not change their relative
ordering.
Left Rotation
Definition
• In a binary search tree, pushing a node A down and
to the left to balance the tree.
• A's right child replaces A, and the right child's left
child becomes A's right child.

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 (b) shows the tree after an RR rotation at vertex B


• Figure (c) shows the result of performing an LL rotation at vertex A of the tree of Figure (b).
• The resulting tree is the same as that shown in Figure (c).
An LL Rotation

Figure An LL Rotation
An LL Rotation

Figure An LL Rotation

1 100 1 100 1 100


1 A2 0 0
50 0200 50 200 25 0
1 200
0 B25 75 0 1
25 0750150 0 0 150 3000 10 0 50 0 0
300 150 300
1 0 0
0 0 10 040 0
10 40 5 40 75
0
5
LR Rotation
An LR 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

• Figure (b) shows the tree after an RR rotation at vertex B


• Figure (c) shows the result of performing an LL rotation at vertex A of the tree of Figure (b).
• The resulting tree is the same as that shown in Figure (c).
RR Rotation

-2 A B
-1 A 1
B B
ALh 0
ALh -1
A 0
RR Rotation BRh

BLh BRh BL h BR` h+1 ALh BLh

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

• After the RR rotation, is the resulting tree an AVL search tree?


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

1)Insert “JAN” 2)Insert “FEB” 3)Insert “MAR”


JAN 0 1 JAN 0 JAN

0
0 FEB 0 FEB MAR

4)Insert “APR” 5)Insert “MAY” 6)Insert “JUN”


1 0
JAN 0 JAN
JAN
1 0 1 1
-1 0
FEB MAR FEB MAR
FEB MAR
0 0 0 0 0 0
APR MAY APR MAY
APR JUN
6)Insert “JUL” 6)Insert “AUG” 6)Insert “AUG”
LR Rotation
-1 -1 -1 JAN
JAN
JAN
1 1 2 0
FEB MAR
1 1
FEB MAR AUG MAR
0 -1 0 0 1 0
0 1 JUN 0
APR MAY APR JUN MAY APR FEB JUN MAY

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 3: //updation of balance factor of PQ which is the parent of Q


if Q is on the left side of PQ decrease balance factor of PQ by 1. If Q is on the right side of PQ
increase the balance factor of PQ by 1. Then go to step 4 , 5 or 6 based on updated balance
factor of PQ.
Step 4: if updated balance factor of PQ is zero means that the height of PQ is reduced by one so

update the balance factors of its ancestors and then terminate.

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

Figure : An R0 rotation (single rotation)


A=2 B=1 R1 Rotation
An R1 Rotation

Figure .An R1 rotation (single rotation)


An R-1 Rotation

Figure An R-1 rotation (double rotation)


An R-1 Rotation A=2 B=-1 R-1
Rotation

Figure An R-1 rotation (double rotation)


An L0 Rota- A=-2 B=0 L0 Rotation
tion
-2 A B
-1 A 1
B B
ALh 0
Alh-1 0
A -1
RR Rotation BRh

BLh BRh BL h BR h Alh-1 BLh

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

Before delete After delete


-2 10 A Apply B
L0 Rotation 40
0 7 0B 40 1
-1
45
A 10 -1
03 -1 60
08 130 45 -1 430 0
0 7 130 65
025 35 43 60-1
0 0 0 0
0 0 3 025 035
20 27 65 0 8
0 0
20 27
An L-1 Rota- A=-2 B=-1 L-1
tion Rotation

-2 A B
-1 A 0
B B
ALh -1
Alh-1 -1
A 0
RR Rotation BRh

BLh-1 BRh BL h-1 BRh Alh-1 BLh-1


Delete 5
-1 10 A
-2 10 B
17 40 -1
07 40 -1
-1 3 08 130 45 -1
30 08 130 45 -1
05 025 27 43 60-1
0 0 025 27 43 60-1
0 65 0 0
0 65
-2 10 A B
07 40 -1

30 08 130 45 -1 Apply L-1 Rotation

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

A=-2 B=1 L1 Rotation


c
0
A B

Alh-1 CL
CR
BR h-1
A=-2 B=0 R0 Rotation
Red Black
Trees
Red-Black Trees

A Red-Black tree is a self-balancing


binary search tree in which each node
contains an extra bit for denoting the
color of the node, either red or black.
(0 for black, 1 for Red)
Self-Balancing Binary Search Trees are height-
balanced binary search trees that automatically keeps height as
small as possible when insertion and deletion operations are
performed on tree.
Why 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

• AVL trees are more balanced compared to Red-Black Trees,


but they may cause more rotations during insertion and
deletion.
• So, if our application involves many frequent insertions and
deletions, then Red-Black trees should be preferred.
• And if the insertions and deletions are less frequent and search
is a more frequent operation, then AVL tree should be
preferred over Red-Black Tree.
Red-Black Trees
A Red-Black tree is a self-balancing binary search tree in which each node
contains an extra bit for denoting the color of the node, either red or black.
(0 for black, 1 for Red)

A Red-black tree Properties


 Red/Black Property: Every node is colored, either red or black.

 Root Property: The root is black.

 Leaf Property: Every leaf (NIL Node) is black.

 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 insertion operation in Red Black Tree is similar to


insertion operation in Binary Search Tree. But every
node is inserted with a color attached to it.
• After every insertion operation, we need to check all the properties
of Red-Black Tree. If all the properties are satisfied then we go to
next operation otherwise we perform any of the following
operations to make it Red Black Tree.
1.Recolor
2.Rotation
3.Rotation followed by Recolor
The insertion operation in Red Black tree is performed using the following
steps...
Step 1 - Check whether tree is Empty.
Step 2 - If tree is Empty then insert the newNode as Root node with
color
Black and exit from the operation.
Step 3 - If tree is not Empty then insert the newNode at the
appropriate position and fill with Red color
Step 4 - If the parent of newNode is Black then exit from
the operation.
Step 5 - If the parent of newNode is Red then check the
color of parent node's sibling of newNode.
a)If it is colored Red then Recolor. Repeat the same
until tree becomes Red Black Tree.
b)If it is colored Black or NULL then make suitable
Rotation and Recolor it.
Balancing RED-Black Trees during Insertions

• 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)

• Insertion cases can be broadly classified in to two cases:


• Case a): Colour of Un node is Red. This case has 4 subcasses depending
on positions of u,p, and g. No Rotations are required in this case, Only
Recoloring has to be done.
• LLr, LRr, RRr, RLr

• 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.

Removal of LRr imbalance can be done by:


• Change color of p from red to black.
• Change color of Un from red to black.
• Change color of g from black to red, provided g is not a root node.
But then, recheck for continuous red colored nodes up
• the tree till its root node.
Note: If given g is root node then there will be no changes in color of g.
a manner that p and u have red
color at left and left positions
with respect to grandparent g
respectively. Therefore it is
termed as Left Left imbalance.

Removal of LLr imbalance can be done by:


i.Change color of p from red to black.
ii.Change color of Un from red to black.
iii.Change color of g from black to red, provided g is not a root node. But then, recheck for continuous red colored nodes
up the tree till its root node.
Note: If given g is root node then there will be no changes in color of g.
in such a manner that p and u
have red color at right and
right positions with respect to
grandparent g respectively.
Therefore it is termed as Right
Right imbalance.

Removal of RRr imbalance can be done by:


Change color of p from red to black.
Change color of Un from red to black.
Change color of g from black to red, provided g is not a root node. But then, recheck for continuous red colored nodes
up the tree till its root node.
Note: If given g is root node then there will be no changes in color of g.
a manner that p and u have red
color at right and left positions
with respect to grandparent g
respectively. Therefore it is
termed as Right Left imbalance.

Removal of RLr imbalance can be done by:


Change color of p from red to black.
Change color of Un from red to black.
Change color of g from black to red, provided g is not a root node. But then, recheck for continuous red colored nodes
up the tree till its root node.
Note: If given g is root node then there will be no changes in color of g.
In the remaining four cases, color of node Un is black or Un is not present.

5) LL imbalance: It can be balanced by following two steps:


• u is the newly inserted
• Apply single rotation of p around g. (LL Rotation of AVL Tree) node.
• Recolor p to black and g to red. • 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)
6) RR imbalance: It can be balanced by following two steps:
• Apply single rotation of p around g. (RR Rotation of AVL Tree)
• Recolor p to black and g to red.
7) LR imbalance can be balanced by following steps:
• Apply double rotation of u around p followed by u around g. (LR Rotation of AVL Tree)
• Recolor u to black and g to red.
8) RL imbalance can be balanced by following steps:
• Apply double rotation of u around p followed by u around g. (RL Rotation of AVL Tree)
• Recolor u to black and g to red.
Insert into Red-Black
8,18,5,15,17,25,40,80
Insert( Insert(1 Insert(
8) 8) 5)
8 8 8

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

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 1
Insert(57) Insert(57)
0 Recolor 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.

An example of a Red Black Tree that is not an AVL Tree


Extended
Binary Tree
Extended binary tree is a type of binary tree in which
all the null sub tree of the original tree are replaced
with special nodes called external nodes whereas
other nodes are called internal nodes
Extended Binary
Tree
3. Extended Binary
Tree
B Trees
Definition
• A B tree is a specialized m-way tree data structure. A B-
Tree of order ‘m’ can have atmost m-1 keys and m
children.
Properties of B Trees
• Property #1 - All the leaf nodes must be at the same
level.
• Property #2 - All nodes except root must have at least
[m/2]-1 keys and maximum of m-1 keys.
• Property #3 - All non leaf nodes except root (i.e. all
internal nodes) must have at least m/2 children.
• Property #4 - If the root node is a non leaf node, then
it must have at least 2 children.
• Property #5 - All the key values within a node must be
in Ascending Order.
Example
• In a B-tree of order 3 , internal nodes have either 2 or 3
children. So , a B-tree of order 3 is also known as 2-3
tree. Example for 2-3 B Tree is shown below
B Tree of Order 4
Operations on B-tree:

• The following operations are performed on a B-Tree...


1.Search
2.Insertion
3.Deletion
Algorithm for Insertion Operation in
B Tree
In a B-Tree, the new element must be added only at leaf node. That means, always
the new keyValue is attached to leaf node only. The insertion operation is performed
as follows...
• Step 1: Check whether tree is Empty.
• Step 2: If tree is Empty, then create a new node with new key value and insert into
the tree as a root node.
• Step 3: If tree is Not Empty, then find a leaf node to which the new key value cab
be added using Binary Search Tree logic.
• Step 4: If that leaf node has an empty position, then add the new key value to that
leaf node by maintaining ascending order of key value within the node.
• Step 5: If that leaf node is already full, then split that leaf node by sending middle
value to its parent node. Repeat the same until sent value is fixed into a node.
• Step 6: If the splitting is occurring to the root node, then the middle value
becomes new root node for the tree and the height of the tree is increased by one.
Example
• Construct a B tree of order 3 for the following sequence
of keys:
10, 20, 5,6,12
10
Insert 10 :
10 20

Insert 20 :
Insert 5 : × 5 10 20

Since the order is 3 , can have atmost 2 keys. So split the


node and move the middle element as the parent.

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)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy