CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has...
Transcript of CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has...
![Page 1: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/1.jpg)
CMSC 132: Object-Oriented Programming II
2-3-4 Tree
1CMSC 132 Summer 2016
![Page 2: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/2.jpg)
2-3-4 Tree
Self-balancing treeevery internal node has either two, three, or four child nodes.• a 2-node has one data element, and if internal has two
child nodes;• a 3-node has two data elements, and if internal has three
child nodes;• a 4-node has three data elements, and if internal has four
child nodes.
CMSC 132 Summer 2016 2
![Page 3: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/3.jpg)
2-3-4 Tree Properties
Every node (leaf or internal) is a 2-node, 3-node or a 4-node, and holds one, two, or three data elements, respectively.All leaves are at the same depth (the bottom level).All data is kept in sorted order.Tree height.• Worst case: lg N [all 2-nodes]• Best case: log4 N = 1/2 lg N [all 4-nodes]• Between 10 and 20 for 1 million nodes.• Between 15 and 30 for 1 billion nodes.
Guaranteed logarithmic performance for both search and insert.
CMSC 132 Summer 2016 3
![Page 4: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/4.jpg)
2-3-4 Tree Insertion
1. If the current node is a 4-node:• Remove and save the middle value to get a 3-node.• Split the remaining 3-node up into a pair of 2-nodes (the now
missing middle value is handled in the next step).• If this is the root node (which thus has no parent):
• the middle value becomes the new root 2-node and the tree height increases by 1. Ascend into the root.
• Otherwise, push the middle value up into the parent node. Ascend into the parent node.
2. Find the child whose interval contains the value to be inserted.3. If that child is a leaf, insert the value into the child node and finish.
• Otherwise, descend into the child and repeat from step 1
CMSC 132 Summer 2016 4
![Page 5: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/5.jpg)
2-3-4 Tree Example: Insertion
112
8 2
25 6
14 28 17
7 52 16 48 68
3 26 29 53 55 45
1
CMSC 132 Summer 2016 5
Insert 1
![Page 6: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/6.jpg)
2-3-4 Tree Example: Insertion
1 12
82
25 6
14 28 17
7 52 16 48 68
3 26 29 53 55 45
1281
CMSC 132 Summer 2016 6
Insert 12,8
![Page 7: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/7.jpg)
1 12
8 2
25 6
14 28 17
7 52 16 48 68
3 26 29 53 55 45
1281
Insert 2
2-3-4 Tree Example: Insertion
CMSC 132 Summer 2016 7
![Page 8: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/8.jpg)
12
8
1 2
1 12
8 2
25 6
14 28 17
7 52 16 48 68
3 26 29 53 55 45
1281
Insert 2
2-3-4 Tree Example: Insertion
CMSC 132 Summer 2016 8
4-node splits
![Page 9: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/9.jpg)
1 12
8 2
256
14 28 17
7 52 16 48 68
3 26 29 53 55 45
2-3-4 Tree Example: Insertion
12
8
1 2
Insert 25
CMSC 132 Summer 2016 9
![Page 10: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/10.jpg)
1 12
8 2
256
14 28 17
7 52 16 48 68
3 26 29 53 55 45
12
8
1 2 25
2-3-4 Tree Example: Insertion
12
8
1 2
Insert 25
CMSC 132 Summer 2016 10
![Page 11: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/11.jpg)
1 12
8 2
256
14 28 17
7 52 16 48 68
3 26 29 53 55 45
2-3-4 Tree Example: Insertion
Insert 612
8
1 2 25
CMSC 132 Summer 2016 11
![Page 12: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/12.jpg)
1 12
8 2
256
14 28 17
7 52 16 48 68
3 26 29 53 55 45
2-3-4 Tree Example: Insertion
Insert 6
12
8
1 2 25612
8
1 2 25
CMSC 132 Summer 2016 12
![Page 13: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/13.jpg)
12
8
1 2
1 12
8 2
256
1428 17
7 52 16 48 68
3 26 29 53 55 45
25
2-3-4 Tree Example: Insertion
Insert 14
6
CMSC 132 Summer 2016 13
![Page 14: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/14.jpg)
12
8
1 2
1 12
8 2
256
1428 17
7 52 16 48 68
3 26 29 53 55 45
25
2-3-4 Tree Example: Insertion
Insert 14
6 12
8
1 2 146 25
CMSC 132 Summer 2016 14
![Page 15: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/15.jpg)
12
8
1 2
1 12
8 2
256
142817
7 52 16 48 68
3 26 29 53 55 45
25
2-3-4 Tree Example: Insertion
Insert 28
6 14
CMSC 132 Summer 2016 15
![Page 16: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/16.jpg)
12
8
1 2
1 12
8 2
256
142817
7 52 16 48 68
3 26 29 53 55 45
25
2-3-4 Tree Example: Insertion
Insert 28
6 12
8
1 2
14
6 25 2814
CMSC 132 Summer 2016 16
4-node splits, middle node ascends to parent
![Page 17: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/17.jpg)
1 12
8 2
256
142817
7 52 16 48 68
3 26 29 53 55 45
2-3-4 Tree Example: Insertion
Insert 17
12
8
1 2
14
6 25 28
CMSC 132 Summer 2016 17
![Page 18: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/18.jpg)
1 12
8 2
256
142817
7 52 16 48 68
3 26 29 53 55 45
2-3-4 Tree Example: Insertion
Insert 7
12
8
1 2
14
6 25 28 12
8
1 2
14
6 25 2817
CMSC 132 Summer 2016 18
![Page 19: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/19.jpg)
1 12
8 2
256
142817
752 16 48 68
3 26 29 53 55 45
2-3-4 Tree Example: Insertion
Insert 7
12
8
1 2
14
6 252817
CMSC 132 Summer 2016 19
![Page 20: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/20.jpg)
1 12
8 2
256
142817
752 16 48 68
3 26 29 53 55 45
2-3-4 Tree Example: Insertion
Insert 7
12
8
1 2
14
6 252817
12
8
1
2 14
6 2528177
CMSC 132 Summer 2016 20
4-node splits, middle node ascends to parent
![Page 21: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/21.jpg)
1 12
8 2
256
142817
75216 48 68
3 26 29 53 55 45
2-3-4 Tree Example: Insertion
Insert 52
12
8
1
2 14
6 25 28177
CMSC 132 Summer 2016 21
![Page 22: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/22.jpg)
1 12
8 2
256
142817
75216 48 68
3 26 29 53 55 45
2-3-4 Tree Example: Insertion
Insert 52
12
8
1
2 14
6 25 28177
12
8
1
2 14
6
25
28177 52 12
8
1
2 14
6
25
28177 52
CMSC 132 Summer 2016 22
![Page 23: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/23.jpg)
1 12
8 2
256
142817
7521648 68
3 26 29 53 55 45
2-3-4 Tree Example: Insertion
Insert 16
12
8
1
2 14
6
25
28177 52
CMSC 132 Summer 2016 23
![Page 24: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/24.jpg)
1 12
8 2
256
142817
7521648 68
3 26 29 53 55 45
2-3-4 Tree Example: Insertion
Insert 16
12
8
1
2 14
6
25
28177 52
12
8
1
2 14
6
25
28177 5216
CMSC 132 Summer 2016 24
![Page 25: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/25.jpg)
1 12
8 2
256
142817
752164868
3 26 29 53 55 45
2-3-4 Tree Example: Insertion
Insert 48
12
8
1
2 14
6
25
28177 5216
CMSC 132 Summer 2016 25
![Page 26: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/26.jpg)
1 12
8 2
256
142817
752164868
3 26 29 53 55 45
2-3-4 Tree Example: Insertion
Insert 48
12
8
1
2 14
6
25
28177 5216
12
8
1
2 14
6
25
28177 4816 52
CMSC 132 Summer 2016 26
![Page 27: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/27.jpg)
1 12
8 2
256
142817
752164868
3 26 29 53 55 45
2-3-4 Tree Example: Insertion
Insert 68
12
8
1
2 14
6
25
28177 4816 52
CMSC 132 Summer 2016 27
![Page 28: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/28.jpg)
1 12
8 2
256
142817
752164868
3 26 29 53 55 45
2-3-4 Tree Example: Insertion
Insert 68
12
8
1
2 14
6
25
28177 4816 52
12
8
1
2 14
6
25
28177
48
16 52 68
CMSC 132 Summer 2016 28
![Page 29: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/29.jpg)
1 12
8 2
256
142817
752164868
3 2629 53 55 45
2-3-4 Tree Example: Insertion
Insert 3, 26
12
8
1
2 14
6
25
28177
48
16 52 68
CMSC 132 Summer 2016 29
![Page 30: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/30.jpg)
1 12
8 2
256
142817
752164868
3 2629 53 55 45
2-3-4 Tree Example: Insertion
Insert 3, 26
12
8
1
2 14
6
25
28177
48
16 52 68
12
8
1
2 14
6
25
28177
48
16 52 683 26
CMSC 132 Summer 2016 30
![Page 31: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/31.jpg)
1 12
8 2
256
142817
752164868
3 2629 535545
2-3-4 Tree Example: Insertion
Insert 55
12
8
1
2 14
6
25
28177
48
16 52 683 26 5329
CMSC 132 Summer 2016 31
![Page 32: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/32.jpg)
1 12
8 2
256
142817
752164868
3 2629 535545
2-3-4 Tree Example: Insertion
Insert 55
12
8
1
2 14
6
25
28177
48
16 52 683 26 5329
12
8
1
2 14
6
25
28177
48
16 52 683 26
53
29 55
CMSC 132 Summer 2016 32
![Page 33: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/33.jpg)
1 12
8 2
256
142817
752164868
3 2629 535545
2-3-4 Tree Example: Insertion
Insert 45
12
8
1
2 14
6
25
28177
48
16 52 683 26
53
29 55
CMSC 132 Summer 2016 33
![Page 34: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/34.jpg)
1 12
8 2
256
142817
752164868
3 2629 535545
2-3-4 Tree Example: Insertion
Insert 45
12
8
1
2 14
6
25
28177
48
16 52 683 26
53
29 55
12
8
1
2 14
6
25
28
177
48
16 52 683 26
53
29 5545
CMSC 132 Summer 2016 34
![Page 35: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/35.jpg)
2-3-4 Tree: Delete• Leaf:
• Just delete the key• Make sure that a leaf is not empty after
deleting a key
Delete 2
CMSC 132 Summer 2016 35
![Page 36: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/36.jpg)
2-3-4 Tree: Delete• Leaf:
• When key deletion would create an empty leaf, borrow a key from leaf 's immediate siblings (i.e. to the left and then right).
CMSC 132 Summer 2016 36
![Page 37: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/37.jpg)
2-3-4 Tree: Delete
• Leaf:• If siblings are 2-nodes (no immediate sibling from
which to borrow a key), steal a key from our parent by doing the opposite of a split.
Delete 6
CMSC 132 Summer 2016 37
![Page 38: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/38.jpg)
2-3-4 Tree: Delete
• What if parent is a 2-node (one key)?
CMSC 132 Summer 2016 38
![Page 39: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/39.jpg)
2-3-4 Tree: Delete• What if parent is a 2-node (one key)?
• Steal from siblings (parent’s)• Merge
CMSC 132 Summer 2016 39
![Page 40: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/40.jpg)
2-3-4 Tree: Delete• What if parent is a 2-node (one key)?
• Steal from siblings (parent’s)• Merge
CMSC 132 Summer 2016 40
![Page 41: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/41.jpg)
2-3-4 Tree: Delete• Internal Node:
• Delete the predecessor, and swap it with the node to be deleted.
Delete 5: first delete 4, then swap 4 for 5.
CMSC 132 Summer 2016 41
![Page 42: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/42.jpg)
2-3-4 Tree: Delete• Internal Node:
• Delete the predecessor, and swap it with the node to be deleted.
• Key to delete may move.
Delete 2: first delete 1, then swap 1 for 2.
CMSC 132 Summer 2016 42
![Page 43: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/43.jpg)
2-3-4 Tree Example: Delete
Delete 3,17,55
12
8
1
2 14
6
25
28
177
48
16 52 683 26
53
29 5545
CMSC 132 Summer 2016 43
![Page 44: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/44.jpg)
2-3-4 Tree Example: Delete
12
8
1
2 14
6
25
28
7
48
16 52 6826
53
29 45
Delete 1: borrow from siblings (rotate)
CMSC 132 Summer 2016 44
![Page 45: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/45.jpg)
2-3-4 Tree Example: Delete
Delete 1
12
8
2
6 14
25
28
7
48
16 5226
53
29 45 68
CMSC 132 Summer 2016 45
![Page 46: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/46.jpg)
2-3-4 Tree Example: Delete
Delete 52: borrow from sibling
12
8
2
6 14
25
28
7
48
16 5226
53
29 45 68
CMSC 132 Summer 2016 46
![Page 47: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/47.jpg)
2-3-4 Tree Example: Delete
Delete 52: borrow from sibling
12
8
2
6 14
25
28
7
45
16 4826
53
29 68
CMSC 132 Summer 2016 47
![Page 48: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/48.jpg)
2-3-4 Tree Example: Delete
Delete 48: borrow from parent
12
8
2
6 14
25
28
7
45
16 4826
53
29 68
CMSC 132 Summer 2016 48
![Page 49: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/49.jpg)
2-3-4 Tree Example: Delete
Delete 48: borrow from parent
12
8
2
6 14
25
28
7 4516 26
53
29 68
CMSC 132 Summer 2016 49
![Page 50: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/50.jpg)
2-3-4 Tree Example: Delete
Delete 2: borrow from parent, and parent
12
8
2
6 14
25
28
7 4516 26
53
29 68
merge
CMSC 132 Summer 2016 50
![Page 51: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/51.jpg)
2-3-4 Tree Example: Delete
Delete 2: borrow from parent, and parent
126
8 14
25
28
7 4516 26
53
29 68
CMSC 132 Summer 2016 51
![Page 52: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/52.jpg)
2-3-4 Tree Example: Delete
Delete 14: delete 12, swap 12 for 14
126
8 14
25
28
7 4516 26
53
29 68
CMSC 132 Summer 2016 52
![Page 53: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/53.jpg)
2-3-4 Tree Example: Delete
Delete 14: delete 12, swap 12 for 14
86
7 12
25
28
4516 26
53
29 68
CMSC 132 Summer 2016 53
![Page 54: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/54.jpg)
2-3-4 Tree Example: Delete
Delete 25: delete 16, swap 16 for 25
86
7 12
25
28
4516 26
53
29 68
CMSC 132 Summer 2016 54
![Page 55: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/55.jpg)
2-3-4 Tree Example: Delete
Delete 25: delete 16, swap 16 for 25
86
7
12
16
28
4526
53
29 68
CMSC 132 Summer 2016 55
![Page 56: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/56.jpg)
Represent 2-3-4 tree as a BST
Use "internal" red edges for 3- and 4- nodes.Require that 3-nodes be left-leaning.
CMSC 132 Summer 2016 56
![Page 57: CMSC 132: Object-Oriented Programming II · 2-3-4 Tree Self-balancing tree every internal node has either two, three, or four child nodes. •a 2-node has one data element, and if](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ed0283e137588523c69a8b1/html5/thumbnails/57.jpg)
Represent 2-3-4 tree as a BST
Elementary BST search worksEasy-to-maintain 1-1 correspondence with 2-3-4 treesTrees therefore have perfect black-link balance
CMSC 132 Summer 2016 57