




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Data Structures and Algorithms; Subject: Computer Science; University: University of New Mexico; Term: Spring 2004;
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Jared Saia
University of New Mexico
Class Evaluation
Binary Trees
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)
Tree-Search(x,k){
if (x=nil) or (k = key(x)){
return x;
if (k
Let
h
be the height of the tree
The run time is
h )
Correctness???
Q1: What is the loop invariant for Tree-Search?
Q2: What is Initialization?
Q3: Maintenance?
Q4: Termination?
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
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
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).
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
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
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
(log
(^) n
) time
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;}
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))
Insert(T,x)
r
be the root of
p
be the last node pro-
cessed in that search
p
is nil (there is no tree), make
x
the root of a new tree
p, make
x
the left child of
p , else make
x
the right child of
p
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
Let
l ,
T r
be the left and right subtrees of
respectively.
Let
n
be the number of nodes in
Then
l ) +
r ) +
n (^) −
(^) 1. Why?
Let
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
l has
i
nodes and
r
has
n
−
(^) i (^) −
1 nodes is 1
/n
Thus
n ) =
n 1
∑
n − 1
i ) +
n (^) −
i (^) −
(^) n
(^) −
n )
n 1
n − 1
i=0 ∑
i ) +
n (^) −
(^) i (^) −
(^) n
(^) −
(^) 1)
n 1 ( n − 1
i=0 ∑
i ) +
n (^) −
(^) i (^) −
n 1 ( n − 1
i=0 ∑
n (^) −
n 1 ( n − 1
i=0 ∑
i ) +
n (^) −
(^) i (^) −
n )
n 2 ( n − 1
∑
k )) + Θ(
n )
We have
n ) =
n 2 ( ∑
n − 1
k )) + Θ(
n )
This is the same recurrence for randomized Quicksort
recurrence isIn your hw (problem 7-2), you show that the solution to this
n ) =
n (^) log
(^) n
)
n ) is the expected total depth of all nodes in a randomly
built binary tree with
n
nodes.
We’ve shown that
n ) =
n (^) log
(^) n
)
There are
n
nodes total
Thus the expected average depth of a node is
(log
(^) n
)
binary tree isThe expected average depth of a node in a randomly built
(log
(^) n
)
expected timeThis implies that operations like search, insert, delete take
(log
(^) n
) for a randomly built binary tree
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
would
not
be
randomly
built,
and
so
the
expected average depth of the nodes would not be
(log
(^) n
)
such a way that the height of the tree is alwaysA Red-Black tree implements the dictionary operations in
(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
(log
(^) n
) time
Next time we’ll see how to create Red-Black Trees