Search Trees COMPSCI 355 Fall 2016
2-4 Trees Search Trees AVL trees Red-Black trees Splay trees Multiway Search Trees (2, 4) Trees External Search Trees (optimized for reading and writing large blocks) B trees B+ trees
Red-Black Trees Same asymptotic complexity as AVL and 2-4 Trees. O(1) time for trinode restructuring O(log n) time to search, insert, delete O(log n) recoloring operations per update But at most 2 structural operations per update. More popular than AVL and 2-4 trees in practice.
Defining Properties Root is black. Every leaf is black. Children of a red node are black. Every leaf has the same black depth. 5 3 10 13 17 4 7 11 14 6 8
Red-Black 2-4 Collapse each red child into its parent. Height of red-black tree with n nodes is at most 2lg(n+1). 5 3 10 13 17 4 7 11 14 6 8
Red-Black 2-4 Collapse each red child into its parent. Height of red-black tree with n nodes is at most 2lg(n+1). 5 3 4 10 13 17 7 11 14 6 8
Red-Black 2-4 Collapse each red child into its parent. Height of red-black tree with n nodes is at most 2lg(n+1). 5 3 4 10 13 17 7 11 14 6 8
Red-Black 2-4 Collapse each red child into its parent. Height of red-black tree with n nodes is at most 2lg(n+1). 5 3 4 10 13 14 17 7 11 6 8
Red-Black 2-4 Collapse each red child into its parent. Height of red-black tree with n nodes is at most 2lg(n+1). 5 3 4 10 13 14 17 7 11 6 8
Red-Black 2-4 Collapse each red child into its parent. Height of red-black tree with n nodes is at most 2lg(n+1). 5 3 4 10 13 14 17 6 7 8 11
Red-Black 2-4 Collapse each red child into its parent. Height of red-black tree with n nodes is at most 2lg(n+1). 5 3 4 10 13 14 17 6 7 8 11
Red-Black 2-4 Collapse each red child into its parent. Height of red-black tree with n nodes is at most 2lg(n+1). 5 10 3 4 6 7 8 11 13 14 17
2-4 Red-Black Color all nodes black. Keep the children of a 2-node black. 50 70 20 30 55 60 65 80 25 40 53 56 63 68 69 75 90
2-4 Red-Black Color all nodes black. Keep the children of a 2-node black. Split up 3-nodes and 4-nodes. 50 70 20 30 55 60 65 80 25 40 53 56 63 68 69 75 90
2-4 Red-Black Color all nodes black. Keep the children of a 2-node black. Split up 3-nodes and 4-nodes. 50 70 20 55 60 65 80 30 53 56 63 68 69 75 90 25 40
2-4 Red-Black Color all nodes black. Keep the children of a 2-node black. Split up 3-nodes and 4-nodes. 50 70 20 55 60 65 80 30 53 56 63 68 69 75 90 25 40
2-4 Red-Black Color all nodes black. Keep the children of a 2-node black. Split up 3-nodes and 4-nodes. 50 70 20 60 80 30 55 65 75 90 25 40 53 56 63 68 69
2-4 Red-Black Color all nodes black. Keep the children of a 2-node black. Split up 3-nodes and 4-nodes. 50 70 20 60 80 30 55 65 75 90 25 40 53 56 63 68 69
2-4 Red-Black Color all nodes black. Keep the children of a 2-node black. Split up 3-nodes and 4-nodes. 50 20 70 30 60 80 25 40 55 65 75 90 53 56 63 68 69
Insertions Perform BST insertion. If new node is root, color it black; otherwise, color it red. 5 3 10 13 17 insert 2 4 7 11 14 6 8 insert 16
Insertions Perform BST insertion. If new node is root, color it black; otherwise, color it red. 5 3 10 13 17 insert 2 2 4 7 11 14 16 6 8 insert 16 Simple. Is that all there is to it?
Fixing a Double Red (Okasaki's Method) z z x x x D y D A y A z A y x C B z y D B C A B C D B C y Preserves postorder traversal: AxByCzD x z A B C D
Fixing a Double Red (Okasaki's Method) z y x z x y x z y x y z x y z A A B A A B B B C C C C D D D D A B C D Another double red might result, but it will be two levels closer to the root. Repeat as needed or until reaching the root, which may then be colored black.
Fixing a Double Red (Okasaki's Method) z z x x x D y D A y A z A y x C B z y D B C A B C D B C Another double red might result, but it will be two levels closer to the root. Repeat as needed or until reaching the root, which may then be colored black. y x z A B C D But amazingly, a double red can be fixed with a single trinode restructuring!
Fixing a Double Red 30 30 10 10 20 10 20 30 10 20 30 20 20 10 30 Parent of new node has black sibling: trinode restructuring.
Fixing a Double Red 30 30 10 20 10 20 Parent of new node has red sibling: recolor. And if this creates a new double red...?
Illustration 4 2 10 13 17 1 3 7 11 14 16 6 8 Insert 5 5 The top node of the double-red has a red sibling, so we recolor.
Illustration 4 2 10 13 17 1 3 7 11 14 16 6 8 Insert 5 5 The top node of the double-red has a red sibling, so we recolor.
Illustration The top node of the double-red has a black sibling, so we restructure. 4 2 10 13 17 1 3 7 11 14 16 6 8 5
Illustration The top node of the double-red has a black sibling, so we restructure. 7 4 10 13 17 2 6 8 11 14 16 1 3 5
Deletion Remove key 5 5 3 10 13 17 2 4 8 11 14 16 6 9 Copy successor key to node and delete successor node.
Deletion 6 3 10 13 17 2 4 8 11 14 16 9 Copy successor key to node and delete successor node. Simple! Now suppose we want to delete key 9. Well, just delete it!
Deletion 6 3 10 13 17 2 4 8 11 14 16 But what if we want to delete key 8, 10, or 11?
Deletion 6 3 10 13 17 2 4 11 14 16 The black depth property has been violated. In this case, we can just recolor.
Deletion 6 3 10 13 17 2 4 11 14 16 The black depth property has been violated. In this case, we can just recolor.
Deletion 6 3 10 13 17 2 4 11 14 16 But suppose we delete key 11 and then we delete key 10.
Deletion 6 3 13 17 2 4 14 16 The black depth property no longer holds.
Removing a Double Black 6 3 13 17 2 4 14 16 The basic idea is to double count the blackness of the problem node and look for a nearby red node to absorb the extra blackness.
Removing a Double Black 6 3 13 17 2 4 14 16 In this case, the sibling of the double black node is black and has a red child, so we can rotate and recolor...
Removing a Double Black 3 2 6 13 17 4 14 16
Removing a Double Black There are two other cases to consider. Double black may propagate up to root. If you really want to delete a key from a RBT by hand without memorizing all the details: Transform the RBT to a (2, 4) tree. Delete the desired key. Transform the (2, 4) tree back to a RBT.