Binary Search Trees and Red-Black Trees: Implementation and Analysis, Study notes of Computer Science

The implementation and analysis of binary search trees (bsts) and red-black trees, two data structures used to implement the dictionary abstract data type (adt). The properties of bsts, the operations of search trees, and the advantages of using search trees over hash tables. It also introduces red-black trees, a self-balancing binary search tree, and explains how they maintain a balanced tree. Examples, in-class exercises, and an analysis of the time complexity of search operations.

Typology: Study notes

Pre 2010

Uploaded on 07/23/2009

koofers-user-1ih-1
koofers-user-1ih-1 🇺🇸

5

(1)

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 361, Lecture 17
Jared Saia
University of New Mexico
Outline
Binary Search Trees
Red-Black Trees
Class Evaluation
1
Hash Tables
Hash Tables implement the Dictionary ADT, namely:
Insert(x) - O(1) expected time, Θ(n) worst case
Lookup(x) - O(1) expected time, Θ(n) worst case
Delete(x) - O(1) expected time, Θ(n) worst case
2
Red-Black Trees
Red-Black trees (a kind of binary tree) also implement the Dic-
tionary ADT, namely:
Insert(x) - O(log n) time
Lookup(x) - O(log n) time
Delete(x) - O(log n) time
3
pf3
pf4
pf5
pf8

Partial preview of the text

Download Binary Search Trees and Red-Black Trees: Implementation and Analysis and more Study notes Computer Science in PDF only on Docsity!

CS 361, Lecture 17

Jared Saia

University of New Mexico

Outline

Binary Search Trees

Red-Black Trees

Class Evaluation

Hash Tables

Hash Tables implement the Dictionary ADT, namely:

Insert(x) -

O

(1) expected time, Θ(

n ) worst case

Lookup(x) -

O

(1) expected time, Θ(

n ) worst case

Delete(x) -

O

(1) expected time, Θ(

n ) worst case

Red-Black Trees

tionary ADT, namely: Red-Black trees (a kind of binary tree) also implement the Dic-

Insert(x) -

O

(log

(^) n

) time

Lookup(x) -

O

(log

(^) n

) time

Delete(x) -

O

(log

(^) n

) time

Why BST?

Q: When would you use a Search Tree?

(e.g. “mission critical” code)A1: When need a hard guarantee on the worst case run times

(e.g.A2: When want something more dynamic than a hash table

don’t want to have to enlarge a hash table when the

load factor gets too large)

A3:

Search trees can implement some other important op-

erations...

Search Tree Operations

Insert

Lookup

Delete

Minimum/Maximum

Predecessor/Successor

What is a BST?

It’s a binary tree

and right childrenEach node holds a key and record field, and a pointer to left

Binary Search Tree Property

is maintained

Binary Search Tree Property

Let

x

be a node in a binary search tree. If

y

is a node in the

left subtree of

x , then key(y)

key(x).

If

y

is a node in the

right subtree of

x

then key(x)

key(y)

Analysis

Correctness?

Run time?

Search in BT

Tree-Search(x,k){

if (x=nil) or (k = key(x)){

return x;

if (k

Tree Min/Max

Tree

Minimum(x):

Return

the

leftmost

child

in

the

tree

rooted at x

Tree Maximum(x):

Return the rightmost child in the tree

rooted at x

Tree-Successor

Tree-Successor(x){

if (right(x) != null){

return Tree-Minimum(right(x));

while (y!=null and x!=left(y)){y = parent(x);}

y = parent(y);x = y;

return y;}

Successor Intuition

Case 1:

If right subtree of

x

is non-empty, successor(x) is

just the leftmost node in the right subtree

Case 2:

If the right subtree of

x

is empty and

x

has a suc-

cessor, then successor(x) is the lowest ancestor of

x

whose

left child is also an ancestor of

x .

Insertion

Insert(T,x)

  1. Let

r

be the root of

T

  1. Do Tree-Search(r,key(x)) and let

p

be the last node pro-

cessed in that search

  1. If

p

is nil (there is no tree), make

x

the root of a new tree

  1. Else if key(x)

p, make

x

the left child of

p , else make

x

the right child of

p

Analysis

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

Note that the average depth of a node in

T

is

n 1

x ∑ ∈ T

d ( x, T

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

Thus we want to show that

P

T

O

n (^) log

(^) n

)

Analysis

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 (^) −

(^) n

(^) −

P Analysis

n )

n 1

n − 1

i=0 ∑

P

i ) +

P

n (^) −

(^) i (^) −

(^) n

(^) −

(^) 1)

n 1 ( n − 1

i=0 ∑

P

i ) +

P

n (^) −

(^) i (^) −

n 1 ( n − 1

i=0 ∑

n (^) −

n 1 ( n − 1

i=0 ∑

P

i ) +

P

n (^) −

(^) i (^) −

n )

n 2 ( n − 1

k

P

k )) + Θ(

n )

Analysis

We have

P

n ) =

n 2 ( ∑

n − 1

k

P

k )) + Θ(

n )

This is the same recurrence for randomized Quicksort

this recurrence isIn your hw (problem 7-2), you showed that the solution to

P

n ) =

O

n (^) log

(^) n

)

Take Away

P

n ) is the expected total depth of all nodes in a randomly

built binary tree with

n

nodes.

We’ve shown that

P

n ) =

O

n (^) log

(^) n

)

There are

n

nodes total

Thus the expected average depth of a node is

O

(log

(^) n

)

Take Away

binary tree isThe expected average depth of a node in a randomly built

O

(log

(^) n

)

expected timeThis implies that operations like search, insert, delete take

O

(log

(^) n

) for a randomly built binary tree

Warning!

search treeIn many cases, data is not inserted randomly into a binary

I.e. many binary search trees are not “randomly built”

tree in almost sorted orderFor example, data might be inserted into the binary search

Then

the

BST

would

not

be

randomly

built,

and

so

the

expected average depth of the nodes would not be

O

(log

(^) n

)