0% found this document useful (0 votes)
13 views102 pages

Tree PDF

Uploaded by

roopitaryaman
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)
13 views102 pages

Tree PDF

Uploaded by

roopitaryaman
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/ 102

Tree

Unit-4

Madhvi Gaur (Asstt. Prof.)


Introduction

• Tree is a non-linear data structure.


• It is mainly used to represent data containing a hierarchical relationship between
elements e.g. records, family relationship and table of contents.

Madhvi Gaur (Asstt. Prof.)


Basic Terminology
• Node: Each data item in a tree is called a node.
• Root: It is specially designed node in a tree. It is a node which has no parent node.
There can be only one root in a tree
• Parent node: Suppose N is a node in T with left successor S1 and right successor
S2. Then N is called the parent of S1 and S2
• S1 is called the left child of N and S2 is called the right child of N.
• S1 and S2 are said to be siblings.
• N is called the predecessor of S1 and S2.
• Edge : The line drawn from one node to another node is called an edge.
• Path: A sequence of consecutive edges from source node to the destination node
is called a path.
• Leaf Node: The nodes with no successors is called terminal node/ leaf node.
• Level of a Node: The root R of the tree T is assigned the level number 0, then its
children are at level 1 and their children are at level 2 and so on. In general, if a
node is at level L then its child is at Madhvi
level GaurL+1.
(Asstt. Prof.)
• Height of a Tree: In a tree data structure, the number of edges from the leaf node
to the particular node in the longest path is known as the height of that node.
• In the tree, the height of the root node is called "Height of Tree".
• The tree height of all leaf nodes is 0.
• Depth of a Tree : the total number of edges from the root node to the leaf node in
the longest path is known as "Depth of Tree". the depth of the root node is 0.
• Degree of a Node: the total number of children of a node is called the degree of
the node.
• Degree of a Tree: The highest degree of the node among all the nodes in a tree is
called the Degree of Tree.
• Internal Node :Trees in the data structure have at least one child node known as
internal nodes. In trees, nodes other than leaf nodes are internal nodes.

Madhvi Gaur (Asstt. Prof.)


Madhvi Gaur (Asstt. Prof.)
Madhvi Gaur (Asstt. Prof.)
Binary Trees
A binary Tree T can have at most 2 children i.e. 0,1, or 2.
A binary tree T is defined as a finite set of elements, called nodes, such that:
(a) T is empty (called the null tree or empty tree), or
(b) T contains a distinguished node R, called the root of T, and the remaining nodes
of T form an ordered pair of disjoint tree T1 and T2.
If T contains a root R, then two trees T1 and T2 are called, respectively, the left and
right sub trees of R.
If T1 is nonempty, then its root is called the left successor of R.
If T2 is nonempty, then its root is called the right successor of R.

Madhvi Gaur (Asstt. Prof.)


Binary Tree T

C
B

D E G H

F L

Madhvi Gaur (Asstt. Prof.)


• T consists of 9 Nodes.
• The root of T is the node A at the top of the diagram.
• B is the left successor and C is the right successor of the node A.
• The left subtree of the root A consists of the nodes B, D, E and F.
• The right subtree of A consists of the nodes C, G, H and L.
• A node in a binary tree T has either 0, 1 or 2 successors.
• The nodes D, F, L and H have no successors. The nodes with no successors are
called terminal nodes or leaf nodes.

Madhvi Gaur (Asstt. Prof.)


Complete Binary Tree
Complete Binary Tree: Level r of T can have at most 2r nodes. A complete binary
tree is a special type of binary tree where all the levels of the tree are filled
completely except the lowest level nodes which are filled from as left as possible.

Full Binary Tree(Strict Binary Tree) : For a full binary tree, every node has either
2 children or 0 children.

Madhvi Gaur (Asstt. Prof.)


Extended Binary Tree or 2-Tree:
A binary tree T is said to be a 2-tree or an extended binary tree if each
node N has either 0 or 2 children.
The nodes with 2 children are called internal nodes, and the nodes with
0 children are called external nodes.
Internal nodes are represented by circles and the external nodes are
represented by squares.

Madhvi Gaur (Asstt. Prof.)


Binary Tree Extended 2-Tree

Madhvi Gaur (Asstt. Prof.)


