b alanced t rees
play

B ALANCED T REES Acknowledgement: The course slides are adapted from - PowerPoint PPT Presentation

BBM 202 - ALGORITHMS D EPT . OF C OMPUTER E NGINEERING B ALANCED T REES Acknowledgement: The course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton University. B ALANCED S EARCH T REES 2-3


  1. Insertion in a 2-3 tree Case 1. Insert into a 2-node at bottom. • Search for key, as usual. • Replace 2-node with 3-node. inserting K M R E J P S X H L A C search for K ends here M R E J K L P S X H A C replace 2-node with new 3-node containing K 37

  2. 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 H L S X Z A C replace 2-node with new 3-node M containing middle key E J R X P S Z A C H L split 4-node into two 2-nodes pass middle key to parent 38

  3. 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 39

  4. 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 40

  5. 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 41

  6. 2-3 tree: performance Perfect balance. Every path from root to null link has same length. Tree height. • Worst case: • Best case: 42

  7. 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. 43

  8. 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 44

  9. 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. 45

  10. B ALANCED S EARCH T REES ‣ 2-3 search trees ‣ Red-black BSTs ‣ B-trees ‣ Geometric applications of BSTs

  11. Multiple Node Types ‣ In 2-3 Trees, the algorithm automatically balances the tree ‣ However, we have to keep track of two different node types, complicating the source code. ‣ Nodes with one key ‣ Nodes with two keys ‣ Instead of multiple nodes: ‣ Multiple edge types; red and black ‣ Rotations instead of Split 47

  12. 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 greater less between greater than a a and b than 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 S H S X P A C H L A 2-3 tree corresponding red-black BST 48

  13. 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 49

  14. 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 E L X C S H A horizontal red links M J E R A C H P L S X 2-3 tree M R E J P S X H L A C 50

  15. 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. 51

  16. 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 52

  17. 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. 53

  18. 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. 54

  19. 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. 55

  20. 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. 56

  21. 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. 57

  22. 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. 58

  23. 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 59

  24. 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 60

  25. 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 61

  26. 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 node with b 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 62

  27. 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 63

  28. 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 S E M C M C M E R both children A H red so P A H C P 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 R M E S E S E P C P H S C M C A H P A H A both children red so flip colors 64

  29. Red-black BST insertion insert S S 65

  30. Red-black BST insertion insert E S E 66

  31. Red-black BST insertion insert A S E A 67

  32. Red-black BST insertion two left reds in a row insert A (rotate S right) S E A 68

  33. Red-black BST insertion both children red (flip colors) E A S 69

  34. Red-black BST insertion both children red (flip colors) E A S 70

  35. Red-black BST insertion red-black BST E A S 71

  36. Red-black BST insertion red-black BST E A S 72

  37. Red-black BST insertion insert R E A S R 73

  38. Red-black BST insertion red-black BST E A S R 74

  39. Red-black BST insertion red-black BST E A S R 75

  40. Red-black BST insertion insert C E A S C R 76

  41. Red-black BST insertion E right link red (rotate A left) A S C R 77

  42. Red-black BST insertion red-black BST E C S A R 78

  43. Red-black BST insertion red-black BST E C S A R 79

  44. Red-black BST insertion red-black BST E C S A R 80

  45. Red-black BST insertion insert H E C S A R H 81

  46. Red-black BST insertion E two left reds in a row (rotate S right) C S A R H 82

  47. Red-black BST insertion E both children red (flip colors) C R A H S 83

  48. Red-black BST insertion E both children red (flip colors) C R A H S 84

  49. Red-black BST insertion right link red (rotate E left) E C R A H S 85

  50. Red-black BST insertion red-black BST R S E C H A 86

  51. Red-black BST insertion red-black BST R S E C H A 87

  52. Red-black BST insertion red-black BST R S E C H A 88

  53. Red-black BST insertion insert X R S E C H X A 89

  54. Red-black BST insertion insert X R right link red (rotate S left) S E C H X A 90

  55. Red-black BST insertion red-black BST R X E C H S A 91

  56. Red-black BST insertion red-black BST R X E C H S A 92

  57. Red-black BST insertion red-black BST R X E C H S A 93

  58. Red-black BST insertion insert M R X E C H S A M 94

  59. Red-black BST insertion insert M R X E right link red (rotate H left) C H S A M 95

  60. Red-black BST insertion red-black BST R X E C M S A H 96

  61. Red-black BST insertion insert P R X E C M S A H P 97

  62. Red-black BST insertion insert P R X E two red children (flip colors) C M S A H P 98

  63. Red-black BST insertion insert P R X E two red children (flip colors) C M S A H P 99

  64. Red-black BST insertion R right link red (rotate E left) X E C M S A H P 100

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend