0% found this document useful (0 votes)
29 views33 pages

Red Black Trees: Colored Nodes Definition Red

Red black trees are self-balancing binary search trees where each node is colored red or black. Properties of red black trees include: - Each path from the root to a leaf contains the same number of black nodes - No path contains two consecutive red nodes - Insertion and deletion of nodes maintains the red-black properties through rotations and recoloring in O(log n) time - Red black trees have height of O(log n) and support search, insertion and deletion in O(log n) time

Uploaded by

PaVan Nelakuditi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views33 pages

Red Black Trees: Colored Nodes Definition Red

Red black trees are self-balancing binary search trees where each node is colored red or black. Properties of red black trees include: - Each path from the root to a leaf contains the same number of black nodes - No path contains two consecutive red nodes - Insertion and deletion of nodes maintains the red-black properties through rotations and recoloring in O(log n) time - Red black trees have height of O(log n) and support search, insertion and deletion in O(log n) time

Uploaded by

PaVan Nelakuditi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 33

Red Black Trees

Colored Nodes Definition Binary search tree. Each node is colored red or black. Root and all external nodes are black. No root-to-external-node path has two consecutive red nodes. All root-to-external-node paths have the same number of black nodes

Red Black Trees


Colored Edges Definition Binary search tree. Child pointers are colored red or black. Pointer to an external node is black. No root to external node path has two consecutive red pointers. Every root to external node path has the same number of black pointers.

Example Red-Black Tree


10

40

30
35

45

20 25

60

Properties
The height of a red black tree that has n (internal) nodes is between log2(n+1) and 2log2(n+1).

Properties
Start with a red black tree whose height is h; collapse all red nodes into their parent black nodes to get a tree whose node-degrees are between 2 and 4, height is >= h/2, and all external nodes are at the same level.

Properties
10

7 3 8 30 35

40 45

20
25

60

Properties
Let h>= h/2 be the height of the collapsed tree. Internal nodes of collapsed tree have degree between 2 and 4. Number of internal nodes in collapsed tree >= 2h-1. So, n >= 2h-1 So, h <= 2 log2 (n + 1)

Properties
At most 1 rotation and O(log n) color flips per insert/delete. Priority search trees.
Two keys per element. Search tree on one key, priority queue on other. Color flip doesnt disturb priority queue property. Rotation disturbs priority queue property. O(log n) fix time per rotation => O(log2n) overall time for AVL.

Properties
O(1) amortized complexity to restructure following an insert/delete. C++ STL implementation java.util.TreeMap => red black tree

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.

Classification Of 2 Red Nodes/Pointers


gp pp

XYz

a b X => relationship between gp and pp. pp left child of gp => X = L. Y => relationship between pp and p. p left child of pp => Y = L.

z = b (black) if d = null or a black node. z = r (red) if d is a red node.

Color flip.
gp
pp p a b d c

XYr
gp
pp p a b d c

Move p, pp, and gp up two levels. Continue rebalancing.

LLb
Rotate.
gp
pp p a
x y z x

y
z

d c a b c d

Done! Same as LL rotation of AVL tree.

LRb
Rotate.
gp
pp
x z x y z

a
b

d p c

Done! Same as LR rotation of AVL tree. RRb and RLb are symmetric.

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.

Delete A Black Leaf


10

7 3 8 30 35

40 45

20 25

60

Delete 8.

Delete A Black Leaf


10

py
3

40

y
5 20

30 35

45

60

y is root of deficient subtree. py is parent of y.

25

Delete A Black Degree 1 Node


10

7 3 8 30 35

40

py
45

20 25

60

Delete 45. y is root of deficient subtree.

Delete A Black Degree 2 Node


10

7 3 8 30 35

40 45

20 25

60

Not possible, degree 2 nodes are never deleted.

Rebalancing Strategy
If y is a red node, make it black.
10

7
3 8 30 35

40

py
45

20 25

60

Rebalancing Strategy
Now, no subtree is deficient. Done!
10

7
3 8 30 35

40

py
45

20 25

60

Rebalancing Strategy
y is a black root (there is no py). Entire tree is deficient. Done!
10

y
40

7 3 8 30 35

45

20 25

60

Rebalancing Strategy
y is black but not the root (there is a py).
py
v a b y

Xcn y is right child of py => X = R. Pointer to v is black => c = b. v has 1 red child => n = 1.

Rb0 (case 1, py is black)


py v a b v y a b py y

Color change. Now, py is root of deficient subtree. Continue!

Rb0 (case 2, py is red)


py v a b v y a b py y

Color change. Deficiency eliminated. Done!

Rb1 (case 1)
py v a b y a v py b y

LL rotation. Deficiency eliminated. Done!

Rb1 (case 2)
py v y v w py

a
b

w
c

LR rotation. Deficiency eliminated. Done!

Rb2
py v y v w py

a
b

w
c

LR rotation. Deficiency eliminated. Done!

Rr(n)
n = # of red children of vs right child w.

py

v
a b

w
c

Rr(0)
py v a b y a v py b y

LL rotation. Done!

Rr(1) (case 1)
py v y v w py

a
b

w
c

LR rotation. Deficiency eliminated. Done!

Rr(1) (case 2)
py v y v x py

a
b c

w
x d

a
b

w
c

Rotation. Deficiency eliminated. Done!

Rr(2)
py v y v x py

a
b c

w
x d

a
b

w
c

Rotation. Deficiency eliminated. Done!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy