0% found this document useful (0 votes)
52 views103 pages

Trees One

The document provides an overview of non-linear data structures, specifically focusing on trees, including definitions, properties, and types of binary trees. It covers basic terminologies, traversal methods, and memory representation techniques for binary trees. Additionally, it discusses the construction of general trees to binary trees and includes examples of tree traversal algorithms.

Uploaded by

Rudraksh Mall
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views103 pages

Trees One

The document provides an overview of non-linear data structures, specifically focusing on trees, including definitions, properties, and types of binary trees. It covers basic terminologies, traversal methods, and memory representation techniques for binary trees. Additionally, it discusses the construction of general trees to binary trees and includes examples of tree traversal algorithms.

Uploaded by

Rudraksh Mall
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 103

Data Structures

Tree

Dr Deepak Gupta
Assistant Professor, SMIEEE
CSED, MNNIT Allahabad, Prayagraj
Email: [email protected]
Non-linear Data Structures
A data structure is said to be non-linear if its elements form a hierarchical
relationship where data items appear at various levels.
Trees and Graphs are widely used non-linear data structures. Tree and
graph structures represent hierarchical relationships between individual
data elements.
Trees can be defined recursively as:
A tree is a finite set of nodes such that:
1. There is a distinguished node called root node
2. The remaining nodes are partitioned into n>=0 disjoint sets T1, T2 ,…,
Tn where each of these sets is a tree. The sets T1, T2,…,Tn are the
subtrees of the root.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
Basic Terminologies in Tree Data Structure:
Parent Node: The node which is a predecessor of a node is called the parent node of that
node. {B} is the parent node of {D, E}.
Child Node: The node that is the immediate successor of a node is called the child node of
that node. Examples: {D, E} are the child nodes of {B}.
Root Node: The topmost node of a tree or the node that does not have any parent node is
called the root node. {A} is the root node of the tree. A non-empty tree must contain
exactly one root node and exactly one path from the root to all other nodes of the tree.
Leaf Node or External Node: The nodes that do not have any child nodes are called leaf
nodes. {K, L, M, N, O, P, G} are the leaf nodes of the tree.
Ancestor of a Node: Any predecessor nodes on the path of the root to that node are called
Ancestors of that node. {A,B} are the ancestor nodes of the node {E}
Descendant: A node x is a descendant of another node y if and only if y is an ancestor of y.
Sibling: Children of the same parent node are called siblings. {D,E} are called siblings.
Level of a node: The count of edges on the path from the root node to that node. The root
node has level 0.
Internal node: A node with at least one child is called an Internal Node.
Neighbour of a Node: Parent or child nodes of that node are called neighbors of that node.
Subtree: Any node of the tree along with its descendant.
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
N-ary Tree Data Structure

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Binary Tree
A binary tree is a finite set of nodes that is:
1. Either empty or
2. Consists of a distinguished node called root and the remaining nodes
are partitioned into two disjoint sets T1 (called left subtree) and T2
(called right subtree) and both of them are binary trees.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Property 1: The maximum number of nodes on any level i is 2i where i ≥0.

Property 2: The maximum number of nodes possible in a binary tree of


height h is 2h-1.

Proof:
If we sum up the maximum number of nodes possible on each level then we
can get the maximum number of nodes possible in the binary tree. First level
is 0 and last level is h-1. By using property 1, the total number of nodes
possible in a binary tree of height h is given by:
h −1
n =  2i
i =0

n = 1 + 21 + 22 + ... + 2h −1
2( h −1) +1 − 1
n=
2 −1
n = 2h − 1
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
Property 3: The minimum number of nodes possible in a binary tree of
height h is equal to h.

Property 4: If a binary tree contains n nodes, then its maximum height


possible is n and the minimum height possible is log 2 (n + 1)  .
Proof: There should be at least one element on each level, so the height
cannot be more than n. A binary tree of height h can have a maximum 2h-1
nodes (from property 2). So the number of nodes will be less than or equal to
this maximum value.
n  2h − 1
2h  n + 1
h  log 2 ( n + 1)
h  log 2 (n + 1)  (h is an integer)
So the minimum height possible is log 2 (n + 1)  .

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Property 5: In a non-empty binary tree, if n is the total number of nodes and
e is the total number of edges then e=n-1.

Property 6: For any non-empty binary tree, if n0 is the number of nodes


with no child and n2 is the number of nodes with 2 children, then n0 = n2+1.

Total Number of binary tree is generated by using n nodes (without


label nodes) : = 2nCn/(n+1)

Total Number of binary tree is generated by using n nodes (labelled


nodes) : = 2nCn/(n+1)*n!

Total Number of binary tree of maximum height generated by using n


nodes: = 2n-1

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Types of Binary Tree
1. Strictly Binary Tree
A Binary Tree is a strictly binary tree if each node in the tree is either a leaf
node or has exactly two children i.e. there is no node with one child.

Property 7: A strictly binary tree with a non-leaf nodes has n+1 leaf nodes.
Property 8: A strictly binary tree with n leaf nodes always has 2n-1 nodes.
Remark: Number of Leaf nodes = Number of Internal nodes + 1

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


2. Extended Binary Tree
If in a binary tree, each empty subtree (NULL link) is replaced by a special
node then the resulting tree is an extended binary tree or 2-tree.
So we can convert a binary tree to an extended binary tree by adding special
nodes to leaf nodes and nodes that have only one child.
The special nodes added to the tree are called external nodes and the
original nodes of the tree are internal nodes.

Internal nodes

External nodes

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


