




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
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
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Jared Saia
University of New Mexico
Binary Search Trees
Red-Black Trees
Class Evaluation
Hash Tables implement the Dictionary ADT, namely:
Insert(x) -
(1) expected time, Θ(
n ) worst case
Lookup(x) -
(1) expected time, Θ(
n ) worst case
Delete(x) -
(1) expected time, Θ(
n ) worst case
tionary ADT, namely: Red-Black trees (a kind of binary tree) also implement the Dic-
Insert(x) -
(log
(^) n
) time
Lookup(x) -
(log
(^) n
) time
Delete(x) -
(log
(^) n
) time
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)
Search trees can implement some other important op-
erations...
Insert
Lookup
Delete
Minimum/Maximum
Predecessor/Successor
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
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)
Correctness?
Run time?
Tree-Search(x,k){
if (x=nil) or (k = key(x)){
return x;
if (k
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(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;}
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 .
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
“Shut up brain or I’ll poke you with a Q-Tip” - Homer Simpson
Note that the average depth of a node in
is
n 1
x ∑ ∈ T
d ( x, T
n 1 P (^) ( T (^) )
Thus we want to show that
n (^) log
(^) n
)
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
this recurrence isIn your hw (problem 7-2), you showed that the solution to
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
)