0% found this document useful (0 votes)
39 views92 pages

DS Unit-3

The document discusses different types of tree data structures and their properties. It describes binary trees and their representations using arrays and linked lists. The key types of binary tree traversals - inorder, preorder and postorder - are summarized. Different types of binary trees are defined, including strict, complete, perfect and extended binary trees. Expression trees are described as binary trees where internal nodes are operators and leaves are operands.

Uploaded by

himagiri
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)
39 views92 pages

DS Unit-3

The document discusses different types of tree data structures and their properties. It describes binary trees and their representations using arrays and linked lists. The key types of binary tree traversals - inorder, preorder and postorder - are summarized. Different types of binary trees are defined, including strict, complete, perfect and extended binary trees. Expression trees are described as binary trees where internal nodes are operators and leaves are operands.

Uploaded by

himagiri
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/ 92

Prepared by D.

HIMAGIRI
UNIT - III
----------------------------------------------------------------------------------------------------------
Trees – Terminology, Representation of Trees, Binary tree ADT, Properties of Binary Trees,
Binary Tree Representations-array and linked representations, Binary Tree traversals, Threaded
binary trees, Binary Heap-Properties,Max and Min Heap, Operations-Insertion and Deletion.
Search Trees-Binary Search tree, Tree traversals, AVL tree – operations, B-tree – operations, B+
trees, Red Black tree.
----------------------------------------------------------------------------------------------------------
Tree:
A tree is a non linear hierarchical data structure,consists of nodes connected by edges.
Why Tree Data Structure?
Other data structures such as arrays, linked list, stack,
and queue are linear data structures that store data
sequentially. In order to perform any operation in
a linear data structure, the time complexity increases
with the increase in the data size. But, it is not acceptable in
today's computational world.
Different tree data structures allow quicker and easier access to the
data as it is a non-linear data structure.
1
Prepared by D.HIMAGIRI
Tree Terminology

Node-A node is an entity that contains a key or value and pointers/link to its child nodes.
Edge-It is the link between any two nodes.
Root -The node at the top/start of the tree is called root. There is only one root per tree.
Parent - Any node except the root node has one edge upward to a node called parent.
Child - The node below a given node connected by its edge downward is called its child node.
Leaf - The node which does not have any child node is called the leaf node.
Subtree -Subtree represents the descendants of a node.
Levels -Level of a node represents the generation of a node. If the root node is at level 0, then
its next child node is at level 1, its grandchild is at level 2, and so on.
2
Siblings: if two are more nodes are having same parent in a tree, then they are called siblings.
Prepared by D.HIMAGIRI
Tree Terminology...
. Height of a Node-The height of a node is the number of edges from the node to the deepest
leaf (ie. the longest path from the node to a leaf node).
Depth of a Node-The depth of a node is the number of edges from the root to the node.
Height of a Tree-The height of a Tree is the height of the root node or the depth of the deepest
node or maximum of all nodes height or maximum of all
nodes depth.
 In a tree, height of a tree is equal to depth of a tree.

3
Prepared by D.HIMAGIRI
Types of Trees
 Different types of tree are:
1. Binary Tree
I. Strict Binary Tree
II. Complete Binary Tree
III. Perfect Binary Tree
IV. Extended Binary Tree
2. Expression Tree
3. Threaded Binary Tree
4. Binary Heap
5. Search Trees
I. Binary Search Tree(BST)
II. AVL Tree
III. Red Black Tree
IV. B-Tree
V. B+-Tree

4
Prepared by D.HIMAGIRI
Binary Tree
 A Binary tree is a tree which is either empty or each node can have maximum of two child
nodes, i.e. each node can have 0, or 1 or 2 child nodes.
Binary Tree Representations:
1. Array Representation
2. Linked List Representation
 Array Representation of Binary Tree
 Binary Tree is stored in a single array.
 Number are assigned to each node from leftmost to rightmost node.
 Root node always assign no. 1
 Left Child is placed at position [2 * K] (k is position of root/parent)
 Right Child is placed at position [2 * K + 1] A 1
 Size of array is Depends on Depth of tree i.e. 2 d+1-1