The path length for any node is the number of edges traversed from that
node to the root node.
The path length of a tree is the sum of the path lengths of all the nodes of
the tree.
The internal path length of a binary tree is the
sum of the path lengths of all internal nodes
which is equal to the sum of levels of all internal
nodes.
Internal path length (I): 0+1+1+2+2+3 = 9
The external path length of a binary tree is the
sum of the path lengths of all external nodes
which is equal to the sum of levels of all
external nodes.
External path length (E): 2+2+3+3+3+4+4 = 21

Property 9: In an extended binary tree, if E is the external path length, I is the


internal path length and n is the number of internal nodes then E = I+2n.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


3. Full Binary Tree
A binary tree is a full binary tree if all the levels have a maximum number
of nodes, i.e. if the height of the tree is h, then it will have 2h-1 nodes.

Full Binary Tree

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


4. Complete Binary Tree
A complete binary tree is a binary
tree in which every level, except
possibly the last, has to be filled and
all nodes are as far left as possible.

Complete Binary Tree

Property 10: If the height of a complete binary tree is h, h ≥1, then the
minimum number of nodes possible is 2h-1 and the maximum number of nodes
possible is 2h -1.
Proof: The number of nodes will be maximum when the last level also
contains maximum nodes i.e. all levels are full, and so total nodes will be 2h -1
from property 2.
The number of nodes will be minimal when the last level has only one node. In
this case the total nodes will be
Total nodes in a full binary tree of height (h-1) + one node
i.e. (2h-1 -1)+1 = 2h-1.
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
5. Balanced Binary Tree
A balanced binary tree is a binary tree
in which the height of the left and the
right sub-trees of every node may
differ by at most 1.
Remark: AVL Tree and Red-Black Tree are
well-known data structures.

6. Degenerate Binary Tree


Degenerate Binary Tree is a Binary
Tree where every parent node has
only one child node.
Remark: The height of a Degenerate Binary
Tree is equal to the total number of nodes in
that tree.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Representation of Binary Trees in Memory
There are two common methods used for representing a binary tree
1. Sequential representation using Array
2. Linked representation using Linked List
1. Sequential representation using Array
A one-dimensional array can be used
to represent a binary tree in the
memory. Here, we consider that the
root is present at index 0. Assume
tree[N] is an array representing a
binary tree. If ‘i’ be index, then

1. The root node is stored at index 0, i.e. root is at tree[0].


2. Left child is stored at tree[2*i+1].
3. Right child is stored at tree[2*i+2].  (i − 1) 
4. Its parent will be at index floor((i-1)/2) i.e.  2  .
Drawbacks of Array Representation: While representing a binary tree in the form of an
array, most of the memory blocks in the middle will be left blank or unused if the tree is
not a complete binary tree.
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
2. Linked representation using Linked List
A binary tree can be represented by using a linked list, where each node is
divided into 3 parts:
• Info: it is used to store the information
• Left: It is a pointer that holds the address of the left child
• Right: It is a pointer that holds the address of the right child

The structure for tree node can be declared as


struct node{
char data;
struct node *lchild;
struct node *rchild;
};

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Construction of General Tree to Binary Tree
It is convenient to represent a general tree to binary tree in a program,
because in the case of a general tree, the number of edges connected from a
node at any given time is unpredictable.
Therefore the node space has to be managed in such a way that each node is
allowed a variable number of sub-tree pointers.

The procedure is:


• Insert edges connecting siblings from left to right at the same level.
• Delete all edges of a parent to its children except to its left most
offspring.
• Rotate the resultant tree 45o to mark clearly the left and right sub-trees.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
Traversal of Binary Tree

The process of visiting each node of the tree exactly once is called tree
traversal. The traversal of the binary tree involves three basic activities such
as:
1. Visiting the root
2. Traverse the left sub-tree
3. Traverse the right sub-tree
These three basic activities are done in a different order which follows:
1. Pre-order Traversal (NLR)
a. Visit the root (N)
b. Traverse the left subtree of root in
preorder(L)
c. Traverse the right subtree of root in
preorder(R)

The Pre-order traversal is: ABDECFG

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


2. In-order Traversal (LNR)
a. Traverse the left subtree of root in
preorder(L)
b. Visit the root (N)
c. Traverse the right subtree of root in
preorder(R)

The In-order traversal is: DBEAFCG

3. Post-order Traversal (LRN)


a. Traverse the left subtree of root in
preorder(L)
b. Traverse the right subtree of root in
preorder(R)
c. Visit the root (N)

The In-order traversal is: DEBFGCA

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


The above tree traversal methods can be implemented in two ways:
1. Recursive tree traversal
2. Non-Recursive tree traversal
1. Recursive tree traversal
void preorder(struct node * ptr) void inorder(struct node * ptr)
{ {
if(ptr = = NULL) if(ptr = = NULL)
return; return;
printf(“%d”, ptr->info); inorder(ptr->lchild);
preorder(ptr->lchild); printf(“%d”, ptr->info);
preorder(ptr->rchild); inorder(ptr->rchild);
} }

void postorder(struct node * ptr)


{
if(ptr = = NULL)
return;
postorder(ptr->lchild);
postorder(ptr->rchild);
printf(“%d”, ptr->info);
}
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
2. Non-Recursive tree traversal
void nrec_preorder(struct node *root)
Pre-order Traversal {
1. Push the root node on the stack struct node *ptr = root;
2. Pop a node from the stack if(ptr = = NULL)
3. Visit the popped node {
4. Push the right child of the visited printf(“Tree is empty”);
node on the stack return;
5. Push the left child of the visited }
node on the stack push_stack(ptr);
6. Repeat steps 2,3,4,5 till the stack while(!stack_empty())
is not empty. {
ptr = pop_stack();
printf(“%d”, ptr->info);
if(ptr->rchild!=NULL)
push_stack(ptr->rchild);
if(ptr->lchild!=NULL)
push_stack(ptr->lchild);
}
printf(“\n”);
}

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


