Red-Black Trees - Data Structures and Algorithms | CS 361L, Study notes of Computer Science

Material Type: Notes; Class: Data Structures and Algorithms; Subject: Computer Science; University: University of New Mexico; Term: Fall 2005;

Typology: Study notes

Pre 2010

Uploaded on 07/23/2009

koofers-user-19a
koofers-user-19a 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 361, Lecture 20
Jared Saia
University of New Mexico
Outline
Red-Black Trees
1
Randomly Built BSTs
We want to answer the question: “What will b e the average
depth of a node in a randomly built tree?”
Define the random variable Xto be the depth of a node
chosen uniformly at random in the tree
Xtakes on npossible values, it takes on each value with
probability 1/n
2
Our Problem
For a tree Tand node x, let d(x, T ) be the depth of node x
in T
Define the total path length, P(T), to b e the sum over all
nodes xin Tof d(x, T )
Then
E(X) = 1
nX
xT
d(x, T )
=1
nP(T)
Thus we want to show that P(T) = O(nlogn)
3
pf3
pf4
pf5

Partial preview of the text

Download Red-Black Trees - Data Structures and Algorithms | CS 361L and more Study notes Computer Science in PDF only on Docsity!

CS 361, Lecture 20

Jared Saia

University of New Mexico

Outline

Red-Black Trees

Randomly Built BSTs

depth of a node in a randomly built tree?”We want to answer the question: “What will be the average

Define the random variable

X

to be the depth of a node

chosen uniformly at random in the tree

X

takes on

n

possible values, it takes on each value with

probability 1

/n

Our Problem

For a tree

T

and node

x , let

d ( x, T

(^) ) be the depth of node

x

in

T

Define the total path length,

P

T

(^) ), to be the sum over all

nodes

x

in

T

of

d ( x, T

Then

E

X

n 1

x ∑ ∈ T

d ( x, T

n 1 P (^) ( T (^) )

Thus we want to show that

P

T

O

n (^) log

(^) n

)

Analysis

“Shut up brain or I’ll poke you with a Q-Tip” - Homer Simpson

Let

T

l ,

T r

be the left and right subtrees of

T

respectively.

Let

n

be the number of nodes in

T

Then

P

T

P

T

l ) +

P

T

r ) +

(^) n

(^) 1. Why?

Analysis

Let

P

n ) be the expected total depth of all nodes in a ran-

domly built binary tree with

n

nodes

Note that for all

i , 0

i ≤

n (^) −

(^) 1, the probability that

T

l has

i

nodes and

T

r

has

n (^) −

i (^) −

(^) 1 nodes is 1

/n

Thus

P

n ) =

n 1 ∑

n − 1

i

P

i ) +

P

n (^) −

i −

(^) 1) +

(^) n

(^) −

P Analysis

n )

n 1

n − 1

i=0 ∑

P

i ) +

P

n (^) −

i (^) −

(^) n

(^) −

n 1 ( n − 1

i=0 ∑

P

i ) +

P

n (^) −

i −

(^) 1)) +

n 1 ( n − 1

i=0 ∑

n (^) −

n 1 ( n − 1

i=0 ∑

P

i ) +

P

n (^) −

i −

(^) 1)) + Θ(

n )

n 2 ( n − 1

k=1 ∑

P

k )) + Θ(

n )

Analysis

We have

P

n ) =

n 2 ( ∑

n − 1

k

P

k )) + Θ(

n )

This is the same as the recurrence for randomized Quicksort

rence isRecall from hw problem 7-2, that the solution to this recur-

P

n ) =

O

n (^) log

(^) n

)

What is a RB-Tree

A RB-Tree is a balanced binary search tree

The

height

of

the

tree

is

always

O

(log

(^) n

)

where

n

is

the

number of nodes in the tree

RB Trees

and parent pointerEach node has a “color” field in addition to a key, left, right,

sponding pointer field will contain the value NILIf the child or parent of a node does not exist, the corre-

internal nodes of the tree(leaves) of the tree, and say that all key-bearing nodes areWe will say that these NIL’s are pointers to external nodes

Red-Black Properties

A BST is a red-black tree if it satisfies the RB-Properties

  1. For each node, all paths from the node to descendant leaves4. If a node is red, then both its children are black3. Every leaf (NIL) is black2. The root is black1. Every node is either red or black

contain the same number of black nodes

Example RB-Tree

Black Height

Black-height

of a node

x , bh(x) is the number of black nodes

on any path from, but not including

x

down to a leaf node.

paths have the same number of black nodesNote that the black-height of a node is well-defined since all

the rootThe black-height of an RB-Tree is just the black-height of

Key Lemma

Lemma: A RB-Tree with

n

internal nodes has height at most

2 log(

n

  1. The subtree rooted at the nodeProof Sketch:

x

contains at least

bh

( x ) −

(^) 1 internal nodes

  1. For the root

r ,

bh

( r )

h/

2, thus

n

h/

2

Taking

logs of both sides, we get that

h

(^) 2 log(

n (^) + 1)

Proof

  1. The subtree rooted at the node

x

contains at least 2

bh

( x ) −

internal nodes. Show by induction on the height of

x .

BC: If the height of

x

is 0,

then

x

is a leaf,

and subtree

rooted at

x

does indeed contain 2

0

(^) 1 = 0 internal nodes

IH: For all nodes

y

of height less than

x , the subtree rooted

at

y

contains at least 2

bh

( y ) −

(^) 1 internal nodes.

IS: Consider a node

x

which is an internal node with two

children(all internal nodes have two children).

Each child

has black-height of either

bh

x ) or

bh

x ) −

1 (the former if

it is red, the latter if it is black).

Since the height of these

children is less than

x , we can apply the inductive hypothesis

to conclude that each child has at least 2

bh

( x ) − 1

(^) 1 internal

nodes. This implies that the subtree rooted at

x

has at least

bh

( x ) − 1 −^

(^) 1) + (

bh

( x ) − 1 −^

(^) 1) + 1 = 2

bh

( x ) −^

(^) 1 internal nodes.

This proves the claim.

Maintenance?

tained?How do we ensure that the Red-Black Properties are main-

holds?we re-arrange the new tree so that the Red-Black PropertyI.e. when we insert a new node, what do we color it? How do

How about for deletions?

In-Class Exercise

words, Show that Left-Rotate(x) maintains the BST Property. In other

show

that

if

the

BST

Property

was true

for

the

tree

after the operation.before the Left-Rotate(x) operation, then it’s true for the tree

Show that after rotation,

the BST property holds for the

entire subtree rooted at

x

Show that after rotation,

the BST property holds for the

subtree rooted at

y

the entire treeNow argue that after rotation, the BST property holds for