2 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 B C

A B C D E F G H I 4 5 6 7
D E F G
Note: If root is placed at 0th position then
8 9
L Child = [2 * k + 1 ]
H I 5
R child = [2 * K + 2 ]
Prepared by D.HIMAGIRI
Binary Tree Representations...
Linked List Representation of Binary Tree:
Each node contains three fields –Left child address , Data and Right child address.
If the node does not contain left /right child then address field contains NULL.
Node of a binary tree is declared as:
struct node E.g.
{ A
struct node*left;
int data; B C
struct node *right;
}; D E F G

6
Prepared by D.HIMAGIRI
Types of Binary Tree
1. Strict Binary Tree:
 Each node in a tree is either leaf or has exactly two children.

Strict Binary Tree

 Strictly binary tree with n Leaves always contains 2n-1 nodes


E.g.
• 2 Leaves in First Example having 2*2-1=3 Nodes.
• 3 Leaves in Second Example having 3*2-1=5 Nodes.
• 4 Leaves in Third Example having 4*2-1=7 Nodes

7
Prepared by D.HIMAGIRI
Types of Binary Tree...
2.Complete Binary Tree:
A complete binary tree is a binary tree in which all the levels are completely filled except
possibly the lowest one, which is filled from the left.

3.Perfect Binary Tree:


A perfect binary tree is a binary tree in which all interior nodes have two children and
all leaves are at same level.

8
Prepared by D.HIMAGIRI
Types of Binary Tree...
4.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.
Properties of External binary tree
1. The nodes from the original tree are internal nodes and the special nodes are
external nodes.
2. All external nodes are leaf nodes and the internal nodes are non-leaf nodes.
3. Every internal node has exactly two children and every external node is a leaf.
Example:

9
Prepared by D.HIMAGIRI
Binary Tree Traversals
Binary Tree Traversal
Traversal is a process to visit all the nodes of a tree and print their values . Because, all
nodes are connected via edges (links) we always start from the root node.
Different types of tree traversals are:
1. In-order Traversal
2. Pre-order Traversal
3. Post-order Traversal
1. In-order Traversal (LDR)
1. First, visit all the nodes in the left sub tree.
2. Then the root node.
3. Visit all the nodes in the right sub tree.

In-order: D → B → E → A → F → C → G
10
Prepared by D.HIMAGIRI
Binary Tree Traversals
2. Pre-order Traversal (DLR)
1. Then the root node.
2. First, visit all the nodes in the left sub tree.
3. Visit all the nodes in the right sub tree.

Pre -order: A → B → D → E → C → F → G
3.Postorder traversal
1. Visit all the nodes in the left subtree.
2. Visit all the nodes in the right subtree.
3. Visit the root node.

11
Post -order: D → E → B → F → G → C → A
Prepared by D.HIMAGIRI
Expression Tree
 Expression tree is a binary tree in which each internal node corresponds to operator and each
leaf node corresponds to operand.
 Example: Expression tree for 3 + ((5+9)*2)
 Steps for construction of Expression tree for a given
expression:
1. Convert the given expression to post fix expression.
2. Scan the postfix expression from left to right one character
at a time.
i. If character is operand push that into stack.
ii. If character is operator pop two values from stack make them its child and push
current node again.
3. At the end only element of stack will be root of expression tree.
Problem: Expression is (a+b)*c
1. The postfix is: a b + c *
2. The first symbol ‘a’ is operand, it is pushed onto the stack

12
Prepared by D.HIMAGIRI
Expression Tree...
Next symbol ‘b’ is an operand , insert ‘b’ onto stack

Next, '+' symbol, so topmost two operands are popped , a new tree is formed and push a
pointer to it onto the stack.

Next, 'c' is read, we create one node tree and push a pointer to it onto the stack.

13
Prepared by D.HIMAGIRI
Expression Tree...
Finally, the last symbol is read ' * ', we pop topmost two tree pointers and form a new tree
with ' * ' as root, and a pointer to the final tree is pushed on the stack.

Pop the only element from the stack , we get an expression tree

14
Prepared by D.HIMAGIRI
Threaded Binary Tree
 Threaded Binary Tree:
Binary tree is said to be threaded by making all right child pointers that would normally be
null point to the inorder successor of the node (if it exists), and all left child pointers that
would normally be null point to the inorder predecessor of the node .
Why do we need Threaded Binary Tree?
1. Binary trees have a lot of wasted space, the leaf nodes each have 2 null pointers. We can
use these pointers to help us in inorder traversals.
2. Threaded binary tree makes the tree traversal faster since we do not need stack or
recursion for traversal.
 Example:

15
Prepared by D.HIMAGIRI
Threaded Binary Tree...
 Types of threaded binary tree
1. Right threaded binary tree
The right NULL pointer of the node
is replaced by a thread to the inorder
successor of that node is called
as right threaded binary tree.
2. Left threaded binary tree
The left NULL pointer of the node
is replaced by a thread to the inorder
predecessor of that node is called
as right threaded binary tree.
3. Fully threaded binary tree
Both left and right NULL pointers can
be used to point to Inorder predecessor and
successor of that node respectively,
is called a fully threaded binary tree.

16
Prepared by D.HIMAGIRI
Threaded Binary Tree...
 Construct threaded binary tree for following binary tree
1. Write the inorder traversal for the given binary tree
Inorder is D B H E A F C G
2. Represent the binary tree using linked list representation

3. Link the left and right NULL pointers to inorder Successor or predecessor respectively
as shown below

17
Prepared by D.HIMAGIRI
Binary Heap
 A binary tree is said to be binary heap if it follows the following two properties:
1. Heap order property
Every node is less than or equal to its children (Min Heap) or every node is greater than
or equal to its children(Max Heap).
2. Structure property
The tree is completely filled except possibly the bottom level, which is filled from left
to right.

18
Prepared by D.HIMAGIRI
Binary Heap…
 Different Heaps

19
Prepared by D.HIMAGIRI
Binary Heap...
 Operations:
1. Insertion:
Steps:
1. First increase the heap size by 1, so that it can store the new element.
2. Insert the new element at the end of the Heap.
3. This newly inserted element may distort the properties of Heap for its parents. So, in
order to keep the properties of Heap, heapify this newly inserted element following a
bottom-up approach.

20
Prepared by D.HIMAGIRI
Binary Heap...

Construct Binary Heap for following elements:


35, 33, 42, 10 ,14, 19, 27, 44, 26 ,31,20,18,99,81

21
Prepared by D.HIMAGIRI
Binary Heap...
2. Deletion:
1. Delete the root element
2. Replace the root by the last element.
3. Delete the last element from the Heap.
4. Since, the last element is now placed at the position of the root node. So, it may not
follow the heap property. Therefore, heapify the last node placed at the position of
root.

22
Prepared by D.HIMAGIRI
Binary Search Tree(BST)
 Binary Search Tree:
A binary search tree, also known as an ordered binary tree, in which all the nodes in the left
sub-tree have a value less than that of the root node. Correspondingly, all the nodes in the
right sub-tree have a value either equal to or greater than the root node. The same rule is
applicable to every sub-tree in the tree.
 Example :

 Operations :
1. Insert: insert a node in the BST.
2. Search: Searches for a node in the BST.
3. Delete: deletes a node from the BST. 23
Prepared by D.HIMAGIRI
Binary Search Tree(BST)...
 Insert:
Inorder to insert an element, first locate its proper location. Start searching from the root
node, then if the data is less than the key value, search for the empty location in the left sub
tree and insert the data. Otherwise, search for the empty location in the right sub tree and
insert the data.