If we apply the algorithm to the tree, the steps would be:
• Push 50
• Pop 50: Visit 50
Push right and left child of 50: Push 60. Push 40
• Pop 40: Visit 40
40 has no right child so push its left child: Push 30
• Pop 30: Visit 30
Push right and left child of 30: Push 35. Push 25
• Pop 25: Visit 25
25 has no right child so push its left child: Push 20
• Pop 20: Visit 20
20 has no child so nothing is pushed
• Pop 35: Visit 35
Push right and left child of 35: Push 36. Push 33
• Pop 33: Visit 33
33 has no child so nothing is pushed
• Pop 36: Visit 36
36 has no child so nothing is pushed
• Pop 60: Visit 60
60 has no left child so push its right child: Push 70
• Pop 70: Visit 70
Push right and left child of 70: Push 80. Push 65 Hence pre-order traversal is:
• Pop 65: Visit 65
65 has no child so nothing is pushed
50 40 30 25 20 35 33 36 60 70 65 80
• Pop 80: Visit 80
80 has no child so nothing is pushed Stack is empty
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
In-order Traversal void nrec_inorder(struct node *root)
1. Initially ptr is assigned the address {
of the root node. struct node *ptr = root;
2. Move along the leftmost path if(ptr = = NULL)
{
rooted at the node pointed by ptr,
printf(“Tree is empty”);
pushing all the nodes in the path return;
on the stack. Stop when we reach }
the leftmost node i.e. a node that while(1)
has no left child, it is not pushed {
on the stack. Now ptr points to this while(ptr->lchild!=NULL)
leftmost node. {
3. If the node pointed by ptr has no push_stack(ptr);
ptr = ptr->lchild;
right subtree, then visit it and pop
}
another one from the stack. Now while(ptr->rchild==NULL)
keep on popping and visiting the {
nodes till anode is popped that has printf(“%d”, ptr->info);
a right subtree. Now ptr points to if(stack_empty())
this node that has a right subtree. return;
If the stack becomes empty then ptr = pop_stack();
the traversal is finished. }
printf(“%d”, ptr->info);
4. Visit the node pointed by ptr, and
ptr = ptr->rchild;
now ptr is assigned the address of }
its right child. printf(“\n”);
5. Go to step 2 }
Post-order Traversal void nrec_postorder(struct node *root)
1. Initially ptr is assigned the address of {
struct node *q, *ptr = root;
the root node.
if(ptr = = NULL)
2. Move along the leftmost path rooted at {
the node ptr pushing all the nodes in printf(“Tree is empty”);
the path on the stack. Stop when we return;
reach the leftmost node i.e. a node that }
has no left child, it is not pushed on the q = root;
stack. Now ptr points to this leftmost while(1)
node. {
while(ptr->lchild!=NULL)
3. If ptr has no roght subtree or ots right
{
subtree has been traversed then visit push_stack(ptr);
the node and pop another one from the ptr = ptr->lchild;
stack. Now keep on popping and }
visiting the nodes till a node is popped while(ptr->rchild==NULL || ptr->rchild ==q)
that has a right subtree which has not {
been traversed. Now ptr points to this printf(“%d”, ptr->info);
node that has an untraversed right q = ptr;
if(stack_empty())
subtree. If the stack becomes empty
return;
then the traversal is finished. ptr = pop_stack();
4. Push the node pointed by ptr, and now }
ptr is assigned the address of its right push_stack(ptr);
child. ptr = ptr->rchild;
5. Go to step 2 }
Level-order Traversal
1. Insert the root node in the queue
2. Delete a node from the front of the queue and visit it
3. Insert the left child of the visited node in the queue at the end
4. Insert the right child of the visited node in the queue at the end
5. Repeat steps 2,3,4 till the queue is not empty.

The level-order traversal is ABCDEFG.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Creation of Binary Tree from In-order and Pre-order traversals

The Pre-order traversal is: A B D H E C F I G J K


The In-order traversal is: D H B E A I F C J G K
In pre-order traversal, the first node is the root node.
Hence, A is the root of the binary tree.
From In-order traversal, we see that nodes to the left of
root node A are nodes D, H, B, E so these nodes form the
left subtree of A, similarly nodes I, F, C, J, G, K form the
right subtree of A.

D H B E I F C J G K
Pre-order : B D H E Pre-order : C F I G J K
In-order : D H B E In-order : I F C J G K

left subtree of A right subtree of A


Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
A

B C

D H E I F J G K
Pre-order : D H Pre-order : F I Pre-order : G J K
In-order : D H In-order : I F In-order : J G K
left subtree of B left subtree of C right subtree of C

B C

D E F G

H I J K
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
Creation of Binary Tree from In-order and Post-order traversals

The Post-order traversal is: H D E B I F J K G C A


The In-order traversal is: D H B E A I F C J G K
In post-order traversal, the last node is the root node.
Hence, A is the root of the binary tree.
From In-order traversal, we see that nodes to the left of
root node A are nodes D, H, B, E so these nodes form the
left subtree of A, similarly nodes I, F, C, J, G, K form the
right subtree of A.

D H B E I F C J G K
Post-order : H D E B Post-order : I F J K G C
In-order : D H B E In-order : I F C J G K

left subtree of A right subtree of A


Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
A

B C

D H E I F J G K
Post-order : H D Post-order : I F Post-order : J K G
In-order : D H In-order : I F In-order : J G K
left subtree of B left subtree of C right subtree of C

B C

D E F G

H I J K
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
Creation of Binary Tree from Pre-order and Post-order traversals

The Pre-order traversal is: A B D E H I C F G


The Post-order traversal is: D H I E B F G C A
In first node of pre-order and last node of post-order
traversal i.e. A is considered as root of the binary tree.
Find the node succussing the root node in pre-order, say x1
and the node preceding the root node in post-order say x2.
• If x1 = x2, then it can be taken as left or right child of root and which may not result
unique tree.
• If x1 != x2, then x1 is taken as left child i.e. B and x2 is taken as right child i.e. C of
the root node.
Find the position of x2, i.e. ‘C’ in pre-order and position of x1, i.e. ‘B’ in the post order.
• Consider two sets of pre-order and post-order traversal of left and right sub-tree of root.
• The first set consists of nodes that are present after x1 and before x2 in pre-order
traversal i.e. DEHI and the nodes present before x1 in post-order i.e. DHIE.
• The second set consists of nodes that are present after x2 in pre-order traversal i.e. FG
and the nodes present after x1 and before x2 in post-order traversal i.e. FG.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


A

B = x1 B C C = x2

D H E I G F
Pre-order : D E H I Pre-order : F G
Post-order : D H I E Post-order : F G

Find the node succussing the root node in pre-order, say x1 (i.e. D) and the node
preceding the root node in post-order say x2 (i.e. E).
• If x1 != x2, then x1 is taken as left child i.e. D and x2 is taken as right child i.e. E of
the root node.
Find the position of x2, i.e. ‘E’ in pre-order and position of x1, i.e. ‘D’ in the post order.
• Consider two sets of pre-order and post-order traversal of left and right sub-tree of root.
• The first set consists of nodes that are present after x1 and before x2 in pre-order
traversal i.e. NULL and the nodes present before x1 in post-order i.e. NULL.
• The second set consists of nodes that are present after x2 in pre-order traversal i.e. HI
and the nodes present after x1 and before x2 in post-order traversal i.e. HI.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


A

B C
G F
D = x1 D E E = x2 Pre-order : F G
Post-order : F G
H I
Pre-order : HI
Post-order : HI

Follow the same procedure to get the final binary tree.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Height of Binary Tree
int height(struct node *ptr)
{
int h_left, h_right;
if(ptr = = NULL)
return 0;
h_left = height(ptr->lchild);
h_right = height(ptr->rchild);
if(h_left > h_right)
return 1+h_left ;
else
return 1+h_right;
}

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Expression Tree
It is a binary tree, which stores an arithmetic expression. The leaf nodes of the
expression tree are always operands and all other internal nodes are the operators,
including the root.

arithmetic expression: 4 / ( 2 - ( - 8 * 3 ) )

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Construction of Expression Tree:

First of all, we will do scanning of the given expression into left to the right
manner, then one by one check the identified character:
1. If a scanned character is an operand, we will apply the push operation and push it
into the stack.
2. If a scanned character is an operator, we will apply the pop operation into it to
remove the two values from the stack to make them its child, and after then we will
push back the current parent node into the stack.

postfix notation is: a b + c d e + * *

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
Binary Search Tree

A binary search tree is a binary tree that may be


empty and if it is not empty then it satisfies the
following properties:
1. All the keys in the left subtree of root are less
than the key in the root
2. All the keys in the right subtree of root are greater
than the key in the root
3. Left and right subtrees of root are also binary
search trees.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Traversal in Binary Search Tree

The different traversals for the binary search


tree are:
Pre-order: 30, 20, 10, 15, 25, 23, 39, 35, 42

In-order: 10 , 15 , 20 , 23 , 25 , 30 , 35 , 39 , 42

Post-order: 15 , 10 , 23 , 25, 20, 35, 42, 39, 30

Level order: 30, 20, 39, 10, 25, 35, 42, 15, 23

Remark: Note that the in-order traversal of a binary search tree gives us all keys of
that tree in ascending order.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Searching in a Binary Search Tree

1. Compare the element with the root of the tree.


2. If the item is matched then return the location of the node.
3. Otherwise check if item is less than the element present on root, if so then
move to the left sub-tree.
4. If not, then move to the right sub-tree.
5. Repeat this procedure recursively until match found.
6. If element is not found then return NULL.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
Non-recursive code for searching a key in Binary Search Tree

struct node *search_nrec(struct node *ptr, int skey)


{
while(ptr!=NULL)
{
if(skey < ptr->info)
ptr = ptr->lchild; /* move to left child*/
else if(skey > ptr->info)
ptr = ptr->rchild; /* move to right child*/
else /*skey found*/
return ptr;
}
return NULL;
}

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Recursive code for searching a key in Binary Search Tree
struct node *search_rec(struct node *ptr, int skey)
{
if(ptr==NULL)
{
printf(“key not found\n”);
return NULL;
}
else if(skey < ptr->info) /* search in left subtree*/
return search_rec(ptr->lchild, skey);
else if(skey > ptr->info) /* search in right subtree*/
return seach_rec(ptr->rchild, skey);
else /*skey found*/
return ptr;
}

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Insertion in a Binary Search Tree

1. We start at the root and move down the tree.


2. If the key to be inserted is equal to the key in the node then there is nothing
to be done as duplicate keys are not allowed.
3. If the key to be inserted is less than the key of the node then we move to
left child.
4. If the key to be inserted is greater than the key of the node then we move
to right child.
5.We insert the new key when we reach a NULL left or right child.
6. END

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India

Example of creating a binary search tree


Now, let's see the creation of binary search tree using an example.

Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12, 20, 50

Step 1 - Insert 45.

Step 2 - Insert 15.


As 15 is smaller than 45, so insert it as the root node of the left subtree.

Step 3 - Insert 79.


As 79 is greater than 45, so insert it as the root node of the right subtree.
Step 4 - Insert 90.
90 is greater than 45 and 79, so it will be inserted as the right subtree of 79.

Step 5 - Insert 10.


10 is smaller than 45 and 15, so it will be inserted as a left subtree of 15.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Step 6 - Insert 55.
55 is larger than 45 and smaller than 79, so it will be inserted as the left subtree of 79.

Step 7 - Insert 12.


12 is smaller than 45 and 15 but greater than 10, so it will be inserted as the right subtree of 10.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Step 8 - Insert 20.
20 is smaller than 45 but greater than 15, so it will be inserted as the right subtree of 15.

Step 9 - Insert 50.


50 is greater than 45 but smaller than 79 and 55. So, it will be inserted as a left subtree of 55.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Struct node *insert_nrec(struct node *root, int ikey)
{
struct node *tmp, *par, *ptr; /*par: parent*/
ptr = root;
par = NULL;
while(ptr!=NULL)
{
par = ptr;
if(ikey < ptr->info)
ptr = ptr->lchild;
else if(ikey > ptr->info)
ptr = ptr->rchild;
else
{
printf(“Duplicate key”);
return root;
}
}
tmp = (struct node *)malloc(sizeof(struct node));
tmp->info = ikey;
tmp->lchild = NULL;
tmp->rchild = NULL;
if(par = = NULL)
root = tmp;
else if(ikey < par->info)
par->lchild = tmp;
else
par->rchild = tmp;
return root;
}
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
Struct node *insert_rec(struct node *ptr, int ikey)
{
if(ptr = =NULL)
{
ptr = (struct node *)malloc(sizeof(struct node));
ptr->info = ikey;
ptr->lchild = NULL;
ptr->rchild = NULL;
}
else if(ikey < ptr->info)
ptr->lchild = insert_rec(ptr->lchild, ikey);
else if(ikey > ptr->info)
ptr->rchild = insert_rec(ptr->rchild, ikey);
else
printf(“Duplicate key”);
return ptr;
}

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Deletion in a Binary Search Tree

Given a BST, the task is to delete a node in this BST, which can be broken
down into 3 cases:
Case 1. Delete a Leaf Node in BST

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Case 2. Delete a Node with Single Child in BST
Deleting a single child node is also simple in BST. Copy the child to the node and
delete the node.

Case 3. Delete a Node with Both Children in BST


Here we have to find the inorder successor of the node. The data of the inorder
successor is copied to the node and then the inorder successor is deleted from the tree.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


struct node *del_rec(struct node *ptr, int dkey)
{
struct node *tmp, *succ;
if(ptr = =NULL)
{
printf(“dkey not found”);
return(ptr);
}
if(dkey < ptr->info) /* delete from left subtree*/
ptr->lchild = del_rec(ptr->lchild, dkey);
else if(dkey > ptr->info) /* delete from right subtree*/
ptr->rchild = del_rec(ptr->rchild, dkey);
else
{ /* key to be deleted is found*/
if(ptr->child!=NULL && ptr->rchild!=NULL) /* 2 children*/
{
succ = ptr->rchild;
while(succ->lchild)
succ = succ->lchild;
ptr->info = succ->info;
ptr->rchild = del_rec(ptr->rchild, succ->info);
}
else
{
temp = ptr;
if(ptr->lchild!=NULL) /* only left child*/
ptr = ptr->lchild;
else if(ptr->rchild!=NULL) /* only right child*/
ptr = ptr->rchild;
else
ptr = NULL;
free(tmp);
}
}
return ptr;
}
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India

Threaded Binary Tree

A binary tree with n nodes has 2n pointers out of which n+1 are always
NULL, so we can see that about half the space allocated for pointers is
wasted. We can utilize this wasted to contain some useful information.
A left NULL pointer can be used to store the address of inorder predecessor
of the node and a right NULL pointer can be used to store the address of
inorder successor of the node.

These pointers are called threads and a binary tree which implements these
pointers is called a threaded binary tree.
Types of Threaded Binary Tree

Depending on the type of threading, there are two types of threaded binary
tree:
Types of Threaded Binary Tree

One-way Threading Two-way Threading

Left In-Threaded Binary Tree Right In-Threaded Binary Tree

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


If only left NULL pointers are used as
threads then the binary tree is called a
left in-threaded binary tree.

Left in-threaded binary tree

If only right NULL pointers are used


as threads then the binary tree is
called a right in-threaded binary tree.

Right in-threaded binary tree


Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
If both left and right NULL pointers are used as threads then the binary
tree is called a fully in-threaded binary tree.

The structure declaration of a


node in a fully in-threaded
binary tree will be as:

struct node
{
struct node *left;
boolean lthread;
int info;
boolean rthread;
struct node *right;
Fully in-threaded binary tree };

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


In the inorder traversal, the first node has no predecessor and last node has
no successor. So the left pointer of first node and right pointer of last node
are NULL. To make the threading consistent, we can take a dummy node
called the header node which can be predecessor of the first node and the
successor of the last node.

Fully in-threaded binary tree


Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
Finding inorder successor of a node in in-threaded tree

If the right pointer of the node is a thread, then there is no need to do


anything because the right thread points to inorder successor. If node’s
right pointer is not a thread i.e. node has a right child then to find the
inorder successor, we move to this right child and keep on moving left till
we find a node with no left child.
struct node *in_succ(struct node *ptr)
{
if(ptr->rthread = = true)
return ptr->right;
else
{
ptr = ptr->right;
while(ptr->lthread = =false)
ptr = ptr->left;
return ptr;
}
}

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Finding inorder predecessor of a node in in-threaded tree

If the left pointer of the node is a thread, then thread will point to inorder
predecessor. If the left pointer is not a thread i.e. node has a left child then
to find the inorder predecessor, we move to this left child and keep on
moving right till we find a node with no right child.

struct node *in_pred(struct node *ptr)


{
if(ptr->lthread = = true)
return ptr->left;
else
{
ptr = ptr->left;
while(ptr->rthread = =false)
ptr = ptr->right;
return ptr;
}
}

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Inorder Traversal of in threaded binary tree
struct node *inorder(struct node *root)
The first node to be visit in the {
inorder traversal is the last node struct node *ptr;
in the leftmost branch starting if(root = = NULL)
from root. So we start from the {
root and move left till we get a printf(“Tree is empty”);
return;
node with no left child, and this
}
node is visited. ptr = root;
Now with the help of in_succ() /*Find the leftmost node*/
function we find the inorder while(ptr->lthread = = false)
successor of each node and visit ptr = ptr->left;
while(ptr!=NULL)
it. The right pointer of last node
{
is NULL and we have marked it printf(“%d”, ptr->info);
as thread. So we stop our ptr = in_succ(ptr);
process when we get a node }
whose right thread is NULL. }

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


AVL Tree

The technique for balancing a binary search tree was introduced by Russian
mathematicians G.M. Adelson, Velski and E. M. Lendis in 1962.

AVL tree is a self-balancing binary search tree in which the heights of the
two sub-trees of a node may differ by at most one. Because of this property,
AVL tree is also known as a height-balanced tree.
The key advantage of using an AVL tree is that it takes O(logn) time to
perform search, insertion and deletion operations in average case as well as
worst case (because the height of the tree is limited to O(logn)).
The structure of an AVL tree is same as that of a binary search tree but with
a little difference. In its structure, it stores an additional variable called the
BalanceFactor.
The balance factor of a node is calculated by subtracting the height of its
right sub-tree from the height of its left sub-tree.
Balance factor = Height (left sub-tree) – Height (right sub-tree)
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
A binary search tree in which every node
has a balance factor of -1, 0 or 1 is said to be
height balanced. A node with any other
balance factor is considered to be
unbalanced and requires rebalancing.

bf = hl – hr = {-1, 0, 1}

If the balance factor of a node is 1, then it means that the left sub-tree of the
tree is one level higher than that of the right sub-tree. Such a tree is called
Left-heavy tree.

If the balance factor of a node is 0, then it means that the height of the left
sub-tree is equal to the height of its right sub-tree.

If the balance factor of a node is -1, then it means that the left sub-tree of
the tree is one level lower than that of the right sub-tree. Such a tree is
called Right-heavy tree.
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India

Insert a node in AVL Tree

Types of Rotations
1. LL Rotation: Inserted node is in the left subtree of left subtree of C

2. RR Rotation: Inserted node is in the right subtree of right subtree of A


3. LR Rotation: Inserted node is in the right subtree of left subtree of C

LR Rotation = RR Rotation + LL Rotation

4. RL Rotation: Inserted node is in the left subtree of right subtree of A

RL Rotation = LL Rotation + RR Rotation

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Q: Construct an AVL tree having the following elements
H, I, J, B, A, E, C, F, D, G, K, L
1. Insert H, I, J

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


2. Insert B

3. Insert A

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


4. Insert E

On inserting E, BST becomes unbalanced as the Balance Factor of I is 2, since if we travel from E
to I we find that it is inserted in the left subtree of right subtree of I, we will perform LR Rotation
on node I. LR = RR + LL rotation

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


5. Insert C, F, D

Perform LL rotation Perform RR rotation


on node E on node B

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


6. Insert G

Perform RR rotation Perform LL rotation


on node C on node H

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


7. Insert K

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


8. Insert L

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Delete a node in AVL Tree

Deleting a node from an AVL tree is similar to that in a binary search tree.
Deletion may disturb the balance factor of an AVL tree and therefore the tree
needs to be rebalanced in order to maintain the AVLness.

For this purpose, we need to perform rotations. The two types of rotations
are L rotation and R rotation. Here, we will discuss R rotations. L rotations
are the mirror images of them.
If the node which is to be deleted is present in the left sub-tree of the critical
node, then L rotation needs to be applied else if, the node which is to be
deleted is present in the right sub-tree of the critical node, the R rotation will
be applied.
Let us consider that, A is the critical node and B is the root node of its left
sub-tree. If node X, present in the right sub-tree of A, is to be deleted, then
there can be three different situations:

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


1. R0 rotation (Node B has balance factor 0 )

If the node B has 0 balance factor, and the balance factor of node A
disturbed upon deleting the node X, then the tree will be rebalanced by
rotating tree using R0 rotation.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India

Example: Delete node 30 from the AVL tree shown in the following image.
2. R1 Rotation (Node B has balance factor 1)

R1 Rotation is to be performed if the balance factor of Node B is 1. In R1


rotation, the critical node A is moved to its right having sub-trees T2 and T3
as its left and right child respectively. T1 is to be placed as the left sub-tree
of the node B.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India

Example: Delete node 55 from the AVL tree shown in the following image.
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India

3. R-1 Rotation (Node B has balance factor -1)


R-1 rotation is to be performed if the node B has balance factor -1. This case
is treated in the same way as LR rotation. In this case, the node C, which is
the right child of node B, becomes the root node of the tree with B and A as
its left and right children respectively.
The sub-trees T1, T2 becomes the left and right sub-trees of B whereas, T3,
T4 become the left and right sub-trees of A.
Example: Delete node 60 from the AVL tree shown in the following image.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India

Multiway (M-way) Search Tree

A multiway search tree of order m is a search tree in which any node can
have at the most m children.
The properties of a non empty m way search tree of order m are:
1. Each node can hold maximum m-1 keys and can have maximum m
children.
2. A node with m children has m-1 key
values. Some of the children can be
NULL (empty subtrees).
3. The keys in a node are in
ascending order.
4. Keys in a non-leaf node will
divide the left and right subtrees
where the value of the left subtree
keys will be less and the value of
the right subtree keys will be more
than that particular key.
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India

B-Tree

A B-Tree is a perfectly height-balanced M-way search tree.

A B-Tree of order m can be defined as an m-way search tree that is either


empty or satisfies the following properties:
1. All leaf nodes are at the same level.
2. All non-leaf nodes (except the root node) should have at least children.
3. All nodes (except root node) should have at least keys.
4. The root node must have at least 2 children and at least one key.
5. A non-leaf node with n-1 key values should have n non-NULL children.
Searching in B-Tree
Searching in B Trees is similar to that in Binary search tree.

For example, if we search for item 49 in the following B Tree. The process
will be something like the following :

• Compare item 49 with root node 78. since 49 < 78 hence, move to its left sub-tree.
• Since, 40<49<56, traverse right sub-tree of 40.
• 49>45, move to right. Compare 49.
• match found, return.
Remark: Searching in a B tree depends upon the height of the tree. The search algorithm takes
O(log n) time to search any element in a B tree.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Insertion in B-Tree
Insertions are done at the leaf node level. The following algorithm needs to
be followed in order to insert an item into B Tree.
1. Traverse the B Tree in order to find the appropriate leaf node at which the
node can be inserted.
2. If the leaf node contains less than m-1 keys then insert the element in the
increasing order.
3. Else, if the leaf node contains m-1 keys, then follow the following steps.

o Insert the new element in the increasing order of elements.


o Split the node into the two nodes at the median.

o Push the median element up to its parent node.


o If the parent node also contains m-1 number of keys, then split it
too by following the same steps.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Let us take some keys and create a B-tree of order 5
10, 40, 30, 35, 20, 15, 50, 28, 25, 5, 60, 19, 12, 38, 27, 90, 45, 48

1. Insert 10
10

2. Insert 40
10 40

3. Insert 30

10 30 40
4. Insert 35
10 30 35 40

5. Insert 20
30

10 20 35 40
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
6. Insert 15, 50, 28
30

10 15 20 28 35 40 50

7. Insert 25
20 30

10 15 25 28 35 40 50
8. Insert 5, 60, 19
20 30

5 10 15 19 25 28 35 40 50 60

9. Insert 12
12 20 30

5 10 15 19 25 28 35 40 50 60
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India
10. Insert 38 12 20 30 40

5 10 15 19 25 28 35 38 50 60

11. Insert 27, 90, 45


12 20 30 40

5 10 15 19 25 27 28 35 38 45 50 60 90

12. Insert 48

30

12 20 40 50

5 10 15 19 25 27 28 35 38 45 48 60 90

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Deletion in B-Tree
Deletion in a B-tree can be classified into two cases:
o Deletion from leaf node.
o Deletion from non-leaf node
• Deletion from leaf node.
CASE1: If node has more than MIN keys
In this case, deletion is very simple and key can be easily deleted from
the node y shifting other keys of the node

Delete 7, 52 30

12 20 40 55

3 7 9 11 15 19 25 28 35 38 45 47 52 65 78

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Delete 7, 52 30

12 20 40 55

3 7 9 11 15 19 25 28 35 38 45 47 52 65 78

30

12 20 40 55

3 9 11 15 19 25 28 35 38 45 47 65 78

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


CASE 2: If node has MIN keys
After the deletion of a key, the node will have less than MIN keys, and
will become an underflow node. In this case, we can borrow a key from
the left or right sibling if any one of them has more than MIN keys.
2a) Borrow a key from left sibling:

