Docsity

Red Black Trees - Advanced Data Structures - Lecture Slides, Slides of Computer Science

Toggle sidebar
bg1
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.
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Red Black Trees - Advanced Data Structures - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

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 7 8 (^1 )

30

40

20 25

35

45 60

3

Properties

  • The height of a red black tree that has n

(internal) nodes is between log 2 (n+1) and

2log 2 (n+1).

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’-
  • So, h <= 2 log 2 (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 doesn’t disturb priority queue property.  Rotation disturbs priority queue property.  O(log n) fix time per rotation => O(log^2 n) 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 hasan 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.

LLb

  • Rotate.
    • Done!
    • Same as LL rotation of AVL tree.

y x a b

z c d a b

c

d

gp pp p x

y

z

LRb

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

y x a b

z c d b c

a

d

gp pp y p

x

z

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 8

(^1 )

30

40

20 25

35

45 60

3

  • Delete 8.

Delete A Black Degree 2 Node

10 7 8 (^1 )

30

40

20 25

35

45 60

3

  • Not possible, degree 2 nodes are never deleted.

Rebalancing Strategy

  • If y is a red node, make it black. 10 7 8 (^1 )

30

40

20 25

35

45 60

3 y

py

Rebalancing Strategy

  • Now, no subtree is deficient. Done!

60

10 7 8 (^1 )

30

40

20 25

35

3 45 y

py

Rebalancing Strategy

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

60

10 7 8 (^1 )

30

40

20 25

35

3 45

y

Rb0 (case 2, py is red)

  • Color change.
  • Deficiency eliminated.
  • Done!

a b

y

py v y a b

py v

Rb1 (case 1)

  • LL rotation.
  • Deficiency eliminated.
  • Done!

a b

y

py v (^) a b y

v py

Rb1 (case 2)

  • LR rotation.
  • Deficiency eliminated.
  • Done!

a

y

py v

b c

w c^ y

w py a b

v

Rb

  • LR rotation.
  • Deficiency eliminated.
  • Done!

a

y

py v

b c

w c^ y

w py a b

v

Rr(1) (case 1)

  • LR rotation.
  • Deficiency eliminated.
  • Done!

a

y

py v

b

w c

c y

w py a

v b

Rr(1) (case 2)

  • Rotation.
  • Deficiency eliminated.
  • Done!

a

y

py v

b

w

c d

x

d y

x py a

v

b c

w

Rr(2)

  • Rotation.
  • Deficiency eliminated.
  • Done!

a

y

py v

b

w

c d

x

d y

x py a

v

b c

w