24
Prepared by D.HIMAGIRI
Binary Search Tree(BST)...

25
Prepared by D.HIMAGIRI
Binary Search Tree(BST)...
 Search:
Inorder to search an element in BST, start from the root node. if the data is less than the key
value, search for the element in the left sub tree. Otherwise, search for the element in the right
sub tree. Follow the same procedure for each node.
 Example: Search for element 12

26
Prepared by D.HIMAGIRI
Binary Search Tree(BST)...
 Delete:
Deleting a node from Binary search tree includes following three cases:
Case 1: Deleting a Leaf node (A node with no children)
Case 2: Deleting a node with one child
Case 3: Deleting a node with two children

 Case 1: Deleting a Leaf node (A node with no children)


Search for that node in BST, If the node to be deleted is the leaf node then simply delete the
node from the tree .

27
Prepared by D.HIMAGIRI
Binary Search Tree(BST)...
Case 2: Deleting a node with one child
In the second case, the node to be deleted lies has a single child node. In such a case follow the
steps below:
1. Replace that node with its child node.
2. Remove the child node from its original position.

28
Prepared by D.HIMAGIRI
Binary Search Tree(BST)...
Case 3: Deleting a node with two children
In the third case, the node to be deleted has two children. In such a case follow the steps below:
1. Replace the node’s value with its in-order predecessor (largest value in the left sub-tree)
or in-order successor (smallest value in the right sub-tree).
2. The in-order predecessor or the successor can then be deleted using case1 or case2.

29
Prepared by D.HIMAGIRI
Binary Search Tree(BST)...
Deletion:

30
Prepared by D.HIMAGIRI
AVL Tree
AVL Tree:
 It is a self-balancing binary search tree invented by G.M. Adelson-Velsky and E.M.
Landis in 1962.
The structure of an AVL tree is the same as that of a binary search tree, in addition to this
every node in this tree is associated with balance factor.
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)
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 of the tree.

31
AVL Tree Not AVL Tree
Prepared by D.HIMAGIRI
AVL Tree...
 The Unbalanced node in the AVL tree is called critical node.
In order to balance the unbalanced tree/node we have to make rotations .Four types of rotations
are:
1. LL rotation: The new node is inserted in the left sub-tree of the left sub-tree of the
critical node.

32
Prepared by D.HIMAGIRI
AVL Tree...
LL Rotation Example:

2.RR Rotation: The new node is inserted in the right sub-tree of the right sub-tree of the
critical node.

33
Prepared by D.HIMAGIRI
AVL Tree...

RR Rotation Example

34
Prepared by D.HIMAGIRI
AVL Tree...
3.LR Rotation :The new node is inserted in the right sub-tree of the left sub-tree of the critical
node.

35
Prepared by D.HIMAGIRI
AVL Tree...
LR Rotation Example

36
Prepared by D.HIMAGIRI
AVL Tree...
4.RL Rotation: The new node is inserted in the left sub-tree of the right sub-tree of the
critical node.

37
Prepared by D.HIMAGIRI
AVL Tree...
RL Rotation Example

38
Prepared by D.HIMAGIRI
AVL Tree...
Operations:
1. Insertion
2. Deletion
3. Searching ( Search operation in AVL is same as BST)
1. Insertion:
 Insertion in an AVL tree is done in the same way as it is done in a binary search tree.
 In the AVL tree, the new node is always inserted as the leaf node.
 After insertion the balance factor of each node has to be updated. If any unbalance node
is found then rotation has to be performed to balance the tree.
Example:
Construct an AVL tree for the elements: H, I, J, B, A, E, C, F, D, G, K, L

Insert H Insert I

39
Prepared by D.HIMAGIRI
AVL Tree...
Example...
Elements: H, I, J, B, A, E, C, F, D, G, K
Insert J

Insert B

40
Prepared by D.HIMAGIRI
AVL Tree...
Example...
Elements: H, I, J, B, A, E, C, F, D, G, K
Insert A

Insert E

