Docsity

Red-Black Trees - Data Structures - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

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

Red-Black Trees

2

Red-Black Trees

  • “Balanced” binary trees guarantee an O(lgn)

running time on the basic dynamic-set

operations

  • Red-black tree
    • Binary tree with an additional attribute for its nodes: color which can be red or black
    • The nodes inherit all the other attributes from the binary-search trees: key, left, right, p

Example: RED-BLACK-TREE

  • For convenience, we add NIL nodes and refer to them as the leaves of the tree. - Color[NIL] = BLACK

26

17 41

30 47

38 50

NIL NIL

NIL NIL NIL NIL^ NIL

NIL

Red-Black-Trees Properties

(Binary search tree property is satisfied)

  1. Every node is either red or black
  2. The root is black
  3. Every leaf (NIL) is black
  4. If a node is red , then both its children are black
    • No two consecutive red nodes on a simple path from the root to a leaf
  5. For each node, all paths from that node to a leaf

contain the same number of black nodes

Height of Red-Black-Trees

A red-black tree with n internal nodes

has height at most 2log(N+1)

  • Need to prove two claims first …

Claim 1

  • Any node x with height h(x) has bh(x) ≥ h(x)/
  • Proof
    • By property 4, at most h/2 red nodes on the path from the node to a leaf
    • Hence, at least h/2 are black

26

17 41

30 47

38 50

NIL NIL

NIL NIL NIL NIL^ NIL

NIL

h = 4 bh = 2 h = 3 bh = 2 h = 2 bh = 1 h = 1 bh = 1

h = 1 bh = 1 h = 2 bh = 1 (^) h = 1 bh = 1

Claim 2 (cont’d)

Proof: By induction on h[x]

Basis: h[x] = 0 ⇒

x is a leaf (NIL[T]) ⇒

bh(x) = 0 ⇒

# of internal nodes: 2^0 - 1 = 0

Inductive Hypothesis: assume it is true for

h[x]=h-

Inductive step:

  • Prove it for h[x]=h

NIL

x

Claim 2 (cont’d)

Prove it for h[x]=h

internal nodes at x=

internal nodes at l +

internal nodes at r + 1

Using inductive hypothesis:

internal nodes at x ≥ (2 bh(l)^ – 1) + (2 bh(r)^ – 1) + 1

x

l r

h

h-

Claim 2 (cont’d)

  • So, back to our proof:

internal nodes at x ≥ (2bh(l)^ – 1) + (2bh(r)^ – 1) + 1

≥ (2 bh(x) - 1^ – 1) + (2 bh(x) - 1^ – 1) + 1

= 2 · (2 bh(x) - 1^ - 1) + 1

= 2 bh(x)^ - 1 internal nodes

x

l r

h

h-

Height of Red-Black-Trees (cont’d)

A red-black tree with N internal nodes has height

at most 2log(N+1).

Proof:

N

  • Solve for h:

N + 1 ≥ 2 h/

log(N + 1) ≥ h/2 ⇒

h ≤ 2 log(N + 1)

root

l r

bh(root) = b^ height(root)^ = h

number of internal nodes

≥ 2b^ - 1 ≥ 2h/2^ - 1

Claim 2 Claim 1

16

InsertItem

What color to make the new node?

  • Red? Let’s insert 35!
    • Property 4 is violated: if a node is red, then both its children are black
  • Black? Let’s insert 14!
    • Property 5 is violated: all paths from a node to its leaves contain the same number of black nodes 26

17 41

30 47

38 50

Rotations

  • Operations for re-structuring the tree after insert

and delete operations

  • Together with some node re-coloring, they help restore the red-black-tree property
  • Change some of the pointer structure
  • Preserve the binary-search tree property
  • Two types of rotations:
  • Left & right rotations

19

Example: LEFT-ROTATE

Right Rotations

  • Assumptions for a right rotation on a node x:
    • The left child x of y is not NIL
  • Idea:
    • Pivots around the link from y to x
    • Makes x the new root of the subtree
    • y becomes x’s right child
    • x’s right child becomes y’s left child