Binary Tree Representation
Array/ Sequential Representation of Binary Tree (Static representation):

• An array can be used to store the nodes of a binary tree, which is called sequential
representation.
• In this representation, the nodes of tree are stored level-by-level, starting from the
0th level.
• The root node is always at index 0.
• Then, in successive memory locations the left child and the right child are stored.

Madhvi Gaur (Asstt. Prof.)


A

B C

D E F G

This representation uses only a single linear array Tree as follows:


0 1 2 3 4 5 6

A B C D E F G

The root of T is stored in TREE[0].


The parent of a node having index i will be stored at floor ((i-1)/2), if i is not equal to 0.
The left child of a node having index i is at (2*i+1).
The right child of a node having index i is at (2*i+2).
The index of Sibling of any node i will be i+1, if i is a left child of its parent.
The index of Sibling of any node i will be i-1, Madhvi
if i isGaur
the(Asstt.
right child of its parent.
Prof.)
The array representation is more efficient for complete binary trees. It is not suitable
for other than complete binary tree as it results in wastage of memory. Consider the
following binary tree:

It is a skewed binary tree. Since only the left sub-tree is present, this type of binary
tree is called left-skewed binary tree.
Madhvi Gaur (Asstt. Prof.)
Linked Representation of Binary Tree (Dynamic
Representation)
The binary tree can be represented using dynamic memory allocation in a linked list
form.
The basic component to be represented in a binary tree is a node.
The node consists of three fields:
(i) Info
Left_child Info Right_child
(ii) Left_child
(iii) Right_child

When a node has no children, the corresponding pointer fields will be NULL.

Madhvi Gaur (Asstt. Prof.)


Declaration of Structure

struct node
{
char info;
struct node *left;
struct node *right;
};

Madhvi Gaur (Asstt. Prof.)


Binary Tree T

C
B

D E F

Madhvi Gaur (Asstt. Prof.)


Linked Representation of Binary Tree
A

B C NULL

NULL D NULL E NULL NULL F NULL

NULL G NULL

Madhvi Gaur (Asstt. Prof.)


Traversal in Binary Tree
There are three types of traversals:
1. Preorder
(a) Process the root R.
(b) Traverse the Left-subtree of the root R
(c) Traverse the Right-subtree of the root R.

2. Inorder
(a) Traverse the Left-subtree of the root R.
(b) Process the root R.
(c) Traverse the Right-subtree of the root R.

3. Postorder
(a) Traverse the Left-subtree of the root R.
(b) Traverse the Right-subtree of the root R.
(c) Process the root. Madhvi Gaur (Asstt. Prof.)
void Preorder(struct node *root) void Postorder(struct node *root)
{ {
if(root!=NULL) if(root!=NULL)
{ {
printf(“%d”,root->info); Postorder(root->left);
Preorder(root->left); Postorder(root->right);
Preorder(root->right); printf(“%d”,root->info);
}} }}
void Inorder(struct node *root)
{
if(root!=NULL)
{
Inorder(root->left);
printf(“%d”,root->info);
Inorder(root->right);
}} Madhvi Gaur (Asstt. Prof.)
Binary Tree: Traversal

C
B

D E G H

F J L

K Gaur (Asstt. Prof.)


Madhvi
Pre-Order Traversal:
ABDEFCGJKHL

In-Order Traversal:
D B F EAK J G C HL

Post-Order Traversal:
DFEBKJGLHCA

Madhvi Gaur (Asstt. Prof.)


Binary Tree: Traversal

C
B

D E F

Madhvi Gaur (Asstt. Prof.)


Pre-Order Traversal:
ABDCEFG

In-Order Traversal:
BDAECGF

Post-Order Traversal:
DBEGFCA

Madhvi Gaur (Asstt. Prof.)


Creation of Binary Tree using Traversals
There are two ways of creating a binary tree such as:
(i) Pre-order and In-order Traversals
(ii) Post-order and In-order Traversals

Creation of Binary Tree from Pre-order and In-order Traversals:


Here pre-order and in-order traversing sequences are given, then we need to
construct a binary tree. The general procedure for creating a binary tree is as
follows:
Step 1: Scan the pre-order traversal from left to right.
Step 2: For each node scanned locate its position in in-order traversal. Let the
scanned node be X.
Step 3: The node preceding X in in-order form its left-subtree and node succeeding
it form right sub-tree.
Step 4: Repeat step 1 for each symbol inGaur
Madhvi pre-order.
(Asstt. Prof.)
Construct the binary tree given the following traversals:
Pre-order :ABDHE CFG
In-order :DHBEAFCG

DHBE A FCG

DH B E F C G

D H E F G

Madhvi Gaur (Asstt. Prof.)


Construct the binary tree given the following traversals:
Pre-order : FAE K C D H G B
In-order :EAC KFHDBG

EACK F HDBG

E A CK H D BG

E C K H B G

C B

Madhvi Gaur (Asstt. Prof.)


Creation of Binary Tree from Post-order and In-order Traversals:
Here post-order and in-order traversing sequences are given, then we need to
construct a binary tree. The general procedure for creating a binary tree is as
follows:
Step 1: Scan the post-order traversal from right to left.
Step 2: For each node scanned locate its position in in-order traversal. Let the
scanned node be X.
Step 3: The node preceding X in in-order form its left-subtree and node succeeding
it form right sub-tree.
Step 4: Repeat step 1 for each symbol in post-order.

Madhvi Gaur (Asstt. Prof.)


Construct the binary tree given the following traversals:
Post-order : HD IE B J FKLG CA
In-order : HD B I EAFJ C K GL

HDBI E A FJCKGL

HD B IE FJ C KGL

H D I E F J K G L

J K L
H I

Madhvi Gaur (Asstt. Prof.)


Threaded Binary Tree
• In a linked representation of a binary tree T, approximately half of the entries in
the pointer fields LEFT and RIGHT will contain null elements.
• This space may be more efficiently used by replacing the null entries by special
pointers.
• These special pointers are called threads and the binary tree with such pointers
are called threaded trees.
• Threads in a binary tree must be distinguished from normal pointers. The threads
are shown by dotted lines.
• If the LEFT child of a node p is equal to NULL, then it is replaced by a pointer to
the node which immediately precedes node p in inorder traversal. Such a tree is
called a left threaded tree.
• If the RIGHT child of a node p is equal to NULL, then it is replaced by a pointer
to its inorder successor node. Such a tree is called a right threaded tree.
• Both left and right NULL pointers can be used to print the predecessor and
successor of that node, respectively, under in-order traversal. Such a tree is called
a fully threaded tree. Madhvi Gaur (Asstt. Prof.)
In the memory representation of a threaded binary tree, it is necessary to distinguish
between a normal pointer and a thread.
Therefore, we have an alternate node representation for a threaded binary tree which
contains 4 fields:

Boolean variable Lchild or Lthread or Info Rchild or Rthread or


is_right_thread or NULL NULL
is_left_thread

Madhvi Gaur (Asstt. Prof.)


Madhvi Gaur (Asstt. Prof.)
Madhvi Gaur (Asstt. Prof.)
Madhvi Gaur (Asstt. Prof.)
Madhvi Gaur (Asstt. Prof.)
Application of Tree:

Converting Algebraic Expression into Binary Tree


Consider any algebraic expression E involving only binary operations, such as:
E= (a-b)/ ((c*d)+e)

- +

a b
* e

c d
Madhvi Gaur (Asstt. Prof.)
For the following expression ((a * x + b) * x + e) * x + f
Construct the expression tree. Find the equivalent postfix notation.

* f

+ x

* e

+ x

* b

a x Madhvi Gaur (Asstt. Prof.)


Application of Tree: Huffman Coding

• Huffman Coding is an application of Binary Tree.


• It is a technique of compressing data to reduce its size without losing
any of the details.
• The technique was initially developed by David Huffman.
• Huffman Coding is generally useful to compress the data in which
there are frequently occurring characters.
• In order to send any data file over the network, the data file can be
compressed and send to reduce the cost of transmission.

Madhvi Gaur (Asstt. Prof.)


Huffman Algorithm
Suppose there are n weights w1,w2,w3……wn.
Take two minimum weights w1 and w2 among the n given weights.
Then the subtree will be:
w1+w2

w1 w2

Create a tree T’ which gives the solution for the remaining weights : w1+w2, w3, w4,
w5 ……,wn.
The new 2-tree T is the desired solution.