41
Prepared by D.HIMAGIRI
AVL Tree...
Example...
Elements: H, I, J, B, A, E, C, F, D, G, K
Insert C Insert F

Insert D

42
Prepared by D.HIMAGIRI
AVL Tree...
Example...
Elements: H, I, J, B, A, E, C, F, D, G, K
Insert G

Insert K

43
Prepared by D.HIMAGIRI
AVL Tree...
2. Deletion:
 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.(L rotations are mirror images of R Rotations)
 R rotations are
1. R0 rotation (Node B has balance factor 0 )

44
Prepared by D.HIMAGIRI
AVL Tree...
R0 Rotation Example

45
Prepared by D.HIMAGIRI
AVL Tree...
R0 Rotation Example

46
Prepared by D.HIMAGIRI
AVL Tree...
2. R1 Rotation (Node B has balance factor 1)

Example

47
Prepared by D.HIMAGIRI
AVL Tree...
2. R-1 Rotation (Node B has balance factor -1)

Example:

48
Prepared by D.HIMAGIRI
Red Black Tree
Red Black Tree:
 It is a self balanced Binary Search Tree in which every node is colored either RED or
BLACK .
 In Red Black Tree, the color of a node is decided based on the properties of Red-Black Tree.
Every Red Black Tree has the following properties.
Properties of Red Black Tree
1. Red - Black Tree must be a Binary Search Tree.
2. The ROOT node must be colored BLACK.
3. The children of Red colored node must be colored BLACK. (There should not be two
consecutive RED nodes).
4. In all the paths of the tree, there should be same number of BLACK colored nodes.
5. Every new node must be inserted with RED color.
6. Every leaf (e.i. NULL node) must be colored BLACK.

49
Prepared by D.HIMAGIRI
Red Black Tree...
Operations:
1.Insertion:
 Insertion in red black tree is same as BST.
 The color of inserted node is Red.

 In above Red Black tree g- Grand parent of node x, p is parent of x and u is uncle of x,
where x is newly inserted node.
 In Red-Black tree, we use two tools to do balancing.
1) Recoloring
2) Rotation
 If uncle is red, we do recoloring.
 If uncle is black, we do rotations and/or recoloring.
 Color of a NULL node is considered as BLACK.
50
Prepared by D.HIMAGIRI
Red Black Tree...
Insertion Algorithm:
Let x be the newly inserted node, the color of newly inserted nodes as RED .
Case 1: If x is root, change color of x as BLACK

Case 2: If x’s uncle is RED (Red Uncle Condition)


1. Change color of parent and uncle as BLACK.
2. color of grand parent as RED.
3. Change x = x’s grandparent, repeat steps 2 and 3 for new x.

51
Prepared by D.HIMAGIRI
Red Black Tree...
Case 3: If x’s uncle is BLACK or no uncle, The possible four configurations are
1. Left Left Case (p is left child of g and x is left child of p).
i. Right Rotate Grand parent g
ii. Swap colors of g and p

52
Prepared by D.HIMAGIRI
Red Black Tree...
2. Right Right Case (p is right child of g and x is right child of p).
i. Left Rotate Grand parent g
ii. Swap colors of g and p

53
Prepared by D.HIMAGIRI
Red Black Tree...
3. Left Right Case (p is left child of g and x is right child of p).
i. Left Rotate p
ii. Apply Left-Left Case

54
Prepared by D.HIMAGIRI
Red Black Tree...
4. Right Left Case (p is right child of g and x is left child of p).
i. Right Rotate p
ii. Apply Right-Right Case

55
Prepared by D.HIMAGIRI
Red Black Tree...
Problem: Insert the following elements into Red black tree
2, 1, 4, 5, 9, 3, 6, 7

56
Prepared by D.HIMAGIRI
Red Black Tree...
Problem: Insert the following elements into Red black tree
2, 1, 4, 5, 9, 3, 6, 7

