10 234trees
10 234trees
10 234trees
COL 106
Shweta Agrawal, Amit Kumar, Dr.
Ilyas Cicekli
Multi-Way Trees
k1 . . . k i-1 k i . . . k M-1
T1 ... Ti ... TM
69
Delete X
Example:
• Delete 10, Delete 44,
Time complexity: O(Mh)=O(h), but h can be O(n)
M-way Search Tree
What we know so far:
• What is an M-way search tree
• How to implement Search, Insert, and Delete
• The time complexity of each of these operations is:
O(Mh)=O(h)
2-3 Tree
Inserting Items
Insert 39
Inserting Items
Insert 38
divide leaf
insert in leaf and move middle result
value up to parent
Inserting Items
Insert 37
Inserting Items
Insert 36
divide leaf
insert in leaf and move middle
value up to parent
overcrowded
node
Inserting Items
... still inserting 36
70
80
Deleting Items
Deleting 70: swap 70 with inorder successor (80)
Deleting Items
Deleting 70: ... get rid of 70
Deleting Items
Result
Deleting Items
Delete 100
Deleting Items
Deleting 100
Deleting Items
Result
Deleting Items
Delete 80
Deleting Items
Deleting 80 ...
Deleting Items
Deleting 80 ...
Deleting Items
Deleting 80 ...
Deleting Items
Final Result
comparison with
binary search tree
Deletion Algorithm I
Deleting item I:
Merging
No sibling has 2 items:
merge node
move item from parent
to sibling
Deletion Algorithm III
Redistribution
Merging
Redistribution not possible:
merge node
move item from parent
to sibling
adopt child of n
1. T is of the form r
TL TR
3-node
where r is a node containing one data item and TL and TR
are both 2-3-4 trees, each of height h-1, or
3. T is of the form r
TL TM TR
where r is a node containing two data items and TL , TM
and TR are 2-3-4 trees, each of height h-1, or
4-node
4. T is of the form r
TL TML TMR TR
where r is a node containing three data items and TL ,
TML , TMR , and TR are 2-3-4 trees, each of height h-1.
• For a 2-3-4 tree, insertion and deletion algorithms that are used for 2-
3 trees, can similarly be used.
• For a 2-3 tree, the insertion algorithm traces a path from the root to a
leaf and then backs up from the leaf as it splits nodes.
• To avoid this return path after reaching a leaf, the insertion algorithm
for a 2-3-4 tree splits 4-nodes as soon as it encounters them on the
way down the tree from the root to a leaf.
– As a result, when a 4-node is split and an item is moved up to node’s parent, the
parent cannot possibly be a 4-node and so can accommodate another item.
Insert[ 20 50 40 70 80 15 90 100 ] to 10 30 60
this 2-3-4 tree
10 30
30
30 60
10 10 20 60
60
Insert 20
• Root is a 4-node Split 4-nodes as they are encountered
• So, we split it before insertion
• And, then add 20
30
10 20 4050 60
50 6060
Insert 50 and 40
• No 4-nodes have been encountered No split operation
during their insertion
30 30 50
10 20 40 40 6050
60 70
60
Insert 70
• A 4-node is encountered
• So, we split it before insertion
• And, then add 70
30 50
1010 15 20
20 40 6060 70 7080
Insert 80 and 15
• No 4-nodes have been encountered No split operation
during their insertion
30 50 70
10 15 20
30 40 60 7080 90
10 15 20 40 60 8080 90 100
Insert 100
• A 4-node is encountered
• So, we split it before insertion
• And, then add 100
• To avoid this return path after reaching a leaf, the deletion algorithm
for a 2-3-4 tree transforms each 2-node into either 3-node or 4-node
as soon as it encounters them on the way down the tree from the root
to a leaf.
– If an adjacent sibling is a 3-node or 4-node, transfer an item from that sibling to
our 2-node.
– If adjacent sibling is a 2-node, merge them.