red black trees
play

Red-Black Trees Binary Search Trees with O(log n) Worst-Case Time - PowerPoint PPT Presentation

Red-Black Trees Binary Search Trees with O(log n) Worst-Case Time per Operation The Red-Black Tree Properties Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. nil nil nil nil nil


  1. Red-Black Trees Binary Search Trees with O(log n) Worst-Case Time per Operation

  2. The Red-Black Tree Properties Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. nil nil nil nil nil Both children of a red node are black. nil nil nil nil nil Every root-to-leaf path contains the same nil nil number of black nodes. The black-height of a node x is the number of black nodes on a path from, but not including, x to a leaf. The black-height of T is the black-height of its root.

  3. The Height of a Red-Black Tree with n Nodes Lemma: A red-black tree of black-height h contains at least 2 h – 1 nodes. Lemma: A red-black tree of black-height h has height at most 2 h. nil nil nil nil nil Corollary: A red-black tree with n nodes has height at most 2 log ( n + 1) . nil nil nil nil nil nil nil Corollary: Operations Tree-Minimum, Tree-Maximum, Tree-Predecessor, Tree- Successor, and Find take O( log n) time on a red-black tree of size n.

  4. Rotations—The Key to Height Reduction Right rotation d b b e a d a c c e C A A B B C Left rotation

  5. A Rotation Takes Constant Time Right rotation d b b e a d a c c e C A A B B C d b e a c

  6. A Rotation Takes Constant Time Right rotation d b b e a d a c c e C A A B B C d b e a c

  7. A Rotation Takes Constant Time Right rotation d b b e a d a c c e C A A B B C d b e a c

  8. A Rotation Takes Constant Time Right rotation d b b e a d a c c e C A A B B C d b e a c

  9. A Rotation Takes Constant Time Right rotation d b b e a d a c c e C A A B B C b a d c e

  10. Insertion into a Red-Black Tree 17 19 7 13 18 5 37 nil nil nil nil nil 1 49 25 nil nil nil nil nil 33 nil nil Element to insert: 21

  11. Insertion into a Red-Black Tree Red-black tree properties 4 Every node has a color, red or black. 17 4 The root is black. 19 7 4 13 18 The leaves are black. 5 37 nil nil nil nil nil Both children of a red node are black. 1 49 25 nil nil nil nil Every root-to-leaf path contains the 21 33 same number of black nodes. nil nil nil nil Element to insert: 21

  12. Insertion into a Red-Black Tree Red-black tree properties 4 Every node has a color, red or black. 17 4 The root is black. 19 7 4 13 18 The leaves are black. 5 37 nil nil nil nil nil Both children of a red node are black. 1 49 25 nil nil nil nil nil Every root-to-leaf path contains the 33 same number of black nodes. nil nil Element to insert: 3

  13. Insertion into a Red-Black Tree Red-black tree properties 4 Every node has a color, red or black. 17 4 The root is black. 19 7 4 13 18 The leaves are black. 5 37 No red-red property 8 nil nil nil nil nil Both children of a red node are black. 1 49 25 nil nil nil nil Every root-to-leaf path contains the 4 33 3 same number of black nodes. nil nil nil nil Goal: Restore no-red-red condition while maintaining the other four. Element to insert: 3 Tools: Recoloring of nodes; rotations

  14. Insertion into a Red-Black Tree Red-black tree properties 4 Every node has a color, red or black. 17 4 The root is black. 19 7 4 13 18 The leaves are black. 5 37 No red-red property 8 nil nil nil nil nil Both children of a red node are black. 1 49 25 nil nil nil nil Every root-to-leaf path contains the 4 33 3 same number of black nodes. nil nil nil nil Goal: Restore no-red-red condition while maintaining the other four. Element to insert: 3 Tools: Recoloring of nodes; rotations

  15. Restoring the No-Red-Red Property Invariant: There is exactly one red node x in the tree whose parent may be red. Strategy: ■ Fix the violation of no-red-red property at x ■ This may violate the condition at some ancestor ■ Continue fixing the property at the ancestor (the ancestor becomes x ) Observations: ■ Initially, x has a red parent; hence, x is not the root. ■ If parent[ x ] is red, it is not the root either; parent[parent[ x ]] ? that is, parent[parent[ x ]] exists. ■ If parent[ x ] is black, we are done. parent[ x ] x

  16. Restoring the No-Red-Red Property Invariant: There is exactly one red node x in the tree whose parent may be red. Strategy: ■ Fix the violation of no-red-red property at x ■ This may violate the condition at some ancestor ■ Continue fixing the property at the ancestor (the ancestor becomes x ) Observations: ■ Initially, x has a red parent; hence, x is not the root. ■ If parent[ x ] is red, it is not the root either; parent[parent[ x ]] ? that is, parent[parent[ x ]] exists. ■ If parent[ x ] is black, we are done. parent[ x ] Assumptions: x ■ Neither x nor parent[x] is the root ■ parent[x] is the left child of parent[parent[x]]

  17. Restoring the No-Red-Red Property Invariant: There is exactly one red node x in the tree whose parent may be red. Strategy: ■ Fix the violation of no-red-red property at x ■ This may violate the condition at some ancestor ■ Continue fixing the property at the ancestor (the ancestor becomes x ) Observations: ■ Initially, x has a red parent; hence, x is not the root. ■ If parent[ x ] is red, it is not the root either; parent[parent[ x ]] that is, parent[parent[ x ]] exists. ■ If parent[ x ] is black, we are done. parent[ x ] Assumptions: x ■ Neither x nor parent[x] is the root ■ parent[x] is the left child of parent[parent[x]]

  18. Restoring the No-Red-Red Property Invariant: There is exactly one red node x in the tree whose parent may be red. Strategy: ■ Fix the violation of no-red-red property at x ■ This may violate the condition at some ancestor ■ Continue fixing the property at the ancestor (the ancestor becomes x ) Observations: ■ Initially, x has a red parent; hence, x is not the root. ■ If parent[ x ] is red, it is not the root either; parent[parent[ x ]] that is, parent[parent[ x ]] exists. ■ If parent[ x ] is black, we are done. parent[ x ] Assumptions: x ■ Neither x nor parent[x] is the root ■ parent[x] is the left child of parent[parent[x]]

  19. Restoring the No-Red-Red Property 3 cases: ■ Both parent[ x ] and its parent[parent[ x ]] x sibling are red Recoloring ■ parent[ x ] is red, its sibling parent[ x ] is black, and x is the left child of its parent x ■ As Case 2; but x is the Case 1 right child of its parent

  20. Restoring the No-Red-Red Property 3 cases: ■ Both parent[ x ] and its parent[parent[ x ]] x sibling are red Recoloring ■ parent[ x ] is red, its sibling parent[ x ] is black, and x is the left child of its parent x ■ As Case 2; but x is the Case 1 right child of its parent parent[parent[ x ]] Recoloring Right-rotation parent[ x ] x Case 2

  21. Restoring the No-Red-Red Property 3 cases: ■ Both parent[ x ] and its parent[parent[ x ]] x sibling are red Recoloring ■ parent[ x ] is red, its sibling parent[ x ] is black, and x is the left child of its parent x ■ As Case 2; but x is the Case 1 right child of its parent parent[parent[ x ]] Recoloring + Right-rotation parent[ x ] x Case 2

  22. Restoring the No-Red-Red Property 3 cases: ■ Both parent[ x ] and its parent[parent[ x ]] x sibling are red Recoloring ■ parent[ x ] is red, its sibling parent[ x ] is black, and x is the left child of its parent x ■ As Case 2; but x is the Case 1 right child of its parent parent[parent[ x ]] Recoloring + Left-rotation Right-rotation parent[ x ] x Case 2 Case 3

  23. Insertion into a Red-Black Tree—Summary Observations: ■ Every case takes constant time ■ Case 1 brings x two steps closer to the root and only recolors nodes (no rotations) ■ In Cases 2 and 3, we perform 1 or 2 rotations; then we are done Lemma: An insertion into a red-black tree with n nodes takes O (log n ) time and performs at most two rotations.

  24. Deletion from a Red-Black Tree 17 19 7 13 18 5 37 nil nil nil nil nil 1 49 25 nil nil nil nil nil 33 nil nil Element to delete: 25

  25. Deletion from a Red-Black Tree Red-black tree properties 4 Every node has a color, red or black. 17 4 The root is black. 19 7 4 13 18 The leaves are black. 5 37 4 nil nil nil nil nil Both children of a red node are black. 1 49 33 nil nil nil nil nil nil Every root-to-leaf path contains the same number of black nodes. Element to delete: 25

  26. Deletion from a Red-Black Tree Red-black tree properties 4 Every node has a color, red or black. 17 4 The root is black. 19 7 4 13 18 The leaves are black. 5 37 4 nil nil nil nil nil Both children of a red node are black. 1 49 25 nil nil nil nil nil Every root-to-leaf path contains the 33 same number of black nodes. nil nil Element to delete: 18

  27. Deletion from a Red-Black Tree Red-black tree properties 4 Every node has a color, red or black. 17 4 The root is black. 19 7 4 13 nil The leaves are black. 5 37 4 nil nil nil Both children of a red node are black. 1 49 25 nil nil nil nil nil Every root-to-leaf path contains the 8 33 same number of black nodes. nil nil Element to delete: 18

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