![Page 1: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/1.jpg)
1
Red Black Trees(Guibas Sedgewick 78)
CLRS: Chapter 13
![Page 2: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/2.jpg)
2
• We assume items at the leaves
• Don’t show keys throughout the presentation, they are basically handled as before
![Page 3: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/3.jpg)
5
Red Black trees - definition
Binary search tree
each node is colored red or black such that
1) Leaves are black
2) All paths from the root to an external node contain the same number of black nodes (black rule)
3) Any red node, if has a parent, has a black parent (red rule)
![Page 4: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/4.jpg)
6
Red Black trees - example
![Page 5: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/5.jpg)
7
Red Black Trees -properties
The depth is O(log n) -- prove as an excercise
==> find takes O(log n) time
How do we do the other operations and keep the trees Red-Black ?
![Page 6: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/6.jpg)
8
Insert
![Page 7: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/7.jpg)
9
Insert (cont)
![Page 8: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/8.jpg)
10
Insert (cont)
![Page 9: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/9.jpg)
11
Insert (cont)
![Page 10: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/10.jpg)
12
Insert (cont)
![Page 11: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/11.jpg)
13
Use rotations
x
y
B
C
y
Ax
B C
<===>
A
x
y
z
y
z x
===>
![Page 12: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/12.jpg)
14
Insert (cont)
xy
z
y
z x
====>
![Page 13: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/13.jpg)
15
Insert (cont)
![Page 14: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/14.jpg)
16
Insert (cont)
![Page 15: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/15.jpg)
17
Insert (cont)
![Page 16: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/16.jpg)
18
Insert (cont)
![Page 17: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/17.jpg)
19
Insert (cont)
![Page 18: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/18.jpg)
20
Insert (cont)
![Page 19: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/19.jpg)
21
Insert -- definition
Convert a leaf to a red internal node with two leaves.
This may create violation to property 2. To restore it we walk up towards the root applying one of the following cases (each case has a symmetric version)
![Page 20: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/20.jpg)
22
Insert -- non terminal casesz
y
x
A B
C D E
z
y
x
A B
C D E
z
y
x
B C
A D E
===>
(1)
(2)
y
x
B C
A D E
===>
![Page 21: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/21.jpg)
23
Insert -- terminal cases
z
y
x
A B
C D E
y
zx
A B C
z
y
x
B C
A D E
===>
(3)
(4)
x
B
x
B
===>
w
D E
w
(5)===>
w zy
A B C
D E
w
x
z
y
x
A B
C D E
w
![Page 22: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/22.jpg)
24
Insert - analysis
O(log n) time worst case, since the height is O(log n)
Suppose you start with an empty tree and do m insertions such that the point of insertion is given to you each time, how much time does it take ?
Obviously O(mlog n),
but maybe we can prove it cannot be that bad ?
![Page 23: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/23.jpg)
25
Insert - analysis
Each time we do a color-flip-step the number of red nodes decreases by one.(tree) = #red nodes
Actual(insert) = O(1) + #color-flips-steps
(insert) = O(1) - #color-flips-steps
==> amortized(insert) = O(1)
and the sequence actually takes O(m) time.
![Page 24: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/24.jpg)
26
Delete -- example
![Page 25: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/25.jpg)
27
Delete -- example (cont)
-
![Page 26: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/26.jpg)
28
Delete -- example (cont)
![Page 27: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/27.jpg)
29
Delete -- example2 (cont)
![Page 28: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/28.jpg)
30
Delete -- example2 (cont)
-
![Page 29: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/29.jpg)
31
Delete -- example2 (cont)
-
![Page 30: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/30.jpg)
32
Delete -- example2 (cont)
![Page 31: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/31.jpg)
33
Delete -- definition
Replace the parent of the external node containing the item with the sibling subtree of the deleted item
If the parent of the deleted item is black then we create a short node
To restore the black constraint we go bottom up applying one of the following cases.
![Page 32: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/32.jpg)
34
Delete -- fixing a short node
--
====>(1)
- ====>(2)
![Page 33: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/33.jpg)
35
Delete -- fixing a short node (cont)
- ====>(3) x
y
z
w x
y w
z
- ====>(4) x
y
z
wA
x
yv
v
BA
-x
y
z
w
v
B
A-
wB
![Page 34: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/34.jpg)
36
Delete -- fixing a short node (cont)
- ====>(5) x
y
z
w
yz
v
w
x v-
And apply one of the previous 3 cases.
![Page 35: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/35.jpg)
37
Delete + insert -- analysis
O(log n) time, since the height is O(log n)
Suppose you start with an empty tree and do m insertions and deletions such that the point of insertion is given to you each time, how much time does it take ?
Obviously O(mlog n),
but maybe we can prove it cannot be that bad ?
![Page 36: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/36.jpg)
38
Delete + insert - analysis
The previous potential won’t do the trick
(tree) = #red nodes
Here are the transformation that we want to release potential
![Page 37: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/37.jpg)
39
Delete + insert -- analysis
--
====>
===>
===>
![Page 38: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/38.jpg)
40
Delete + insert -- analysis
(tree) =
#( ) + 2 #( )
==> amortized(delete) = O(1)
amortized(insert) = O(1)
sequence of m delete and inserts, starting from an empty tree takes O(m) time
![Page 39: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/39.jpg)
41
Concatenation
+ =
Define the rank of a node v as the number of black nodes from v to a leaf .
Assume T1 has a black root.
Look on the left spine of T2 for a node x of the same rank as the root of T1
T1
T2
![Page 40: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/40.jpg)
42
Concatenation (cont)
+ =
T1
T2
x
Make y a child of p(x)
Continue as for insert
y
Allocate y make the root of T1 and x children of y.
Color y red
![Page 41: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/41.jpg)
43
Concatenation (analysis)
O(|r1-r2| + 1) = O(log n) worst case.
If the right point on the spine of the taller tree is given then its O(1) amortized
![Page 42: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/42.jpg)
44
Split
x
Concatenate all trees to the left of the path from the root to x to the left tree T1 (including x).
Concatenate all trees to the right of the path from the root to x to the right tree T2
![Page 43: Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13](https://reader035.vdocuments.us/reader035/viewer/2022062722/56813931550346895da0dab8/html5/thumbnails/43.jpg)
45
Split -- analysis.
Can prove that split takes O(log n) time.