57
Prepared by D.HIMAGIRI
Red Black Tree...
Problem: Insert the following elements into Red black tree
2, 1, 4, 5, 9, 3, 6, 7

58
Prepared by D.HIMAGIRI
Red Black Tree...
Deletion:
Important points to be remembered during deletion:
1. Red Black tree follows BST Deletion
2. In this only last nodes(leave nodes) are deleted ,Nodes with children are replaced.
3. In case node to be replaced has two children then replace it with closest predecessor
from left sub tree.
4. In case node to be replaced has one child then replace it with its child.
5. In case node to be replaced has no child then replace it with NULL node.
6. If black node replaces black node then it will become double black.
7. If red node replaces black node then it will become single black.
Deletion Cases:
Cases 1:
i) if deleted node D is RED-Delete it directly and do nothing.

59
Prepared by D.HIMAGIRI
Red Black Tree...
ii) if deleted node D is not RED-then Double Black DB Exists.

Case 2:If DB is a root node –then remove DB directly and make it single black.

Case 3:
3.1 if DB’s sibling is red
i. Rotate P in DB direction
ii. Swap colors of P and S
iii. Reapply cases if needed.

60
Prepared by D.HIMAGIRI
Red Black Tree...

3.2 if DB’s sibling is black and both children are black(SL and ST are black)
i. Remove DB
ii. Add black to P:if P is black make DB ,if P is red make single black.
iii. Make S as RED
iv. DB still exist ,follow other cases.

61
Prepared by D.HIMAGIRI
Red Black Tree...
3.3 if DB’s sibling is black ,inline child SL is black and ST is RED
i. Swap colors of S and ST
ii. Rotate S in opposite direction to DB
iii. Follow case 3.4

62
Prepared by D.HIMAGIRI
Red Black Tree...
3.4 if DB’s sibling is black ,inline child SL is RED
i. Swap colors of P and S
ii. Rotate P in same direction to DB
iii. Remove BD
iv. Change color of SL to Black

63
Prepared by D.HIMAGIRI
Red Black Tree...
Deletion Example: Delete 50,20,100,90,40,60,10,30,70,80 from the following RB Tree

Delete 50

Case3.1

64
Prepared by D.HIMAGIRI
Red Black Tree...
Deletion Example: Delete 50,20,100,90 from the following RB Tree

Case3.2

Delete 20

Case3.2

65
Prepared by D.HIMAGIRI
Red Black Tree...
Deletion Example: Delete 50,20,100,90 from the following RB Tree

Case3.2 Case 2

Delete 100

Case 1

66
Prepared by D.HIMAGIRI
Red Black Tree...
Deletion Example: Delete 50,20,100,90 from the following RB Tree
Delete 90

Case 3.3 -swap color of


S and ST

Case 3.3 –Rotate S in


Opp. to DB

67
Prepared by D.HIMAGIRI
Red Black Tree...
Deletion Example: Delete 50,20,100,90 from the following RB Tree

Case 3.4 –
Case 3.4 -swap color of Rotate P in same
S and P direction of DB

Case 3.4 –Remove


DB and change
color of SL to black

68
Prepared by D.HIMAGIRI
B Tree
 B-Tree is a M-way search tree that can be widely used for disk access.
 One of the main reason of using B tree is its capability to store large number of keys in a
single node by keeping the height of the tree relatively small.
Properties of B-Tree:
1. B-Tree of order M will have atmost M children.
2. Every node in a B-tree except root and the leaf nodes has at least ⌈M/2⌉ children.
3. All leaves are at the same level.
4. All nodes may contain maximum M – 1 keys and minimum of ⌈M/2⌉ -1 keys.
5. All keys of a node are sorted in increasing order. The child between two keys k1 and k2
contains all keys in the range from k1 and k2.
6. The root has at least two children if it is not a leaf node.

69
Prepared by D.HIMAGIRI
B Tree...
Operations :
 Searching: Searching in B-tree is similar to BST

 Insertion:
