cmsc 132: object-oriented programming ii · 2-3-4 tree self-balancing tree every internal node has...
TRANSCRIPT
CMSC 132: Object-Oriented Programming II
2-3-4 Tree
1CMSC 132 Summer 2016
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
2-3-4 Tree: Delete
• What if parent is a 2-node (one key)?
CMSC 132 Summer 2016 38
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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