Madhvi Gaur (Asstt. Prof.)


A binary tree T is said to be a 2-tree or an extended binary tree if each node N has
either 0 or 2 children.
The nodes with 2 children are called internal nodes, and the nodes with 0 children
are called external nodes.
Internal nodes are represented by circles and the external nodes are represented by
squares.
In any 2-tree, the number of external node is 1 more than the number of internal
nodes; that is: NE = NI + 1
The path length for any node(external/internal) is the sum of all nodes
traversed from root node to that node.

Madhvi Gaur (Asstt. Prof.)


Extended 2-Tree

LE = 3 + 3 + 2 + 3 + 4 + 4 + 2 = 21
LI = 0 + 1 + 2 + 1 + 2 + 3 = 9
So the total Path length of external node
LE = LI + 2 n = 9 + 2 * 6 = 21
Where n is the total number of internal
nodes in a tree.

Madhvi Gaur (Asstt. Prof.)


Suppose every external node has some (non-negative) weight W, then the weighted
path length P of the tree T :
P = W1 L1 + W2 L2 + W3 L3 +……..+ Wn Ln
Where Wi and Li denote, respectively, the weight and path length of an external
node Ni.

Madhvi Gaur (Asstt. Prof.)


Extended 2-Tree

P= 2 * 3 + 4 * 3 + 5 * 2 + 1 * 3 + 3* 4 + 2 * 4 + 2 * 2
P= 6 + 12 + 10 + 3 + 12 + 8 + 4
P= 55

5 2

2 4 1

3 2
Madhvi Gaur (Asstt. Prof.)
Madhvi Gaur (Asstt. Prof.)
Binary Search Tree
A binary tree T is called a binary search tree if each node N of T has the following
property:
The value at N is greater than every value in the left subtree of N and is less than
every value in the right subtree of N.
Let x be a node in BST:
If y is a node in the left subtree of x, 38
then key[y] < key[x]
If y is a node in the right subtree of x, 14 56
then key[y]> key[x]

23 45 82
8

70
18
Madhvi Gaur (Asstt. Prof.)
Insertion in BST
• To insert a new value into a BST, we use the TREE_INSERTBST(root, item)
procedure TREE_INSERT. 1. if (root = =NULL)
• If the item value is less than the root node,
2. Return TREE_CREATENODE(item)
then we have to traverse the left subtree.
3. Else if(item < root->info)
• If the value is greater than the root node, then
we have to traverse the right subtree. 4. root->left = TREE_INSERTBST(root->left,
item)
• If root is empty, then make the item as a root
node. 5. Else
TREE_CREATENODE(item) 6. root->right = TREE_INSERTBST(root->right,
item)
1. new_node = (struct node *)
malloc(sizeof(struct node)) 7. Return root.
2. new_node->left=NULL 8. Exit.
3. new_node->info=item
4. new_node->right=NULL
5. Return new_node
6. Exit.
Madhvi Gaur (Asstt. Prof.)
Searching a Node in BST
A common operation performed on a BST is searching for a key stored in the tree.
The following procedure is used to search for a node with a given key in a BST.
Given a pointer to the root of the tree and a key k, TREE_SEARCH returns a
pointer to a node with key k if one exists, otherwise it returns NULL.
TREE_SEARCH(root, k, PAR)
1. If root = NULL,
Set LOC= NULL and PAR_LOC= PAR and return.
2. If root->info = k
Set LOC =root and PAR_LOC =PAR and return.
3. If root->info > k
Set PAR = root
Return TREE_SEARCH(root->left, k, PAR)
else
Set PAR = root
Madhvi Gaur (Asstt. Prof.)
Return TREE_SEARCH(root->right, k, PAR)
Deletion in BST
If we want to delete a node in BST, then we have to consider three cases:
Case 1: if a node to be deleted has no child
Then we simply delete/ remove that node.
Case 2: if a node to be deleted has only one child either left or right
If the node to be deleted has only left child, then we simply delete that node and
replace that node with its left child.
If the node to be deleted has only right child, then we simply delete that node and
replace that node with its right child.
Case 3: If the node to be deleted has both child
Then we replace that node with its inorder successor.

Madhvi Gaur (Asstt. Prof.)


Suppose the BST given is:

21

18 24

7 19 22 26