In a B-Tree, a new element must be added only at the leaf node. That means, the new key
value is always attached to the 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 it into the
tree as a root node.
Step 3 - If tree is Not Empty, then find the suitable leaf node to which the new key value is
added using Binary Search Tree logic.
Step 4 - If that leaf node has empty position, add the new key value to that leaf node in
ascending order of key value within the node.
Step 5 - If that leaf node is already full, split that leaf node by sending middle value to its
parent node. Repeat the same until the sending value is fixed into a node.
Step 6 - If the splitting is performed at root node then the middle value becomes new root
node for the tree and the height of the tree is increased by one.

70
Prepared by D.HIMAGIRI
B Tree...
 Insert the following elements into a B-tree of order 3
1,2,3,4,5,6,7,8,9,10
Max no. of keys =M-1= 3-1=2
Min no. of keys = ⌈M/2⌉ -1 =3/2-1=2-1=1
Min no. of children = ⌈M/2⌉ =3/2=2
Insert 1

Insert 2

Insert 3

71
Prepared by D.HIMAGIRI
B Tree...
 Insert the following elements into a B-tree of order 3
1,2,3,4,5,6,7,8,9,10
Max no. of keys =M-1= 3-1=2
Min no. of keys = ⌈M/2⌉ -1 =3/2-1=2-1=1
Min no. of children = ⌈M/2⌉ =3/2=2
Insert 4

Insert 5

Insert 6

72
Prepared by D.HIMAGIRI
B- Tree...
 Insert the following elements into a B-tree of order 3
1,2,3,4,5,6,7,8,9,10
Max no. of keys =M-1= 3-1=2
Min no. of keys = ⌈M/2⌉ -1 =3/2-1=2-1=1
Min no. of children = ⌈M/2⌉ =3/2=2
Insert 7

Insert 8

73
Prepared by D.HIMAGIRI
B Tree...
 Insert the following elements into a B-tree of order 3
1,2,3,4,5,6,7,8,9,10
Max no. of keys =M-1= 3-1=2
Min no. of keys = ⌈M/2⌉ -1 =3/2-1=2-1=1
Min no. of children = ⌈M/2⌉ =3/2=2
Insert 9

Insert 10

74
Prepared by D.HIMAGIRI
B Tree...
 Insert the following elements into a B-tree of order 3
1,2,3,4,5,6,7,8,9,10
Max no. of keys =M-1= 3-1=2
Min no. of keys = ⌈M/2⌉ -1 =3/2-1=2-1=1
Min no. of children = ⌈M/2⌉ =3/2=2
Finally, B-tree is

75
Prepared by D.HIMAGIRI
B Tree...
 Deletion:
Deleting an element on a B-tree consists of three main events: searching the node where
the key to be deleted exists, deleting the key and balancing the tree if required.
While deleting a tree, a condition called underflow may occur. Underflow occurs when a
node contains less than the minimum number of keys it should hold.
There are three main cases for deletion operation in a B tree.
Case I:The key to be deleted lies in the leaf. There are two cases for it.
i) The deletion of the key does not violate the property of the minimum number of
keys.

76
Prepared by D.HIMAGIRI
B Tree...
ii) The deletion of the key violates the property of the minimum number of keys.
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

77
Prepared by D.HIMAGIRI
B Tree...
iii) If both the immediate sibling nodes already have a minimum number of keys:
then merge the node with either the left sibling node or the right sibling node. This merging is
done through the parent node.

78
Prepared by D.HIMAGIRI
B Tree...
Case II :If the key to be deleted lies in the internal node, the following cases occur.
i) The internal node, which is deleted, is replaced by an Inorder predecessor if the left child
has more than the minimum number of keys.

79
Prepared by D.HIMAGIRI
B Tree...
Case II :If the key to be deleted lies in the internal node, the following cases occur.
ii) If either child has exactly a minimum number of keys then, merge the left and the right
children. After merging if the parent node has less than the minimum number of keys
then, look for the siblings as in Case I.