Given Tree →
30
12 20 40 55

3 7 9 11 15 19 22 25 28 35 38 45 47 65 78 80

Here key 15 is to be deleted from node [15, 19], since this node has only MIN keys, we
will try to borrow from its left sibling [3, 7, 9, 11] which has more than MIN keys. The
parent of these nodes is node [12, 20] and the separator key is 12. So the last key of left
sibling(11) is moved to the place of separator key and the separator key is moved to the
underflow node. The resulting tree after deletion of 15 will be:

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


2a) Borrow a key from left sibling:
Given Tree →
30
12 20 40 55

3 7 9 11 15 19 22 25 28 35 38 45 47 65 78 80

After deleting 15 →

30
11 20 40 55

3 7 9 12 19 22 25 28 35 38 45 47 65 78 80

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


2b) Borrow a key from right sibling:

Given Tree →
30
9 20 40 55

3 7 11 12 22 25 28 35 38 45 47 65 78 80

The left sibling of [45, 47] is [35, 38] which has only MIN keys so we can’t borrow from it,
hence we will try to borrow from the right sibling [65, 78, 80]. The first key of the right
sibling (65) is moved to the parent node and the separator key from the parent node (55) is
moved to the underflow node. In the underflow node, 47 is shifted left to make room for
55. In the right sibling, 78 and 80 are moved to fill the gap created by removal of 65.

