© 2014 by ali al najjar introduction to algorithms introduction to algorithms red black tree dr....
TRANSCRIPT
![Page 1: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/1.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Introduction to Algorithms
Red Black Tree
Dr. Ali Al Najjar
Day 18 L10.1
![Page 2: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/2.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Balanced search treesBalanced search tree: A search-tree datastructure for which a height of O(lg n) isguaranteed when implementing a dynamicset of n items.
Examples:
• AVL trees• 2-3 trees• 2-3-4 trees• B-trees• Red-black trees
Day 18 L10.2
![Page 3: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/3.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Red-black treesThis data structure requires an extra one-bit color field in each node.
Red-black properties:1. Every node is either red or black.2. The root and leaves (NIL’s) are black.3. If a node is red, then its parent is black.4. All simple paths from any node x to a descendant leaf have the same number of black nodes = black-height(x).
Day 18 L10.3
![Page 4: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/4.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Example of a red-black tree
Day 18 L10.4
![Page 5: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/5.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Example of a red-black tree
1. Every node is either red or black.
Day 18 L10.5
![Page 6: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/6.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
2. The root and leaves (NIL’s) are black.
Example of a red-black tree
Day 18 L10.6
![Page 7: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/7.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Example of a red-black tree
3. If a node is red, then its parent is black.
Day 18 L10.7
![Page 8: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/8.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Example of a red-black tree
4. All simple paths from any node x to adescendant leaf have the same number ofblack nodes = black-height(x).
Day 18 L10.8
![Page 9: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/9.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Height of a red-black treeTheorem. A red-black tree with n keys has height
h ≤ 2 lg(n + 1).
Proof. (The book uses induction. Read carefully.)
INTUITION:• Merge red nodes into their black parents.
Day 18 L10.9
![Page 10: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/10.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Height of a red-black tree
Theorem. A red-black tree with n keys has height
h ≤ 2 lg(n + 1).
Proof. (The book uses induction. Read carefully.)
INTUITION:• Merge red nodes into their black parents.
Day 18 L10.10
![Page 11: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/11.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Height of a red-black treeTheorem. A red-black tree with n keys has height
h ≤ 2 lg(n + 1).
Proof. (The book uses induction. Read carefully.)
INTUITION:• Merge red nodes into their black parents.
Day 18 L10.11
![Page 12: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/12.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Height of a red-black tree
Theorem. A red-black tree with n keys has height
h ≤ 2 lg(n + 1).
Proof. (The book uses induction. Read carefully.)
INTUITION:• Merge red nodesinto their blackparents.
Day 18 L10.12
![Page 13: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/13.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Height of a red-black treeTheorem. A red-black tree with n keys has height
h ≤ 2 lg(n + 1).
Proof. (The book uses induction. Read carefully.)
INTUITION:• Merge red nodesinto their blackparents.
Day 18 L10.13
![Page 14: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/14.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Height of a red-black treeTheorem. A red-black tree with n keys has height
h ≤ 2 lg(n + 1).
Proof. (The book uses induction. Read carefully.)
INTUITION:• Merge red nodes into their black parents. • This process produces a tree in which each node has 2, 3, or 4 children.• The 2-3-4 tree has uniform depth h' of leaves.
Day 18 L10.14
![Page 15: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/15.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Proof (continued)• We have h' ≥ h/2, since at most half the leaves on any path are red.• The number of leaves in each tree is n + 1
Day 18 L10.15
![Page 16: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/16.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Query operationsCorollary. The queries SEARCH, MIN,MAX, SUCCESSOR, and PREDECESSOR
all run in O(lg n) time on a red-blacktree with n nodes.
Day 18 L10.16
![Page 17: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/17.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Modifying operationsThe operations INSERT and DELETE causemodifications to the red-black tree:
• the operation itself,
• color changes,
• restructuring the links of the tree via “rotations”.
Day 18 L10.17
![Page 18: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/18.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Rotations
Rotations maintain the inorder ordering of keys:
• a α, ∈ b β, ∈ c γ ∈ ⇒ a ≤ A ≤ b ≤ B ≤ c.
A rotation can be performed in O(1) time.
Day 18 L10.18
![Page 19: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/19.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Insertion into a red-black treeIDEA: Insert x in tree. Color x red. Only redblackproperty 3 might be violated. Move theviolation up the tree by recoloring until it canbe fixed with rotations and recoloring.
Example:
Day 18 L10.19
![Page 20: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/20.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Insertion into a red-black treeIDEA: Insert x in tree. Color x red. Only redblackproperty 3 might be violated. Move theviolation up the tree by recoloring until it canbe fixed with rotations and recoloring.
Example:• Insert x =15.• Recolor, moving the violation up the tree.
Day 18 L10.20
![Page 21: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/21.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Insertion into a red-black treeIDEA: Insert x in tree. Color x red. Only redblackproperty 3 might be violated. Move theviolation up the tree by recoloring until it canbe fixed with rotations and recoloring.
Example:• Insert x =15.• Recolor, moving the violation up the tree.• RIGHT-ROTATE(18).
Day 18 L10.21
![Page 22: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/22.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Insertion into a red-black treeIDEA: Insert x in tree. Color x red. Only redblackproperty 3 might be violated. Move theviolation up the tree by recoloring until it canbe fixed with rotations and recoloring.
Example:• Insert x =15.• Recolor, moving the violation up the tree.• RIGHT-ROTATE(18).• LEFT-ROTATE(7) and recolor.
Day 18 L10.22
![Page 23: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/23.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Insertion into a red-black treeIDEA: Insert x in tree. Color x red. Only redblackproperty 3 might be violated. Move theviolation up the tree by recoloring until it canbe fixed with rotations and recoloring.
Example:• Insert x =15.• Recolor, moving the violation up the tree.• RIGHT-ROTATE(18).• LEFT-ROTATE(7) and recolor.
Day 18 L10.23
![Page 24: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/24.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms Day 18 L10.24
PseudocodeRB-INSERT(T, x) TREE-INSERT(T, x) color[x] ← RED ⊳ only RB property 3 can be violated while x ≠ root[T] and color[p[x]] = RED do if p[x] = left[p[p[x]] then y ← right[p[p[x]] ⊳ y = aunt/uncle of x if color[y] = RED then ⟨Case 1⟩ else if x = right[p[x]] then ⟨Case 2⟩ ⊳ Case 2 falls into Case 3 ⟨Case 3⟩ else ⟨“then” clause with “left” and “right” swapped⟩ color[root[T]] ← BLACK
![Page 25: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/25.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Graphical notation
Let denote a subtree with a black root.
All s have the same black-height.
Day 18 L10.25
![Page 26: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/26.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Case 1
(Or, children ofA are swapped.)
Push C’s black ontoA and D, and recurse,since C’s parent maybe red.
Day 18 L10.26
![Page 27: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/27.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Case 2
Transform to Case 3.
Day 18 L10.27
![Page 28: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/28.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Case 3
Done! No moreviolations of RBproperty 3 arepossible.
Day 18 L10.28
![Page 29: © 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649eab5503460f94bb0324/html5/thumbnails/29.jpg)
© 2014 by Ali Al Najjar Introduction to Algorithms
Analysis• Go up the tree performing Case 1, which onlyrecolors nodes.
• If Case 2 or Case 3 occurs, perform 1 or 2rotations, and terminate.
Running time: O(lg n) with O(1) rotations.
RB-DELETE— same asymptotic running timeand number of rotations as RB-INSERT (seetextbook).
Day 18 L10.29