Download presentation
Presentation is loading. Please wait.
Published byNoel Carter Modified over 9 years ago
2
Bottom-Up Red-Black Trees Top-down red-black trees require O(log n) rotations per insert/delete. Color flips cheaper than rotations. Priority search trees. Two keys per element. Search tree on one key, priority queue on other. Color flip doesn’t disturb priority queue property. Rotation disturbs priority queue property. O(log n) fix time per rotation => O(log 2 n) overall time.
3
Bottom-Up Red-Black Trees Bottom-up red-black tree properties. At most 1 rotation per insert/delete. O(1) amortized complexity to restructure following an insert/delete.
4
Bottom-Up Insert New pair is placed in a new node, which is inserted into the red-black tree. New node color options. Black node => one root-to-external-node path has an extra black node (black pointer). Hard to remedy. Red node => one root-to-external-node path may have two consecutive red nodes (pointers). May be remedied by color flips and/or a rotation.
5
Classification Of 2 Red Nodes/Pointers ab c d gp pp p XYz X => relationship between gp and pp. pp left child of gp => X = L. Y => relationship between pp and p. p right child of pp => Y = R. z = b (black) if d = null or a black node. z = r (red) if d is a red node.
6
XYr Color flip. ab c d gp pp p ab c d gp pp p Move p, pp, and gp up two levels. Continue rebalancing if necessary.
7
LLb Rotate. Done! Same as LL rotation of AVL tree. y x ab z cd ab c d gp pp p x y z
8
LRb Rotate. Done! Same as LR rotation of AVL tree. RRb and RLb are symmetric. y x ab z cd bc a d gp pp p y x z
9
Exercise: build the red-black tree Build a red-black tree from the following key values in sequence: 45, 28, 36, 22, 68, 50, 10, 98, 26, 15, 25, 27, 78
10
Delete Delete as for unbalanced binary search tree. If red node deleted, no rebalancing needed. If black node deleted, a subtree becomes one black pointer (node) deficient.
11
Delete A Black Leaf 10 7 8 1 5 30 40 20 25 35 45 60 3 Delete 8.
12
Delete A Black Leaf y y is root of deficient subtree. py is parent of y. 10 7 1 5 30 40 20 25 35 45 60 3 py
13
Delete A Black Degree 1 Node 10 7 8 1 5 30 40 20 25 35 45 60 3 Delete 45. y y is root of deficient subtree. py
14
Delete A Black Degree 2 Node 10 7 8 1 5 30 40 20 25 35 45 60 3 Not possible, degree 2 nodes are never deleted.
15
Rebalancing Strategy If y is a red node, make it black. 10 7 8 1 5 30 40 20 25 35 45 60 3 y py
16
Rebalancing Strategy Now, no subtree is deficient. Done! 60 10 7 8 1 5 30 40 20 25 35 45 3 y py
17
Rebalancing Strategy y is a black root (there is no py). Entire tree is deficient. Done! 60 10 7 8 1 5 30 40 20 25 35 45 3 y
18
Rebalancing Strategy y is black but not the root (there is a py). Xcn y is right child of py => X = R. Pointer to v is black => c = b. v has 1 red child => n = 1. ab y py v
19
Rb0 (case 1) Color change. Now, py is root of deficient subtree. Continue! ab y py v y ab v
20
Rb0 (case 2) Color change. Deficiency eliminated. Done! ab y py v y ab v
21
Rb1 (case 1) LL rotation. Deficiency eliminated. Done! ab y py v a by v
22
Rb1 (case 2) LR rotation. Deficiency eliminated. Done! a y py v bc wcy w ab v
23
Rb2 LR rotation. Deficiency eliminated. Done! a y py v bc wcy w ab v
24
Rr(n) n = # of red children of vs right child w. a y py v bc w
25
Rr(0) LL rotation. Done! ab y py v a by v
26
Rr(1) (case 1) LR rotation. Deficiency eliminated. Done! a y py v b w c yc w a v b
27
Rr(1) (case 2) Rotation. Deficiency eliminated. Done! a y py v b w cd x yd x a v bc w
28
Rr(2) Rotation. Deficiency eliminated. Done! a y py v b w cd x dy x a v bc w
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.