80
Prepared by D.HIMAGIRI
B Tree...
Case III :In this case, the height of the tree shrinks. If the target key lies in an internal node, and
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 the children
contain a minimum number of keys then, borrowing cannot take place. This leads to Case II(ii)
i.e. merging the children.
Again, look for the sibling to borrow a key. But, if the sibling also has only a minimum number
of keys then, merge the node with the sibling along with the parent. Arrange the children
accordingly (increasing order).

81
Prepared by D.HIMAGIRI
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 internal nodes of B+ tree are often called index nodes.
The leaf nodes of a B+ tree are linked together in the form of a singly linked lists to make the
search queries more efficient.

82
Prepared by D.HIMAGIRI
B+ Tree...
Operations:
Insertion:
Case I
If the leaf is not full, insert the key into the leaf node in increasing order.
Case II
1. If the leaf is full, insert the key into the leaf node in increasing order and balance
the tree in the following way.
2. Break the node at m/2th position.
3. Add m/2th key to the parent node as well.
4. If the parent node is already full, follow steps 2 to 3.
Example:
Construct B+ Tree of order 3 for the elements 5,15, 25, 35, 45.
Max no. of keys =M-1= 3-1=2
Min no. of keys = ⌈M/2⌉ -1 =3/2-1=2-1=1
Min no. of children = ⌈M/2⌉ =3/2=2

83
Prepared by D.HIMAGIRI
B+ Tree...
Construct B+ Tree of order 3 for the elements 5,15, 25, 35, 45.
Max no. of keys =M-1= 3-1=2
Min no. of keys = ⌈M/2⌉ -1 =3/2-1=2-1=1
Min no. of children = ⌈M/2⌉ =3/2=2
Insert 5

Insert 15

Insert 25

84
Prepared by D.HIMAGIRI
B+ Tree...
Construct B+ Tree of order 3 for the elements 5,15, 25, 35, 45.
Max no. of keys =M-1= 3-1=2
Min no. of keys = ⌈M/2⌉ -1 =3/2-1=2-1=1
Min no. of children = ⌈M/2⌉ =3/2=2
Insert 35

Insert 45

85
Prepared by D.HIMAGIRI
B+ Tree...

86
Prepared by D.HIMAGIRI
B+ Tree...
Deletion: (B+ Tree of order 3)
Case I:The key to be deleted is present only at the leaf node not in the indexes (or
internal nodes).
There are two cases for it:
i) There is more than the minimum number of keys in the node. Simply delete the key.
Delete 40:

87
Prepared by D.HIMAGIRI
B+ Tree...
Deletion: (B+ Tree of order 3)
Case I:The key to be deleted is present only at the leaf node not in the indexes (or
internal nodes).
ii) There is an exact minimum number of keys in the node. Delete the key and borrow a key
from the immediate sibling. Minimum from right child through parent.
Delete 5:

88
Prepared by D.HIMAGIRI
B+ Tree...
Deletion: (B+ Tree of order 3)
Case 2:The key to be deleted is present in the internal nodes and leaf node. Then we have to
remove them from the internal nodes and leaf node.
There are the following cases for this situation:
i) If there is more than the minimum number of keys in the node, simply delete the key from
the leaf node and delete the key from the internal node as well.Fill the empty space in the
internal node with the Inorder successor.
Delete 45:

89
Prepared by D.HIMAGIRI
B+ Tree...
ii) This case is similar to Case 2(i) but here, empty space is generated above the immediate parent
node.After deleting the key, merge the empty space with its sibling.
Fill the empty space in the grandparent node with the inorder successor.
Delete 25:

90
Prepared by D.HIMAGIRI
B+ Tree...
Case 3:In this case, the height of the tree gets shrinked. It is a little complicated.Deleting 55 from
the tree below leads to this condition. It can be understood in the illustrations below.
Delete 55:

91
Prepared by D.HIMAGIRI
Comparison between B-Tree and B+ Tree

92

You might also like