30
Madhvi Gaur (Asstt. Prof.)
Case 1: Suppose we have to delete 30.

Then the BST will be:


21

18 24

7 19 22 26

Madhvi Gaur (Asstt. Prof.)


Case 2: Suppose we have to delete 26 in the
given BST
Then the BST will be:
21

18 24

7 19 22 30

Madhvi Gaur (Asstt. Prof.)


Case 3: Suppose we have to delete 18 in the
given BST
Then the BST will be:
21

19 24

7 22 26

30
Madhvi Gaur (Asstt. Prof.)
Deletion in BST
TREE_DELBST(root,item)
1. if(root = = NULL) // tree is empty.
Display “tree is empty” and return.
2. TREE_SEARCH(root,item,PAR)
3. if x==NULL // node to be deleted is not found
Display “ Data to be deleted is not found” and Return.
4. if(x->leftchild==NULL && x->rightchild==NULL) /*Case 1*/
5. if(Parent->rightchild==x)
Parent->rightchild=NULL
else
Parent->leftchild=NULL
free(x) and return.
6. if(x->leftchild==NULL && x->rightchild!=NULL) /*CASE 2*/
Madhvi Gaur (Asstt. Prof.)
7. if(parent->leftchild==x)
parent->leftchild = x->rightchild
else
parent->rightchild=x->rightchild
8. free(x) and return.
9. if(x->leftchild!=NULL && x->rightchild==NULL) /*CASE 2*/
10. if(parent->leftchild==x)
parent->leftchild = x->leftchild
else
parent->rightchild=x->leftchild
11. free(x) and return.

Madhvi Gaur (Asstt. Prof.)


12. If (x->leftchild!=NULL&& x->rightchild!=NULL) /* Case 3*/
Parent =x
x_succ=x->rightchild
13. Repeat step 14 and 15 while(x_succ->leftchild!=NULL)
14. Parent=x_succ
15. x_succ=x_succ->leftchild
16. x->info=x_succ->info
17. x=x_succ
18. free(x) and return.

Madhvi Gaur (Asstt. Prof.)


AVL TREE
An AVL tree is a height balanced tree. A height balanced tree is one in which the
difference in the height of two subtrees for any node is not more than 1.
An empty binary tree is an AVL tree.
If B is non-empty binary tree with TL and TR are its left and right sub-trees then B
is an AVL tree iff:
(a) TL and TR are AVL tree, and
(b) |hL-hR|<=1
Where hL and hR are the heights of TL and TR sub-trees respectively.
To implement an AVL tree each node must contain a balance factor, which indicates
its states of balance relative to its sub-trees.
Balance Factor(BF) = height of Left sub-tree - height of Right sub-tree
Then the balance factors in a balanced tree can have values of +1, 0 or -1. If it
is other than these three values then the tree is not balanced or it is not an AVL
tree.
Madhvi Gaur (Asstt. Prof.)
Representation of an AVL Tree

-1

0 1

A G

Madhvi Gaur (Asstt. Prof.)


Insertion in An AVL TREE
• Inserting an element into an AVL tree in its first phase is similar to that one used in
BST.
• After insertion of an element into an AVL search tree, the result may not be an
AVL tree or height balanced tree .That is, the tree may become unbalanced.
• If the tree becomes unbalanced, we must adjust the tree to restore the balance of
the binary search tree - this adjustment is called rotation.
The rebalancing rotations are classified as LL,LR,RR and RL based on the position
of the inserted node with reference to A.
1. LL: Inserted node is in the left subtree of the left subtree of node A
2. LR: Inserted node is in the right subtree of the left subtree of node A
3. RR: Inserted node is in the right subtree of the right subtree of node A
4. RL: Inserted node is in the left subtree of the right subtree of node A

Madhvi Gaur (Asstt. Prof.)


LL Rotation
0
+2

A1 A2

+1 0 0

A2 A3 A1

A3

Madhvi Gaur (Asstt. Prof.)


RR Rotation
0
-2

A1 A2

0 0
-1
A1 A3
A2

0
A3

Madhvi Gaur (Asstt. Prof.)


LR Rotation

+2 +2

A1 A1 A3

-1 +1

A2 A3
A2 A1

0
0
A3 A2

Madhvi Gaur (Asstt. Prof.)


RL Rotation
-2
-2
A1 A3
A1

-1
+1

A2 A3 A1 A2

0
0
A2
A3

Madhvi Gaur (Asstt. Prof.)


Construct an AVL tree by inserting the following elements
64,1,14,26,13,110,98,85
Insert 64 64
0

+1
Insert 1 64

0
1

Insert 14
+2 0
+2
64 14
64
LR Rotation +1 0
-1 0
1 14
1 64
0 0
14 1
Madhvi Gaur (Asstt. Prof.)
Insert 26 Insert 110
-1 0
14 14
-1
0 +1 0
1 64
1 64
0 0
0
0 13 26 110
26

Insert 13 0 Insert 98 -1
-1 14 14
+1 -1
1 64 1 -1
64
0 +1
0 0 0
13 26 110
13 26

0
98

Madhvi Gaur (Asstt. Prof.)


Insert 85

-2 -1
14 14
-1 -1
1 64 -2 -1
0 +2 1 64
LL
0 Rotation 0
13 26 110 0
0
13 26 98
+1
98 0
0
85 0 85 110

Madhvi Gaur (Asstt. Prof.)


m- way Search Tree
m-way search trees are generalized versions of BST.
The goal of m-way search tree is to minimize the access while retrieving a key from a file.
An m-way search tree of height h calls for O(h) number of accesses for an insert/delete/retrieval
operation.
An m-way search tree T may be an empty tree. If T is non-empty, it satisfies the following properties:
• For some integer m, known as order of the tree, each node has, at most m child nodes.
• If a node has k child nodes, then the node can have only (k-1) keys.
• The keys in each node are in ascending order i.e. ki<ki+1
• Each of the keys partitions all the keys in the subtrees into k subsets.
• The key ki is larger than keys in sub-tree pointed by Pi and smaller than keys in subtree pointed by
Pi+1.
A binary tree is 2-way tree. It means it has m-1=1 key and it can have maximum of two children.
A binary tree is also called an m-way tree of order 2.

Madhvi Gaur (Asstt. Prof.)


Deletion in m-way Search Tree
Let K be a key to be deleted from the m-way search tree.
Let the node accommodating the key as follows:

……… K ……….

Ai Aj
Where K is the key element, Ai and Aj are pointer to subtrees.
If Ai=Aj=NULL then delete K.
If Ai≠NULL, Aj=NULL, then choose the largest of the key elements K’ in the child node pointed by
Ai , delete the K’ and replace K by K’. (Deletion of K’ may call for subsequent replacements and
therefore deletions in similar manner.)
If Ai=NULL, Aj≠NULL, then choose the smallest of the key elements K’’ in the child node pointed
by Aj , delete the K’’ and replace K by K’’. (Deletion of K’’ may call for subsequent replacements and
therefore deletions in similar manner.)
If Ai≠NULL, Aj≠NULL, then choose either the largest of the key elements K’ in the subtree pointed
to by Ai or the smallest of the key elements K’’ from the subtree pointed to by Aj to replace K.

Madhvi Gaur (Asstt. Prof.)


B-Tree
M-way search trees have the advantage of minimizing file accesses due to their
restricted height.
However it is essential that the height of the tree be kept as low as possible and
therefore there arises the need to maintain balanced m-way search trees. Such a
balanced m-way search tree is known as B-tree.

Madhvi Gaur (Asstt. Prof.)


Properties of B-Tree

A B-tree of order m, if non-empty, is an m-way search tree in which:


(i) The root has at least two child nodes and at most m child nodes.
(ii) The internal nodes except the root have at least upper_bound (m/2)
child nodes and at most m child nodes.
(iii) The number of keys in each internal node is one less than the
number of child nodes.
(iv) All leaf nodes are on the same level.

Madhvi Gaur (Asstt. Prof.)


Insertion in B-Tree
The insertion of a key in a B-tree proceeds as if one were searching for the key in
the tree.
When the search terminates in a failure at a leaf node and tends to fails off the tree,
the key is inserted according to the following procedure:
• If the leaf node in which the key is to be inserted is not full, then the insertion is
done in the node.
• A node is said to be full if it contains a maximum of (m-1) keys and at least
upper_bound (m/2) -1 keys where m is the order of B-tree.
• If the node is full, then insert the key in order into existing set of keys in the node,
split the node at its median into two nodes at the same level, pushing the median
element up by one level. Accommodate the median element in the parent node if it
is not full. Otherwise repeat the same procedure.

