MultiWay Search Trees Manolis Koubarakis Data Structures and
Multi-Way Search Trees Manolis Koubarakis Data Structures and Programming Techniques 1
Multi-Way Search Trees • Data Structures and Programming Techniques 2
Definitions • Data Structures and Programming Techniques 3
Definitions (cont’d) • Data Structures and Programming Techniques 4
Definitions (cont’d) • Data Structures and Programming Techniques 5
22 5 10 3 4 6 8 25 14 11 13 23 24 27 17 Data Structures and Programming Techniques 6
Proposition • Data Structures and Programming Techniques 7
Proof • Data Structures and Programming Techniques 8
Proof (cont’d) Data Structures and Programming Techniques 9
Proof (cont’d) • Data Structures and Programming Techniques 10
Proof (cont’d) • Data Structures and Programming Techniques 11
Proof (cont’d) • Data Structures and Programming Techniques 12
Proof (cont’d) • Data Structures and Programming Techniques 13
Searching in a Multi-Way Search Tree • Data Structures and Programming Techniques 14
Example Multi-Way Search Tree 22 5 10 3 4 6 8 25 14 11 13 23 24 27 17 Data Structures and Programming Techniques 15
Search for Key 12 22 5 10 3 4 6 8 25 14 11 13 23 24 27 17 Unsuccessful search Data Structures and Programming Techniques 16
Search for Key 24 22 5 10 3 4 6 8 25 14 23 24 27 Successful search 11 13 17 Data Structures and Programming Techniques 17
Insertion in a Multi-Way Search Tree • Data Structures and Programming Techniques 18
22 5 10 3 4 6 8 25 14 23 24 27 Unsuccessful search 11 13 17 Data Structures and Programming Techniques 19
Key 28 Inserted 22 5 10 3 4 6 8 25 14 11 13 23 24 27 28 17 Data Structures and Programming Techniques 20
Insert Key 32 22 5 10 3 4 6 8 25 14 23 24 27 28 Unsuccessful search 11 13 17 Data Structures and Programming Techniques 21
Key 32 Inserted 22 5 10 3 4 6 8 25 14 11 13 23 24 17 Data Structures and Programming Techniques 27 28 32 22
Insert Key 12 22 5 10 3 4 6 8 25 14 11 13 23 24 17 27 28 32 Unsuccessful Search Data Structures and Programming Techniques 23
Key 12 Inserted 22 5 10 3 4 25 14 6 8 11 13 23 24 17 27 28 32 12 Data Structures and Programming Techniques 24
Deletion from a Multi-Way Search Tree • The algorithm for deletion from a multi-way search tree is left as an exercise. Data Structures and Programming Techniques 25
Complexity of Operations • Data Structures and Programming Techniques 26
Efficiency Considerations • We know that maintaining perfect balance in binary search trees yields shortest average search paths, but the attempts to maintain perfect balance when we insert or delete nodes can incur costly rebalancing in which every node of the tree needs to be rearranged. • AVL trees showed us one way to solve this problem by abandoning the goal of perfect balance and adopt the goal of keeping the trees “almost balanced”. Data Structures and Programming Techniques 27
Efficiency Considerations (cont’d) • Data Structures and Programming Techniques 28
Balanced Multi-way Search Trees • We will study two kinds of balanced multi-way search trees: – 2 -3 trees – 2 -3 -4 trees or (2, 4) trees. Data Structures and Programming Techniques 29
2 -3 Trees • A 2 -3 tree is a multi-way search tree which has the following properties: • Size property: Each internal node contains one or two entries, and has either two or three children. • Depth property: All leaves of the tree are empty trees that have the same depth (lie on a single bottom level). Data Structures and Programming Techniques 30
Example of 2 -3 Tree H J D A E F I Data Structures and Programming Techniques K N L O P 31
Searching in 2 -3 Trees • To search for the key L, for example, we start at the root and since L > H, we follow the pointer to the right subtree of the root node. • Now we note that L lies between J and N, so we follow the middle pointer between the nodes J and N to the node containing the keys K and L. • L is found and the search terminates. Data Structures and Programming Techniques 32
Search for Key L H J D A E F I Data Structures and Programming Techniques K N L O P 33
Insertion of New Keys • Suppose we want to insert the new key B into the tree. • Since B<H, we follow the left pointer from the root node to the node containing D in the second row. • Then we follow the left pointer of D’s node to the node containing A. • Since B>A, we follow the right pointer of A’s node which lead us to an empty tree (an external node). • Then we go back to the parent of the external node and try to store the new key there. • The node containing A has room for one more key so we store key B there. We also add a new empty child to this node. Data Structures and Programming Techniques 34
Example: Insert B H J D A E F I Data Structures and Programming Techniques K N L O P 35
Example: Insert B H J D A E F I Data Structures and Programming Techniques K N L O P 36
Example: Result H J D A B E F I Data Structures and Programming Techniques K N L O P 37
Insertion of New Keys (cont’d) • Let us now insert key M. This leads to the attempt to add M to the node containing K and L which now overflows with keys K, L and M. • The strategy for such cases is to split the overflowed node into two nodes and pass the middle key to the parent. • Hence we split the overflowed node into two new nodes containing K and M respectively. • We also pass the middle key L to the parent node containing J and N. Data Structures and Programming Techniques 38
Insertion of New Keys (cont’d) • The attempt to add L to this parent node results in a new overflowed node in which the key L lies between keys J and N. • So we split this parent node into new nodes containing J and N respectively, and we pass the middle key L up to the root. • The root has room for L so we store it there. Data Structures and Programming Techniques 39
Example: Insert M H J D A B E F I Data Structures and Programming Techniques K N L O P 40
Example: Insert M (cont’d) H J D A B E F I Data Structures and Programming Techniques K N L M overflows this node O P 41
Example: Insert M (cont’d) H J D N L A B E F I K M The node is split in two and L is passed Data Structures and Programming to the parent node Techniques O P 42
Example: Insert M (cont’d) H J D L overflows this node N L A B E F I Data Structures and Programming Techniques K M O P 43
Example: Insert M (cont’d) L H D A B E The node is split in two and L is passed up to the parent N J F I K Data Structures and Programming Techniques M O P 44
Example: Result H D A B E L is inserted in the root node L N J F I K Data Structures and Programming Techniques M O P 45
Insertion of New Keys (cont’d) • Let us now insert key Q. Q should be entered in the node containing O and P which now overflows. • Thus, this node is split up to two nodes one containing O and the other containing R, and the middle key is passed up towards the parent node. • The parent node has only key N so there is space for P and it is inserted there. Data Structures and Programming Techniques 46
Example: Insert Q H D A B E L N J F I K Data Structures and Programming Techniques M O P Q overflows this node 47
Example: Insert Q (cont’d) H D L N J P A B E F I K Data Structures and Programming Techniques M O QP This node is split up and P is passed up 48
Example: Result H D A B E L N P J F I K Data Structures and Programming Techniques M O QP 49
Insertion of New Keys (cont’d) • Let us now insert key R. R is inserted in the node with Q where there is space. Data Structures and Programming Techniques 50
Example: Insert R H D A B E L N P J F I K Data Structures and Programming Techniques M O QP R 51
Inserting New Keys (cont’d) • Let us now insert key S. S should be inserted in the node with Q and R. • This node overflows. Thus, it is split into two nodes one containing Q and the other containing S and R is passed up to the parent node. • R now oveflows this node where N and P are also stored. Thus, this node is split into two nodes one containing N and the other containing R and the middle key P is sent up to the parent (the root). Data Structures and Programming Techniques 52
Inserting New Keys (cont’d) • The root now overflows with the addition of P so it is split into two nodes one containing H and the other containing P and the middle key L is used to create a new root. • Thus, we have added one more level to the tree. Data Structures and Programming Techniques 53
Example: Insert S H D A B E L N P J F I K Data Structures and Programming Techniques M O QP R S overflows this node 54
Example: Insert S (cont’d) H D A B E L N P J F I K Data Structures and Programming Techniques M O R Q S This node is split and R is sent up 55
Example: Insert S (cont’d) H L R overflows this node D A B E N P J F I K Data Structures and Programming Techniques M O R Q S 56
Example: Insert S (cont’d) H D A B E P L I R N J F This node is split up and P is sent up K Data Structures and Programming Techniques M O Q S 57
Example: Insert S (cont’d) P overflows the root H D A B E P L F I R N J K Data Structures and Programming Techniques M O Q S 58
Example: Result The root splits and L becomes the new root L H D A B P J E F I R N K Data Structures and Programming Techniques M O Q S 59
Complexity of Insertion in 2 -3 Trees • Data Structures and Programming Techniques 60
(2, 4) Trees • A (2, 4) tree or 2 -3 -4 tree is a multi-way search tree which has the following two properties: – Size property: Every internal node contains at least one and at most three keys, and has at least two and at most four children. – Depth property: All the external nodes are empty trees that have the same depth (lie on a single bottom level). Data Structures and Programming Techniques 61
Result • Data Structures and Programming Techniques 62
Result (cont’d) • Data Structures and Programming Techniques 63
Result (cont’d) • Data Structures and Programming Techniques 64
Insertion in (2, 4) Trees • Data Structures and Programming Techniques 65
Insertion (cont’d) • Data Structures and Programming Techniques 66
Dealing with Overflow Nodes • Data Structures and Programming Techniques 67
Overflow at a 5 -node Data Structures and Programming Techniques 68
Data Structures and Programming Techniques 69
Data Structures and Programming Techniques 70
Example • Let us now see an example of a few insertions into an initially empty (2, 4) tree. Data Structures and Programming Techniques 71
Insert 4 4 Data Structures and Programming Techniques 72
Insert 6 4 6 Data Structures and Programming Techniques 73
Insert 12 4 6 12 Data Structures and Programming Techniques 74
Insert 15 - Overflow 4 6 12 15 Data Structures and Programming Techniques 75
Creation of New Root Node 12 4 6 15 Data Structures and Programming Techniques 76
Split 12 4 6 15 Data Structures and Programming Techniques 77
Insert 3 12 3 4 6 15 Data Structures and Programming Techniques 78
Insert 5 - Overflow 12 3 4 5 6 15 Data Structures and Programming Techniques 79
5 is Sent to the Parent Node 12 5 3 4 6 15 Data Structures and Programming Techniques 80
Split 5 12 3 4 6 Data Structures and Programming Techniques 15 81
Insert 10 5 12 3 4 6 10 Data Structures and Programming Techniques 15 82
Insert 8 5 12 3 4 6 8 10 Data Structures and Programming Techniques 15 83
Insertion (cont’d) • Let us now see a more complicated example of insertion in a (2, 4) tree. Data Structures and Programming Techniques 84
Initial Tree 5 10 12 3 4 6 8 11 13 14 15 Data Structures and Programming Techniques 85
Insert 17 - Overflow 5 10 12 3 4 6 8 11 13 14 15 17 Data Structures and Programming Techniques 86
15 is Sent to the Parent Node 5 10 12 3 4 6 8 11 15 13 14 17 Data Structures and Programming Techniques 87
Split 5 10 12 15 3 4 6 8 11 13 14 Data Structures and Programming Techniques 17 88
Overflow at the Root 5 10 12 15 3 4 6 8 11 13 14 Data Structures and Programming Techniques 17 89
Creation of New Root 12 5 10 15 3 4 6 8 11 13 14 Data Structures and Programming Techniques 17 90
Split 12 15 5 10 3 4 6 8 11 13 14 Data Structures and Programming Techniques 17 91
Final Tree 12 15 5 10 3 4 6 8 11 13 14 Data Structures and Programming Techniques 17 92
Complexity Analysis of Insertion • Data Structures and Programming Techniques 93
Removal in (2, 4) Trees • Data Structures and Programming Techniques 94
Removal (cont’d) • Data Structures and Programming Techniques 95
Removal (cont’d) • Data Structures and Programming Techniques 96
Examples • Let us now see some examples of removal from a (2, 4) tree. Data Structures and Programming Techniques 97
Initial Tree 12 15 5 10 4 6 8 11 13 14 Data Structures and Programming Techniques 17 98
Remove 4 12 4 15 5 10 6 8 11 13 14 Data Structures and Programming Techniques 17 99
Transfer 12 15 10 5 6 8 11 13 14 Data Structures and Programming Techniques 17 100
After the Transfer 12 5 15 6 10 8 11 13 14 Data Structures and Programming Techniques 17 101
Remove 12 12 5 15 6 10 8 11 13 14 Data Structures and Programming Techniques 17 102
Remove 12 12 6 10 5 8 15 11 13 14 Data Structures and Programming Techniques 17 103
11 15 6 10 5 8 13 14 Data Structures and Programming Techniques 17 104
After the Fusion 11 6 5 8 10 15 13 14 Data Structures and Programming Techniques 17 105
Remove 13 11 6 15 13 5 8 10 Data Structures and Programming Techniques 14 17 106
After the Removal of 13 11 6 5 8 10 Data Structures and Programming Techniques 15 14 17 107
Remove 14 - Underflow 11 6 15 14 5 8 10 Data Structures and Programming Techniques 17 108
Fusion 11 6 5 8 10 Data Structures and Programming Techniques 15 17 109
11 6 5 8 10 Data Structures and Programming Techniques 15 17 110
Fusion 6 5 11 8 10 Data Structures and Programming Techniques 15 17 111
Remove the Root 6 11 5 8 10 Data Structures and Programming Techniques 15 17 112
Final Tree 6 11 5 8 10 Data Structures and Programming Techniques 15 17 113
Complexity of Removal • Data Structures and Programming Techniques 114
Readings • T. A. Standish. Data Structures, Algorithms and Software Principles in C. – Section 9. 9 • M. T. Goodrich, R. Tamassia and D. Mount. Data Structures and Algorithms in C++. – Section 10. 4 • R. Sedgewick. Αλγόριθμοι σε C. 3η Αμερικανική Έκδοση. Εκδόσεις Κλειδάριθμος. – Section 13. 3 Data Structures and Programming Techniques 115
- Slides: 115