After deleting 45→ 30


9 20 40 65

3 7 11 12 22 25 28 35 38 47 55 78 80

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


2c.1) If both left and right siblings of underflow node have MIN keys,
then we can’t borrow from any of the siblings. In this case, the underflow
node is combined with its left (or right) sibling.

Given Tree → 55
15 24 36 45 64 73 89

5 10 18 22 28 31 39 43 47 53 58 62 67 71 76 86 92 95

We can see that the node [28, 31] has only MIN keys so we’ll try to borrow from left
sibling [18, 22], but it also has MIN keys so we’ll look at the right sibling [39, 43] which
also has only MIN keys. So after deletion of 28 we’ll combine the underflow node with its
left sibling. For combining these two nodes the separator key (24) from the parent node
will move down in the combined node.

After deleting 28→ 55


15 36 45 64 73 89

5 10 18 22 24 31 39 43 47 53 58 62 67 71 76 86 92 95

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


2c.2) If both left and right siblings of underflow node have MIN keys,
then we can’t borrow from any of the siblings. In this case, the underflow
node is combined with its left (or right) sibling.
Given Tree → 55
15 36 45 64 73 89

5 10 18 22 24 31 39 43 47 53 58 62 67 71 76 86 92 95

Here the key is to be deleted from [58, 62] which is leftmost child of its parent, and hence
it has no left sibling. So here we’ll look at the right sibling for borrowing a key, but the
right sibling has only MIN keys, so we’ll delete 62 and combine the underflow node with
the right sibling.