Madhvi Gaur (Asstt. Prof.)


Note:
• The root has at least two children.
• Each node except root can have a maximum of m children and at
least ⌈m/2⌉ children.
• Each node can contain a maximum of m - 1 keys and a minimum of ⌈m/2⌉ -
1 keys.

Madhvi Gaur (Asstt. Prof.)


Construct a B-Tree of degree/order 4 with the following list of elements:
1, 5, 6, 2, 8, 11, 13, 18, 20, 7, 9
2 6
Insert 1 : Insert 11:
1

Insert 5: 1 5 1 5 8 11

Insert 6: 1 5 6 Insert 13: 2 6

Insert 2: 2 1 5 8 11 13

1 5 6 2 6 11
Insert 18:
Insert 8:
2 1 5 8 13 18

1 5 6 8

Madhvi Gaur (Asstt. Prof.)


Insert 20: 2 6 11

1 5 8 13 18 20

Insert 7: 2 6 11

1 5 7 8 13 18 20

2 6 11
Insert 9:
1 5 7 8 9 13 18 20

Madhvi Gaur (Asstt. Prof.)


Deletion in B-Tree
1. Node is Leaf Node
2. Node is non-Leaf Node
While deleting a key from a leaf node, if the node contains more than the minimum number of
elements, then the key can be easily removed.
If the leaf node contains just the minimum number of keys then first we will see the number of keys
in adjacent leaf node. If it has more than minimum number of keys then first key of the adjacent node
will go to the parent node and key in the parent node which is partitioning, will be combined together
in one node.
Suppose now parent node has also less than the minimum number of keys then the same thing will be
repeated until we get the node which has more than the minimum number of keys.
For the Second case, key will be deleted and its predecessor or successor key will come on its place.
Suppose both nodes of predecessor and successor key have minimum number of keys then the nodes
of predecessor and successor keys will be combined.

Madhvi Gaur (Asstt. Prof.)


Madhvi Gaur (Asstt. Prof.)
Madhvi Gaur (Asstt. Prof.)
Madhvi Gaur (Asstt. Prof.)
Madhvi Gaur (Asstt. Prof.)
Madhvi Gaur (Asstt. Prof.)
Madhvi Gaur (Asstt. Prof.)
Deletion in B-Tree
B-Tree of order 5:
110

65 86 120 226

32 44 70 81 90 95 100 115 118 200 221 300 440 550 601

Delete 95,226: 110

65 86 120 300

32 44 70 81 90 100 115 118 200 221 440 550 601

Madhvi Gaur (Asstt. Prof.)


Delete 221:
110

65 86 120 440

32 44 70 81 90 100 115 118 200 300 550 601

Delete 70:

86 110 120 440

32 44 65 81 90 100 115 118 200 300 550 601

Madhvi Gaur (Asstt. Prof.)


B-Tree Operations

• B-Tree of order 4
• Each node has at most 4 pointers and 3 keys, and at least 2 pointers and 1 key.
• Delete: 2, 21, 10, 3, 4

Madhvi Gaur (Asstt. Prof.)


Suppose the B-tree given is:

a
*9*

f g
*3*7* * 13 *

b d h c e
*1*2* *4*5* *8* * 10 * 12 * * 21 *

Madhvi Gaur (Asstt. Prof.)


Delete 2

*9* a

f g

*3*7* * 13 *

b d h c e

*1* *4*5* *8* * 10 * 12 * * 21 *

Node b can loose an element without underflow.

Madhvi Gaur (Asstt. Prof.)


Delete 21

*9* a

f g
*3*7* * 12 *

b d h c e
*1* *4*5* *8* * 10 * * 13 *

Deleting 21 causes node e to underflow, so elements are redistributed


between nodes c, g, and e

Madhvi Gaur (Asstt. Prof.)


Delete 10

*3*7*9* a

b d h e
*1* *4*5* *8* * 12 * 13 *

Deleting 10 causes node c to underflow. This causes the parent, node


g to recombine with nodes f and a. This causes the tree to shrink one
level.

Madhvi Gaur (Asstt. Prof.)


