2. Topics of Unit 2
• Binary search trees
• Tree traversal (Inorder, postorder, preorder)
• Height Balanced tree
• AVL tree
Splay tree
• B-Tree of order m
• Heap tree
• Binomial Heap
• Red-black tree
3. B-tree of order m
• In search trees like binary search tree, AVL Tree, Red-Black
tree, etc., every node contains only one value (key) and a
maximum of two children.
• But there is a special type of search tree called B-Tree in which
a node contains more than one value (key) and more than two
children.
• The number of keys in a node and number of children for a node
depends on the order of B-Tree.
• Every B-Tree has an order.
B-Tree is a self-balanced search tree in which every node contains multiple
keys and has more than two children.
4. B-tree of order m
B-Tree of Order m has the following properties...
Property #1 - All leaf nodes must be at same level.
Property #2 - All nodes except root must have at least [m/2]-1 keys
and maximum of m-1 keys.
Property #3 - All non leaf nodes except root (i.e. all internal nodes)
must have at least m/2 children.
Property #4 - If the root node is a non leaf node, then it must
have atleast 2 children.
Property #5 - A non leaf node with n-1 keys must have n number of
children.
Property #6 - All the key values in a node must be in Ascending
Order.
5. Example: B-tree of order 4
B-Tree of Order 4 has the following properties...
Property #1 - All leaf nodes must be at same level.
Property #2 - All nodes except root must have at least [m/2]-1=[4/2]-
1=1 keys and maximum of m-1 = 4-1= 3 keys.
Property #3 - All non leaf nodes except root (i.e. all internal nodes)
must have at least m/2=4/2=2 children.
Property #4 - If the root node is a non leaf node, then it must
have atleast 2 children.
Property #5 - A non leaf node with n-1=4-1=3 keys must
have n=4 number of children.
Property #6 - All the key values in a node must be in Ascending
Order.
6. Example: B-tree of order 4
Operations on a B-Tree:
1.Search
2.Insertion
3.Deletion
7. B-tree of order m –Search operation
In a B-Tree, the search operation is performed with O(log n) time complexity.
The search operation is performed as follows...
Step 1 - Read the search element from the user.
Step 2 - Compare the search element with first key value of root node in the tree.
Step 3 - If both are matched, then display "Given node is found!!!" and terminate the function
Step 4 - If both are not matched, then check whether search element is smaller or larger than that key
value.
Step 5 - If search element is smaller, then continue the search process in left subtree.
Step 6 - If search element is larger, then compare the search element with next key value in the same
node and repeat steps 3, 4, 5 and 6 until we find the exact match or until the search element is compared
with last key value in the leaf node and follow the right subtree if element larger, repeat the steps 3, 4, 5
and 6.
Step 7 - If the last key value in the leaf node is also not matched then display "Element is not found" and
terminate the function.
8. B-tree of order m – Insert operation
In a B-Tree, a new element must be added only at the leaf node. That means, the
new keyValue 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(ceil(m/2)) 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.
9. B-tree of order m – Delete operation
There are three cases of deletion:
Case 1: A leaf node has to be deleted.
Case 2: An internal node has to be deleted.
Case 3: A root node to be deleted.
Deleting item I:
1. Locate node n, which contains item I
2. If node n is not a leaf, swap I with in-order successor; deletion always begins at a
leaf
3. If leaf node n contains another item, just delete item I (ceil(m/2)-1)
else try to redistribute nodes from siblings
if not possible, merge node
10. B-tree of order m – Delete operation
There are three cases of deletion:
Case 1: A leaf node has to be deleted.
Case 2: An internal node has to be deleted.
Case 3: A root node to be deleted.
Case – I:
Redistribution:
A sibling has 2 items:
redistribute item between siblings and parent
Merging:
No sibling has 2 items:
merge node
move item from parent to sibling
11. B-tree of order m – Delete operation
There are three cases of deletion:
Case 1: A leaf node has to be deleted.
Case 2: An internal node has to be deleted.
Case 3: A root node to be deleted.
Case – II:
Redistribution:
Internal node n has no item left
redistribute
Merging:
Redistribution not possible:
merge node
move item from parent to sibling
adopt child of empty node.
12. B-tree of order m – Delete operation
There are three cases of deletion:
Case 1: A leaf node has to be deleted.
Case 2: An internal node has to be deleted.
Case 3: A root node to be deleted.
Case – III:
If merging process reaches the root and root is without item
delete root
13. B-tree of order m – Delete operation
Deletion Operation:
1. If search key K
2. If key K found at non-leaf node,
1. Swap with its inorder predecessor or successor element
3.If key K found at leaf node L,
i.Delete key K from node L
ii.If remaining keys at L or P >= ⌈m/2⌉-1
A. Do nothing
iii.Else
A.Check total keys at left sibling / right sibling node J
B.If total keys at J > ⌈m/2⌉ - 1
a.Perform Redistribution Operation
C.Else
a.Perform Merging Operation
D.After redistribution or merging operation cont. with step-ii for parent
node P.
14. B-tree of order m – Delete operation
Example: Construct B-tree of order 4 using given data.
11, 45, 34, 67, 78, 89, 90, 75, 110, 95
Delete: 90, 110, 75, 89
15. Heap Tree
• Heap data structure is a specialized binary tree-based data structure.
• Heap is a binary tree with special characteristics.
• In a heap data structure, nodes are arranged based on their values.
• A heap data structure some times also called as Binary Heap.
There are two types of heap data structures:
1.Max Heap
2.Min Heap
16. Heap Tree
Every heap data structure has the following properties:
Property #1 (Ordering): Nodes must be arranged in an order according to their
values based on Max heap or Min heap.
Property #2 (Structural): All levels in a heap must be full except the last level and
all nodes must be filled from left to right strictly.
Structural as well as max heap
17. Heap Tree
Max-Heap: In a Max-Heap the key present at the root node must be greatest among
the keys present at all of it’s children. The same property must be recursively true for
all sub-trees in that Binary Tree.
Min-Heap: In a Min-Heap the key present at the root node must be minimum among
the keys present at all of it’s children. The same property must be recursively true for
all sub-trees in that Binary Tree.
Max Heap sequence: 100, 40, 50, 10,
15, 50, 40
Min Heap sequence: 10, 15, 30, 40,
50, 100, 40
Heapify method rearranges the
elements of an array where the left and
right sub-tree of ith element obeys the
heap property.
18. Heap Tree
Properties:
Parent node location of child node k = floor(k/2)
Left child node location for parent node k = 2*k
Right child node location for parent node k = 2*k + 1
Max Heap sequence: 100, 40, 50, 10,
15, 50, 40
Min Heap sequence: 10, 15, 30, 40,
50, 100, 40
19. Heap Tree – Max Heap
Insertion:
Step 1 − Create a new node at the end of heap.
Step 2 − Assign new value to the node.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them. (heapify)
Step 5 − Repeat step 3 & 4 until Heap property holds.
Deletion:
Deletion in Max Heap always happens at the root to remove the Maximum value.
Step 1 − Remove root node.
Step 2 − Move the last element of last level to root.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them. (heapify)
Step 5 − Repeat step 3 & 4 until Heap property holds.
Finding Maximum Value Operation in Max Heap:
In a max heap, the root node has the maximum value than all other nodes. So,
directly we can display root node value as the maximum value in max heap.
20. Heap Tree – Min Heap
Insertion:
Step 1 − Create a new node at the end of heap.
Step 2 − Assign new value to the node.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is greater than child, then swap them. (heapify)
Step 5 − Repeat step 3 & 4 until Heap property holds.
Deletion:
Deletion in Min Heap always happens at the root to remove the Minimum value.
Step 1 − Remove root node.
Step 2 − Move the last element of last level to root.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is greater than child, then swap them. (heapify)
Step 5 − Repeat step 3 & 4 until Heap property holds.
Finding Minimum Value Operation in Min Heap:
In a min heap, the root node has the minimum value than all other nodes. So, directly
we can display root node value as the minimum value in min heap.