After deleting 62 →
55
15 36 45 73 89

5 10 18 22 24 31 39 43 47 53 58 64 67 71 76 86 92 95

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


2c.3) If both left and right siblings of underflow node have MIN keys,
then we can’t borrow from any of the siblings. In this case, the underflow
node is combined with its left (or right) sibling.

Given Tree → 45
15 36 55 73

5 10 18 31 39 43 47 53 58 64 67 71 76 86 89 95

The underflow node [18] is combining with its left sibling

45

36 55 73

5 10 15 18 39 43 47 53 58 64 67 71 76 86 89 95

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Previous State
45

36 55 73

5 10 15 18 39 43 47 53 58 64 67 71 76 86 89 95

Now the parent node [36] has become underflow so we will try to borrow a key from its
right sibling (since it is leftmost node and has no left sibling), but the right sibling has MIN
keys so we will combine the underflow node [36] with its right sibling [55, 73]. The
separator key (45) comes down in the combined node, and since it was the only key in the
root node, now the root node becomes empty and the combined node becomes the new root
of the tree and height of the tree decreases by one.

After deleting 31 → 36 45 55 73

5 10 15 18 39 43 47 53 58 64 67 71 76 86 89 95

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


• Deletion from non-leaf node.
CASE 1:
In this case, the successor key is copied at the place of the key to be deleted
and then the successor is deleted. Successor key is the smallest key in the
right subtree and will always be in the leaf node. So this case reduces to case
A of deletion from a leaf node.
Given Tree → 30
12 20 40 50 60

