Insertion in a 2-3 tree Case 2. Insert into a 3-node at bottom. • Add new key to 3-node to create temporary 4-node. • Move middle key in 4-node into parent. • Repeat up the tree, as necessary. inserting Z M search for Z ends R at this 3-node E J P H L A C S X replace 3-node with M temporary 4-node containing Z R E J P A C H L S X Z replace 2-node with new 3-node M containing middle key E J R X H P S Z A C L split 4-node into two 2-nodes pass middle key to parent 37
Insertion in a 2-3 tree Case 2. Insert into a 3-node at bottom. • Add new key to 3-node to create temporary 4-node. • Move middle key in 4-node into parent. increases height by 1 • Repeat up the tree, as necessary. • If you reach the root and it's a 4-node, split it into three 2-nodes. inserting D add middle key C to 3-node to make temporary 4-node search for D ends at this 3-node E J C E J A C H L H L A D add new key D to 3-node split 4-node into two 2-nodes to make temporary 4-node pass middle key to parent E J split 4-node into E three 2-nodes A C D H L increasing tree C J height by 1 H L A D 38
Local transformations in a 2-3 tree Splitting a 4-node is a local transformation: constant number of operations. a e b c d greater less between between between between than e than a a and b b and c d and e c and d a c e b d greater less between between between between than e than a a and b b and c d and e c and d 39
Global properties in a 2-3 tree Invariants. Maintains symmetric order and perfect balance. Pf. Each transformation maintains symmetric order and perfect balance. root parent is a 3-node b a b c left d e b d e a c a c a b c parent is a 2-node middle a e a c e b d d left b d b c d a b c a c right a a c right a b a b d b c d b d c e c d e 40
2-3 tree: performance Perfect balance. Every path from root to null link has same length. Tree height. • Worst case: • Best case: 41
2-3 tree: performance Perfect balance. Every path from root to null link has same length. Tree height. • Worst case: lg N . [all 2-nodes] • Best case: log 3 N ≈ .631 lg N .[all 3-nodes] • Between 12 and 20 for a million nodes. • Between 18 and 30 for a billion nodes. Guaranteed logarithmic performance for search and insert. 42
ST implementations: summary worst-case cost average case (after N random inserts) (after N inserts) ordered key implementation iteration? interface search insert delete search hit insert delete sequential search N N N N/2 N N/2 no equals() (unordered list) binary search lg N N N lg N N/2 N/2 yes compareTo() (ordered array) BST N N N 1.39 lg N 1.39 lg N ? yes compareTo() 2-3 tree c lg N c lg N c lg N c lg N c lg N c lg N yes compareTo() constants depend upon implementation 43
2-3 tree: implementation? Direct implementation is complicated, because: • Maintaining multiple node types is cumbersome. • Need multiple compares to move down tree. • Need to move back up the tree to split 4-nodes. • Large number of cases for splitting. Bottom line. Could do it, but there's a better way. 44
B ALANCED S EARCH T REES ‣ 2-3 search trees ‣ Red-black BSTs ‣ B-trees ‣ Geometric applications of BSTs
Left-leaning red-black BSTs (Guibas-Sedgewick 1979 and Sedgewick 2007) 1. Represent 2–3 tree as a BST. 2. Use "internal" left-leaning links as "glue" for 3–nodes. b 3-node larger key is root a b a less between greater greater than b than a a and b than b less between than a a and b black links connect red links "glue" 2-nodes and 3-nodes nodes within a 3-node black tree M M J R R E J P E L X C H S S X P A C H L A 2-3 tree corresponding red-black BST 46
An equivalent definition A BST such that: • No node has two red links connected to it. • Every path from root to null link has the same number of black links. - We will only allow one red link to simulate 2 keys in node - A node with two red links would be the same as having 3 keys "perfect black balance" • Red links lean left (correct ordering) black tree M J R P X E L C S H A 47
Left-leaning red-black BSTs: 1-1 correspondence with 2-3 trees Key property. 1–1 correspondence between 2–3 and LLRB. red − black tree M J R P X E L C S H A horizontal red links M J E R A C H P S X L 2-3 tree M R E J S X H L P A C 48
Search implementation for red-black BSTs Observation. Search is the same as for elementary BST (ignore color). but runs faster because of better balance public Val get(Key key) { Node x = root; black tree M while (x != null) { J R int cmp = key.compareTo(x.key); P X E L if (cmp < 0) x = x.left; C H S else if (cmp > 0) x = x.right; A else if (cmp == 0) return x.val; } return null; } Remark. Most other ops (e.g., ceiling, selection, iteration) are also identical. 49
Red-black BST representation Each node is pointed to by precisely one link (from its parent) ⇒ can encode color of links in nodes. private static final boolean RED = true; private static final boolean BLACK = false; private class Node h { h.left.color h.right.color E is RED Key key; is BLACK C J Value val; Node left, right; A D G boolean color; // color of parent link } private boolean isRed(Node x) { if (x == null) return false; return x.color == RED; } null links are black 50
Elementary red-black BST operations Left rotation. Orient a (temporarily) right-leaning red link to lean left. rotate E left (before) private Node rotateLeft(Node h) { E h assert isRed(h.right); Node x = h.right; S x h.right = x.left; x.left = h; less x.color = h.color; than E h.color = RED; return x; between greater } E and S than S Invariants. Maintains symmetric order and perfect black balance. 51
Elementary red-black BST operations Left rotation. Orient a (temporarily) right-leaning red link to lean left. rotate E left (after) private Node rotateLeft(Node h) { S x assert isRed(h.right); Node x = h.right; h E h.right = x.left; x.left = h; greater x.color = h.color; than S h.color = RED; return x; less between } than E E and S Invariants. Maintains symmetric order and perfect black balance. 52
Elementary red-black BST operations Right rotation. Orient a left-leaning red link to (temporarily) lean right. rotate S right (before) private Node rotateRight(Node h) { S h assert isRed(h.left); Node x = h.left; x E h.left = x.right; x.right = h; greater x.color = h.color; than S h.color = RED; return x; less between } than E E and S Invariants. Maintains symmetric order and perfect black balance. 53
Elementary red-black BST operations Right rotation. Orient a left-leaning red link to (temporarily) lean right. rotate S right (after) private Node rotateRight(Node h) { E x assert isRed(h.left); Node x = h.left; S h h.left = x.right; x.right = h; less x.color = h.color; than E h.color = RED; return x; between greater } E and S than S Invariants. Maintains symmetric order and perfect black balance. 54
Elementary red-black BST operations Color flip. Recolor to split a (temporary) 4-node. flip colors (before) private void flipColors(Node h) { h E assert !isRed(h); assert isRed(h.left); A S asset isRed(h.right); h.color = RED; h.left.color = BLACK; h.right.color = BLACK; } less between between greater than A A and E E and S than S Invariants. Maintains symmetric order and perfect black balance. 55
Elementary red-black BST operations Color flip. Recolor to split a (temporary) 4-node. flip colors (after) private void flipColors(Node h) { h E assert !isRed(h); assert isRed(h.left); A S asset isRed(h.right); h.color = RED; h.left.color = BLACK; h.right.color = BLACK; } less between between greater than A A and E E and S than S Invariants. Maintains symmetric order and perfect black balance. 56
Insertion in a LLRB tree: overview Basic strategy. Maintain 1-1 correspondence with 2-3 trees by applying elementary red-black BST operations. insert C E E A S A R S R add new node here right link red so rotate left E A S C R E E C S A C R S A R Insert into a 2-node 57
Insertion in a LLRB tree Warmup 1. Insert into a tree with exactly 1 node. right left root root search ends b a at this null link search ends attached new node a at this null link with red link root b b red link to root new node b a containing a rotated left a converts 2-node to make a to 3-node legal 3-node 58
Insertion in a LLRB tree Case 1. Insert into a 2-node at the bottom. • Do standard BST insert; color new link red. • If new red link is a right link, rotate left. insert C E E A S A R S R add new node here right link red so rotate left E A S C R E E C S A C R S A R Insert into a 2-node 59
Insertion in a LLRB tree Think of this as a split in Warmup 2. Insert into a tree with exactly 2 nodes. 2-3 tree between larger bet smaller c c search ends b at this a search ends b a null link at this null link search ends c at this null link attached new a c w node with attached new b red link b b node with w attached new red link a c c a node with b red link a rotated left rotated b right rotated d c a b right colors flipped c b a to black ped k c a colors flipped colors flipped b to black b to black c a c a 60
Insertion in a LLRB tree Case 2. Insert into a 3-node at the bottom. As with 2-3 Trees • Do standard BST insert; color new link red. we have to update parents, • Rotate to balance the 4-node (if needed). bottom-to-top if we violate the • Flip colors to pass red link up one level. conditions • Rotate to make lean left (if needed). node here two lefts in a row inserting H so rotate right E E C S C S A R R A R S E H add new node here C H A right link red so rotate left both children red so flip colors E E C R C R S A H S A H 61
Insertion in a LLRB tree: passing red links up the tree Case 2. Insert into a 3-node at the bottom. • Do standard BST insert; color new link red. • Rotate to balance the 4-node (if needed). • Flip colors to pass red link up one level. • Rotate to make lean left (if needed). • Repeat case 1 or case 2 up the tree (if needed). inserting P both children red R so flip colors R S E E S M C M C M E R both children A H red so P A H P C H S add new flip colors node here A two lefts in a row so rotate right right link red so rotate left R M R E R M S E S E P C P H S C M C A H P A H A both children red so flip colors 62
Red-black BST insertion insert S S 63
Red-black BST insertion insert E S E 64
Red-black BST insertion insert A S E A 65
Red-black BST insertion two left reds in a row insert A (rotate S right) S E A 66
Red-black BST insertion both children red (flip colors) E A S 67
Red-black BST insertion both children red (flip colors) E A S 68
Red-black BST insertion red-black BST E A S 69
Red-black BST insertion red-black BST E A S 70
Red-black BST insertion insert R E A S R 71
Red-black BST insertion red-black BST E A S R 72
Red-black BST insertion red-black BST E A S R 73
Red-black BST insertion insert C E A S C R 74
Red-black BST insertion E right link red (rotate A left) A S C R 75
Red-black BST insertion red-black BST E C S A R 76
Red-black BST insertion red-black BST E C S A R 77
Red-black BST insertion red-black BST E C S A R 78
Red-black BST insertion insert H E C S A R H 79
Red-black BST insertion E two left reds in a row (rotate S right) C S A R H 80
Red-black BST insertion E both children red (flip colors) C R A H S 81
Red-black BST insertion E both children red (flip colors) C R A H S 82
Red-black BST insertion right link red (rotate E left) E C R A H S 83
Red-black BST insertion red-black BST R S E C H A 84
Red-black BST insertion red-black BST R S E C H A 85
Red-black BST insertion red-black BST R S E C H A 86
Red-black BST insertion insert X R S E C H X A 87
Red-black BST insertion insert X R right link red (rotate S left) S E C H X A 88
Red-black BST insertion red-black BST R X E C H S A 89
Red-black BST insertion red-black BST R X E C H S A 90
Red-black BST insertion red-black BST R X E C H S A 91
Red-black BST insertion insert M R X E C H S A M 92
Red-black BST insertion insert M R X E right link red (rotate H left) C H S A M 93
Red-black BST insertion red-black BST R X E C M S A H 94
Red-black BST insertion insert P R X E C M S A H P 95
Red-black BST insertion insert P R X E two red children (flip colors) C M S A H P 96
Red-black BST insertion insert P R X E two red children (flip colors) C M S A H P 97
Red-black BST insertion R right link red (rotate E left) X E C M S A H P 98
Red-black BST insertion two left reds in a row (rotate R right) R X M E P S C H A 99
Red-black BST insertion two red children (flip colors) M E R C H P X A S 100
Recommend
More recommend