Delete 3

*4*7*9* a

b d h e
*1* *5* *8* * 12 * 13 *

Because 3 is a pointer to nodes below it, deleting 3 requires keys to be


redistributed between nodes a and d.

Madhvi Gaur (Asstt. Prof.)


Delete 4

*7*9* a

b h e
*1*5* *8* * 12 * 13 *

Deleting 4 requires a redistribution of the keys in the subtrees of 4;


however, nodes b and d do not have enough keys to redistribute
without causing an underflow. Thus, nodes b and d must be
combined.

Madhvi Gaur (Asstt. Prof.)


B+ Tree
• The B-tree structure is the standard organization for indexing in a database system. There
are several variations of the B-tree, known as B*-tree and B+-Tree.
• The B-tree guarantees at least 50% storage utilization i.e. at any given time, the tree has
each of its nodes at least 50% full.
• The B+-tree is slightly different data structure, which in addition to indexed access, also
allows sequential data processing and store all data in the leaf nodes of the tree.
• It is a structure of nodes linked by pointers.
• It stores keys only at leaves and stores reference values in internal nodes.
• It also provides faster sequential access of data.
• In B+-tree all the leaves have been connected to form a linked list of keys in sequential
order.
• The B+-tree has two parts:
Index Part which consists of internal nodes.
Sequence Set which consists of leaf nodes.
Madhvi Gaur (Asstt. Prof.)
B+ Tree Insertion

The following steps are followed for inserting an element.


• Since every element is inserted into the leaf node, go to the appropriate leaf node.
• Insert the key into the leaf node.

Madhvi Gaur (Asstt. Prof.)


Case I
• If the leaf is not full, insert the key into the leaf node in increasing order.
Case II
• If the leaf is full, insert the key into the leaf node in increasing order and balance
the tree in the following way.
• Split the leaf node into two nodes.
• First node contains ceil((m-1)/2) values.
• Second node contains the remaining values.
• Copy the smallest search key value from second node to the parent node.(Right
biased)
 Note➔ Splitting of Leaf node requires median key to be retained in the leaf node
on the other hand, splitting of internal node does not require so.

Madhvi Gaur (Asstt. Prof.)


B+ Tree Insertion

Madhvi Gaur (Asstt. Prof.)


Problem: Insert the following key values 6, 16, 26, 36, 46 on a B+ tree with order
= 3.

Madhvi Gaur (Asstt. Prof.)


Step 1: The order is 3 so at maximum in a node so there can be only 2 search key
values.
As insertion happens on a leaf node only in a B+ tree so insert search key value 6
and 16 in increasing order in the node.

Madhvi Gaur (Asstt. Prof.)


Step 2: We cannot insert 26 in the same node as it causes an overflow in the leaf
node, We have to split the leaf node according to the rules. First part
contains ceil((3-1)/2) values i.e., only 6. The second node contains the remaining
values i.e., 16 and 26. Then also copy the smallest search key value from the second
node to the parent node i.e., 16 to the parent node.

Madhvi Gaur (Asstt. Prof.)


Madhvi Gaur (Asstt. Prof.)
Step 3: Now the next value is 36 that is to be inserted after 26 but in that node, it
causes an overflow again in that leaf node. Again follow the above steps to split the
node. First part contains ceil((3-1)/2) values i.e., only 16. The second node contains
the remaining values i.e., 26 and 36. Then also copy the smallest search key value
from the second node to the parent node i.e., 26 to the parent node.

Madhvi Gaur (Asstt. Prof.)


Madhvi Gaur (Asstt. Prof.)
Step 4: Now we have to insert 46 which is to be inserted after 36 but it causes an
overflow in the leaf node. So we split the node according to the rules. The first part
contains 26 and the second part contains 36 and 46 but now we also have to
copy 36 to the parent node but it causes overflow as only two search key values can
be accommodated in a node. Now follow the steps to deal with overflow in the non-
leaf node.
First node contains ceil(3/2)-1 values i.e. ’16’.
Move the smallest among remaining to the parent i.e ’26’ will be the new parent
node.
The second node contains the remaining keys i.e ’36’ and the rest of the leaf nodes
remain the same.

Madhvi Gaur (Asstt. Prof.)


Madhvi Gaur (Asstt. Prof.)

You might also like