Lecture Slides on Class Evaluation, Binary Tree | CS 361L, Study notes of Computer Science

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

Typology: Study notes

Pre 2010

Uploaded on 07/23/2009

koofers-user-aqh-1
koofers-user-aqh-1 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 361, Lecture 18
Jared Saia
University of New Mexico
Outline
Class Evaluation
Binary Trees
1
Binary Search Tree Property
Let xbe a node in a binary search tree. If yis a node in the
left subtree of x, then key(y)key(x). If yis a node in the
right subtree of xthen key(x)key(y)
2
Search in BT
Tree-Search(x,k){
if (x=nil) or (k = key(x)){
return x;
}
if (k<key(x)){
return Tree-Search(left(x),k);
}else{
return Tree-Search(right(x),k);
}
}
3
pf3
pf4
pf5
pf8

Partial preview of the text

Download Lecture Slides on Class Evaluation, Binary Tree | CS 361L and more Study notes Computer Science in PDF only on Docsity!

CS 361, Lecture 18

Jared Saia

University of New Mexico

Outline

Class Evaluation

Binary Trees

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)

Search in BT

Tree-Search(x,k){

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

return x;

if (k

Analysis

Let

h

be the height of the tree

The run time is

O

h )

Correctness???

Previous In-Class Exercise

Q1: What is the loop invariant for Tree-Search?

Q2: What is Initialization?

Q3: Maintenance?

Q4: Termination?

Loop Invariant Review

A useful tool for proving correctness is loop invariants.

Three

things must be shown about a loop invariant

Initialization:

Invariant is true before first iteration of loop

Maintenance:

If invariant is true before iteration

i , it is also

true before iteration

i (^) + 1

Termination:

When the loop terminates, the invariant gives

a property which can be used to show the algorithm is correct

Loop Invariant Review

When

Initialization

and

Maintenance

hold, the loop invari-

ant is true prior to every iteration of the loop

Similar

to

mathematical

induction:

must

show

both

base

case and inductive step

iteration is like the inductive stepthe base case. Showing the invariant holds from iteration toShowing the invariant holds before the first iteration is like

Maintenance

true that if keyMaintenance: Assume at the beginning of the procedure, it’s

k

exists in the tree that it is in the subtree

rooted at node

x .

There are three cases that can occur

during the procedure:

Case 1: key(

x ) is

k

. In this case, the procedure terminates

and returns

x , so the invariant continues to hold

Case 2:

k <

key(x).

In this case, by the

BST Property

all keys in the subtree rooted on the right child of

x

are

greater than

k

(since key(x)

k).

Thus, if

k

exists in the

subtree rooted at

x , it must exist in the subtree rooted at

left(x).

Case 3:k

key(x).

In this case, by the

BST Property

, All

keys in the subtree rooted on the right child of

x

are less

than

k

(since key(x)

k).

Thus, if

k

exists in the subtree

rooted at

x , it must exist in the subtree rooted at right(x).

Termination

By the loop invariant,

we

know

that when

the procedure

terminates, if

k

is in the tree, then it is in the subtree rooted

at

x

. If

k

is in fact in the tree, then

x

will never be nil, and so

keythe procedure will only terminate by returning a node with

k

. If

k

is not in the tree, then the only way the procedure

will terminate is when

x

is nil.

Thus, in this case also, the

procedure will return the correct answer.

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

Successor

The successor of a node

x

is the node that comes after

x

in

the sorted order determined by an in-order tree walk.

If all keys are distinct, the successor of a node

x

is the node

with the smallest key greater than

x

We can compute the successor of a node in

O

(log

(^) n

) time

Tree-Successor

Tree-Successor(x){

if (right(x) != null){

return Tree-Minimum(right(x));

while (y!=null and x=right(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 .

(i.e.

the lowest ancestor

of

x

whose key is

key(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

Deletion

and one is trickyCode is in book, basically there are three cases, two are easy

Case 1:

The node to delete has no children.

Then we just

delete the node

Case 2:

The node to delete has one child.

Then we delete

the node and “splice” together the two resulting trees

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

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

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

)

What to do?

such a way that the height of the tree is alwaysA Red-Black tree implements the dictionary operations in

O

(log

(^) n

),

where

n

is the number of nodes

all operations will always takeThis will guarantee that no matter how the tree is built that

O

(log

(^) n

) time

Next time we’ll see how to create Red-Black Trees