-
Notifications
You must be signed in to change notification settings - Fork 54
/
RedBlackTrees.html
642 lines (584 loc) · 20.5 KB
/
RedBlackTrees.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
<html>
<!-- THIS FILE WAS GENERATED BY A SCRIPT: DO NOT EDIT IT! -->
<head>
<link href="style.css" rel="stylesheet" type="text/css"/>
<title>
Design and Analysis of Algorithms: Red-Black Trees
</title>
</head>
<body>
<div id="header">
<div id="logo">
<img src="graphics/Julia.png">
</div>
<div id="user-tools">
<a href="index.html">Home</a>
<a href="about.html">About</a>
<a href="feedback.html">Feedback</a>
</div>
</div>
<h1>
Design and Analysis of Algorithms: Red-Black Trees
</h1>
<div style="text-align:center">
<p>
<img
src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/66/Red-black_tree_example.svg/500px-Red-black_tree_example.svg.png">
</p>
</div>
<details>
<summary class="sum1">
13.1 What is a red-black tree?
</summary>
<p>
The colors (indeed, using any color at all -- we could call
them 0 and 1 trees!) are arbitrary. One story from one of
the creators is that they had red and black pens handy!
</p>
<details>
<summary class="sum2">
Red-black properties
</summary>
<ol>
<li>
Every node is red or black.
</li>
<li>
The root is black.
</li>
<li>
Every leaf (<i>T.nil</i>) is black.
</li>
<li>
If a node is red, then both of its children are
black. (Never two reds in a row while descending!)
</li>
<li>
For each node, all paths from the node to its
descendant leaves contain the same number of black
nodes.
</li>
</ol>
<p>
<b>T.nil</b>: This is simply a space saving device: every
"leaf" node of an ordinary BST gets left and right child
pointers to a special node, <i>T.nil</i>, and the root node
of the tree has its parent point to this as well.
</p>
<details>
<summary class="sum3">
How Red-Black Trees Solve the BST Problem
</summary>
<p>
And so, a simple way to get an intuition as
to why no leaf is further than
twice as far from the root as the nearest leaf: The
nearest leaf at the least <i>bh</i> levels from the root.
(The black-height of every node is the same.)
Since there is never more than one R-node
between any two B-nodes, at
most, the furthest node can be 2B levels away from
the root.
<br />
So we get:
<br />
A red-black tree with <i>n</i> internal nodes has height at
most 2 lg(<i>n</i> + 1).
</p>
<figure>
<img src="graphics/RedBlackBalance.png"
height="300" width="480">
<figcaption>
How a red-black tree ensures balance
<br />
(Source:
http://www.geeksforgeeks.org/red-black-tree-set-1-introduction-2/)
</figcaption>
</figure>
</details>
</details>
<details>
<summary class="sum2">
Operations on red-black trees
</summary>
<p>
<b>Remember:</b> This <i>is</i> a binary search tree.
<br>
So, non-modifying operations such as minimum(),
maximum(), successor(), predecessor(), and search() run
in O(height) time, and so for red-black trees, in O(lg
n) time.
<br>
<br>
But what about insert and delete? They are obviously
trickier. In fact, they are the whole trick: the
red-black properties are just a way of keeping the tree
roughly balanced.
</p>
<p>
But to get to insert and delete, we need to pass
through rotations on the way.
</p>
</details>
<details>
<summary class="sum2">
Quiz
</summary>
<ol>
<li>
The maximum height for a red-black tree is
</li>
<ol type="a" class="nested">
<li>
<input type="radio" name="q1" value="a">
n
</li>
<li>
<input type="radio" name="q1" value="b">
8lg(n + 1)
</li>
<li>
<input type="radio" name="q1" value="c">
lg(n)
</li>
<li>
<input type="radio" name="q1" value="d">
2lg(n + 1)
</li>
</ol>
<li>
A red-black tree is
</li>
<ol type="a" class="nested">
<li>
<input type="radio" name="q2" value="a">
a type of binary search tree
</li>
<li>
<input type="radio" name="q2" value="b">
a minimum spanning tree
</li>
<li>
<input type="radio" name="q2" value="c">
a B-tree
</li>
</ol>
<li>
The goal of red-black trees is to
</li>
<ol type="a" class="nested">
<li>
<input type="radio" name="q3" value="a">
keep a BST balanced
</li>
<li>
<input type="radio" name="q3" value="b">
minimize insert time in a BST
</li>
<li>
<input type="radio" name="q3" value="c">
minimize delete time in a BST
</li>
<li>
<input type="radio" name="q3" value="d">
minimize search time in a BST
</li>
</ol>
<li>
Given the properties of a red-black tree, one can never have
</li>
<ol type="a" class="nested">
<li>
<input type="radio" name="q4" value="a">
a root node that is black
</li>
<li>
<input type="radio" name="q4" value="b">
nil leaf nodes
</li>
<li>
<input type="radio" name="q4" value="c">
two black nodes linked
</li>
<li>
<input type="radio" name="q4" value="d">
two red nodes linked
</li>
</ol>
</ol>
<details>
<summary class="sum3">
Answers
</summary>
<p>
1. d; 2. a; 3. a; 4. d;
</p>
</details>
</details>
</details>
<details>
<summary class="sum1">
13.2 Rotations
</summary>
<details>
<summary class="sum2">
Right Rotate
</summary>
<p>
Let's enter 6, 4, 2 in the:
<br />
<a
href="https://www.cs.usfca.edu/~galles/visualization/RedBlack.html">
Red-black tree visualizer
</a>
</p>
<p>
<b>Note</b>: In-order walks of the pre- and post-rotate
trees produce the same output.
It is still a BST!
</p>
<figure>
<img src="graphics/RedBlackRightRotate.png"
height="280" width="500">
<figcaption>
A right rotation
</figcaption>
</figure>
</details>
<details>
<summary class="sum2">
Left Rotate
</summary>
<p>
Let's enter 2, 4, 6 in the:
<br />
<a
href="https://www.cs.usfca.edu/~galles/visualization/RedBlack.html">
Red-black tree visualizer
</a>
</p>
<p>
Here's the pseudo-code:
<code>
<pre>
Left-Rotate(T, x)
y = x.right
x.right = y.left
if y.left != T.nil
y.left.p = x
y.p = x.p
if x.p == T.nil
T.root = y
elif x == x.p.left
x.p.left = y
else
x.p.right = y
y.left = x
x.p = y
</pre>
</code>
</p>
</details>
</details>
<details>
<summary class="sum1">
13.3 Insertions
</summary>
<details>
<summary class="sum2">
Insert Animation
</summary>
<figure>
<iframe width="560" height="315"
src="https://www.youtube.com/embed/vDHFF4wjWYU"
frameborder="0" allowfullscreen>
</iframe>
<figcaption>
A series of insertions in a red-black tree
</figcaption>
</figure>
</details>
<details>
<summary class="sum2">
Code
</summary>
<p>
Here's the pseudo-code:
</p>
<details>
<summary class="sum3">
Insert
</summary>
<p>
<code>
<pre>
RB-Insert(T, z)
y = T.nil
x = T.root
while x != T.nil
y = x
if z.key < x.key
x = x.left
else
x = x.right
z.p = y
if y == T.nil
T.root = z
elseif z.key < y.key
y.left = z
else
y.right = z
z.left = T.nil
z.right = T.nil
z.color = RED
RB-Insert-Fixup(T, z)
</pre>
</code>
</p>
</details>
<details>
<summary class="sum3">
After Insert Fixup
</summary>
<p>
<code>
<pre>
RB-Insert-Fixup(T, z)
while z.p.color == RED
if z.p == z.p.p.left
y = z.p.p.right
if y.color == RED
z.p.color = BLACK
y.color = BLACK
z.p.p.color = RED
z = z.p.p
else
if z == z.p.right
z = z.p
Left-Rotate(T, z)
z.p.color = BLACK
z.p.p.color = RED
Right-Rotate(T, z.p.p)
else // same as if with right and left reversed
...
T.root.color = BLACK
</pre>
</code>
</p>
</details>
</details>
<details>
<summary class="sum2">
Fixup loop invariant
</summary>
<ol class="nested">
<li>
Node <i>z</i> is red.
</li>
<li>
If <i>z</i>.<i>p</i> is the root, then
<i>z</i>.<i>p</i> is black.
</li>
<li>
If the tree violates any red-black properties, then
it violates at most one. That one will be either:
<ul class="nested">
<li>
Property 2, because <i>z</i> is the root and is red,
or
</li>
<li>
Property 4, because <i>z</i> and
<i>z</i>.<i>p</i> are both red.
</li>
</ul>
</li>
</ol>
<p>
Each loop iteration we do one of two things: either we move
up the tree,
</p>
</details>
<details>
<summary class="sum2">
Analysis
</summary>
<p>
The insert code itself (without fixup)
takes O(lg <i>n</i>) time, just like for a BST.
<br />
In fixup, the rotations occur in constant time.
<br />
We move up the tree while fixing, and this happens at most
lg <i>n</i> times.
<br />
Therefore, insert is O(lg <i>n</i>).
</p>
</details>
<details>
<summary class="sum2">
Quiz
</summary>
<ol>
<li>
Why do we left rotate at line 12 of RB-Insert-Fixup?
</li>
<ol type="a" class="nested">
<li>
<input type="radio" name="q1" value="a">
z is its parent's left child, which means the tree is growing to the left
</li>
<li>
<input type="radio" name="q1" value="b">
because z's parent is the right child of z's grandparent
</li>
<li>
<input type="radio" name="q1" value="c">
because z's grandparent is RED
</li>
</ol>
<li>
Line 4 of RB-Insert-Fixup, where the color of y is checked, is determining
</li>
<ol type="a" class="nested">
<li>
<input type="radio" name="q2" value="a">
the color of z's niece
</li>
<li>
<input type="radio" name="q2" value="b">
the color of z's grandparent
</li>
<li>
<input type="radio" name="q2" value="c">
the color of z's uncle
</li>
<li>
<input type="radio" name="q2" value="d">
the color of z's parent
</li>
</ol>
</ol>
<details>
<summary class="sum3">
Answers
</summary>
<p>
1. a; 2. c;
</p>
</details>
</details>
</details>
<details>
<summary class="sum1">
13.4 Deletions
<br />
NOT COVERED FALL 2017
</summary>
<details>
<summary class="sum2">
Red-Black Deletion Video
</summary>
<ul>
<li>
Red node
</li>
<li>
Black node with red child
</li>
<li>
Black node with black children
</li>
</ul>
<figure>
<iframe width="560" height="315" src="https://www.youtube.com/embed/CTvfzU_uNKE" frameborder="0" allowfullscreen>
</iframe>
<figcaption>
Red-Black Tree Deletion
</figcaption>
</figure>
<p>
RB-Delete is going to rely on RB-Transplant:
</p>
<p>
<code>
<pre>
RB-Transplant(t, u, v)
if u.p == T.nil
T.root = v
elsif u == u.p.left
u.p.left = v
else
u.p.right = v
v.p = u.p
</pre>
</code>
</p>
<p>
And this will only be called on nodes for which it will
work: we don't call it on a node with two children asking
that one of them be its replacement without finding a
min or max, for instance.
<br />
So here is RB-Delete:
</p>
<p>
<code>
<pre>
RB-Delete(T, x)
</pre>
</code>
</p>
<p>
And here is RB-Delete-Fixup:
</p>
<p>
<code>
<pre>
RB-Delete-Fixup(T, x)
</pre>
</code>
</p>
</details>
</details>
<details>
<summary class="sum1">
Source Code
</summary>
<p>
<a href="https://github.com/gcallah/algorithms/tree/master/Java/RedBlackTrees">Java</a><br>
</p>
</details>
<details>
<summary class="sum1">
For Further Study
</summary>
<ul class="nested">
<li><a
href="https://www.cs.usfca.edu/~galles/visualization/RedBlack.html">
Red-black tree visualizer
</a>
</li>
<li><a href="https://en.wikipedia.org/wiki/Red–black_tree">
Wikipedia page on red-black trees
</a>.
</li>
</ul>
</details>
<details>
<summary class="sum1">
Homework
</summary>
</details>
</body>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-97026578-2', 'auto');
ga('send', 'pageview');
</script>
</html>