5 10 15 19 25 27 28 35 38 45 48 53 57 69 78

The successor key of 12 is 15, so we’ll copy 15 at the place of 12 and now our task
reduces to deletion of 15 from the leaf node. This deletion is performed by borrowing a
key from the right sibling.

30
15 20 40 50 60

5 10 15 19 25 27 28 35 38 45 48 53 57 69 78

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


30
15 20 40 50 60

5 10 15 19 25 27 28 35 38 45 48 53 57 69 78

After deleting 12 →

30
15 25 40 50 60

5 10 19 20 27 28 35 38 45 48 53 57 69 78

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


CASE 2:
Given Tree → 30
15 25 40 50 60

5 10 19 20 27 28 35 38 45 48 53 57 69 78

The successor key of 30 is 35, so it is copied at the place of 30 and now 35 will be deleted
from the leaf node.
35
15 25 40 50 60

5 10 19 20 27 28 35 38 45 48 53 57 69 78

After deleting 30 →

35
15 25 50 60

5 10 19 20 27 28 38 40 45 48 53 57 69 78

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


B+ -Tree

B+ Tree is an extension of B Tree which allows efficient insertion, deletion


and search operations.

In B Tree, Keys and records both can be stored in the internal as well as leaf
nodes. Whereas, in B+ tree, records (data) can only be stored on the leaf
nodes while internal nodes can only store the key values.

The leaf nodes of a B+ tree are linked together in the form of a singly
linked list to make the search queries more efficient.

B+ trees are used to store the large amount of data that can not be stored in
the main memory. Due to the fact that, the size of the main memory is
always limited, the internal nodes (keys to access records) of the B+ tree are
stored in the main memory whereas, leaf nodes are stored in the secondary
memory.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


Figure 1: The B+ tree data structure

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


30
D30

12 20 40 50
D12 D20 D40 D50

5 10 15 19 25 27 28 35 38 45 48 60 90
D5 D10 D15 D19 D25 D27 D28 D35 D38 D45 D48 D60 D90
Fig: The B tree data structure

30
12 20 40 50

5 10 12 15 19 20 25 27 28 30 35 38 40 45 48 50 60 90
D5 D10 D12 D15 D19 D20 D25 D27 D28 D30 D35 D38 D40 D45 D48 D50 D60 D90
Fig: The B+ tree data structure

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India


B Tree VS B+ Tree
S. No. B Tree B+ Tree
1 Search keys can not be repeatedly Redundant search keys can be
stored. present.
2 Data can be stored in leaf nodes as Data can only be stored on the
well as internal nodes leaf nodes.
3 Searching for some data is a Searching is comparatively faster
slower process since data can be as data can only be found on the
found on internal nodes as well as leaf nodes.
on the leaf nodes.
4 Deletion of internal nodes are so Deletion will never be a
complicated and time consuming. complexed process since element
will always be deleted from the
leaf nodes.
5 Leaf nodes can not be linked Leaf nodes are linked together to
together. make the search operations more
efficient.

Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India

You might also like