The University of Texas at Austin
Binary Search Trees
Department of Computer Sciences
Professor Vijaya Ramachandran
Lectures 1819
CS357: ALGORITHMS, Spring 2006
1
Dictionaries
A
dictionary
is a data structure that supports the operations of
Search, Insert,
Delete
.
•
Search
(
S, k
) is given the set
S
and a key value
k
. It returns a pointer to an
element
x
in
S
such that
key
(
x
) =
k
. If there is no element in
S
with key value
k
it returns
NIL
.
•
Insert
(
S, x
) is given the set
S
and a pointer to an element
x
(which comes
with a key value supplied to it. It forms the set
S
∪ {
x
}
.
•
Delete
(
S, x
) is given the set
S
and
a pointer to an element
x
in
S
. It forms
the set
S
− {
x
}
, i.e., it removes element
x
from the set
S
.
There are two important variants of a dictionary.
•
A
static dictionary
is a restricted version that supports only the
Search
op
eration. The goal here is to come up with a compact representation of the set
while supporting very fast lookups.
•
When the elements of the set come from a totally ordered set, a dictionary
on a total order supports
Minimum, Maximum, Successor, Predecessor
operations in addition to the three standard dictionary operations.
2
Binary Search Trees
Recall that a
binary search tree
T
is a rooted, ordered binary tree with each node
holding a key from a totally ordered set in a manner that satisFes the following
binary
search tree property
for each node
x
in
T
:
•
key
(
x
)
≥
key
(
y
) for every node
y
in the subtree rooted at the left child of
x
.
•
key
(
x
)
≤
key
(
y
) for every node
y
in the subtree rooted at the right child of
x
.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentWe will use
p
(
x
) to denote parent of
x
,
left
(
x
) to denote the left child of
x
and
right
(
x
) to denote the right child of
x
. If any of these nodes are missing, that will be
denoted by
NIL
. For convenience we will assume that all keys stored in the key are
distinct.
Inorder Walk.
We can print out the keys in binary search tree
T
in sorted order in
linear time by performing an
inorder walk
on
T
.
Let
h
be the height of binary search tree
T
. Each of the following operations can be
performed on
T
in
O
(
h
) time.
Search
(
x, k
) determines if there is an element in the subtree rooted at
x
with key
value
k
.
Maximum
(
x
) ±nds the element with maximum key value in the subtree rooted at
x
.
Minimum
(
x
) ±nds the element with minimum key value in the subtree rooted at
x
.
Successor
(
x
) is given a pointer to an element
x
in
T
, and returns a pointer to the
smallest element in
T
with value greater than key value of
x
. Similarly, we have an
operation
Predecessor
(
T, x
).
Insert
(
x, k
) inserts a new node with key value
k
in the subtree rooted at
x
. This
operation is performed by searching for key value
k
, and then adding a new leaf at
the position at which the search terminates.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '06
 Ramachandran
 Algorithms, Binary Search, Redblack tree

Click to edit the document details