Binary Search Trees
Search trees are data structures that support
many dynamic set operations including SEARCH,
MINIMUM, MAXIMUM, PREDECESSOR,
SUCCESSOR, INSERT, and DELETE. Thus, a
search tree can be used both as a dictionary and a priority queue.
Binary search trees are search trees in which the keys are
stored in such a way as to satisfy the binary search tree
property (see Figure 1):
Let
be a node in a binary search tree. If
is a node in the
left subtree of
, then
. If
is a node
in the right subtree of
, then
.
Figure 1: Binary Search Tree Property:
1 Traversing
There are three ways to traverse binary search trees:
- Inorder tree walk - visit the left subtree, the root, and right
subtree.
-
Preorder tree walk - visit the root, the left subtree and right
subtree.
-
Postorder tree walk - visit the left subtree, the right subtree,
and the root.
INORDER-TREE-WALK
1 if
2 then INORDER-TREE-WALK
3 print
/* or some other operation on
*/
4 INORDER-TREE-WALK
PREORDER-TREE-WALK
1 if
2 then print
3 PREORDER-TREE-WALK
4 PREORDER-TREE-WALK
POSTORDER-TREE-WALK
1 if
2 then POSTORDER-TREE-WALK
3 POSTORDER-TREE-WALK
4 print
2 Querying
2.1 Search
The TREE-SEARCH algorithm comes in two flavours: the
recursive and the iterative approaches. In the recursive approach,
we examine the input node
and compare its
with
the key we are looking for,
. If it matches, then we have found
a node with the correct key; and can therefore return
.
Otherwise, depending on the value of
and
, the
node we are after is either in the left subtree or right subtree of
node
.
TREE-SEARCH
1 if
or
2 then return
3 if
4 then return TREE-SEACH
5 else return TREE-SEACH
ITERATIVE-TREE-SEARCH
1 while
and
2 do if
3 then
4 else
5 return
2.2 Minimum and Maximum
The propertities of the binary search tree makes the
implementation of TREE-MINIMUM and TREE-MAXIMUM
very simple. For minimum, simply start at the root and ask "is
there a left child". If so, visit it and repeat the question. The
final left most node is the tree-minimum. Conversely, the right
most node is the tree-maximum.
TREE-MINIMUM
1 while
2 do
3 return
TREE-MAXIMUM
1 while
2 do
3 return
2.3 Successor
The successor of a node
is the node with the next biggest
key. Essentially, this is the minimum of all the nodes with equal
or greater keys. If node
has a right sub-tree
, then its
successor is the minimum of
. Otherwise, its successor must be
the lowest ancestor of
whose left child is either an ancestor of
or
itself.
TREE-SUCCESSOR
1 if
2 then return TREE-MINIMUM
3
4 while
and
5 do
6
7 return
3 Data Manipulation
3.1 Insert
TREE-INSERT inserts a node
into tree
. The
algorithm starts at the root of the tree and traverses down the tree
following the rules of binary search trees. If the node to be
inserted is smaller, then it has to be inserted into the left
subtree, else it should be inserted in the right subtree. The
process continues until the appropriate subtree does not exist.
That is to say, the left/right child of the current node is
.
Node
is then inserted into the tree as the appropriate child of
the current node.
TREE-INSERT
1
2
3 while
4 do
5 if
6 then
7 else
8
9 if
10 then
/* The tree was empty. */
11 else if
12 then
13 else
3.2 Delete
TREE-DELETE deletes a node
from tree
. There
are three possible states for node
prior to deletion. It may
have no children, one child, or two children. The
TREE-DELETE algorithm works by considering each of these
possibilities and behaving accordingly. If
has no children,
then it is simply removed from the tree. For the case where
has
one child,
's left/right child pointer that is currently
directed at
is redirected to
's child (see
Figure 2). Lastly, if
has two children,
it's successor is removed from the tree and is used to replace
.
The algorithm follows:
TREE-DELETE
1 if
or
2 then
3 else
TREE-SUCCESSOR
4 if
5 then
6 else
7 if
8 then
9 if
10 then
11 else if
12 then
13 else
14 if
15 then
16 copy
's satellite data into
17 return
Figure 2: TREE-DELETE. If
has one child, it is simply
spliced out of the tree. Since
can be the left or right
child of its parent
, and it may have a left or right
child
itself, there are four possibilities (a), (b), (c),
and (d).
Figure 3: TREE-DELETE. If
has two children, we remove
its successor
. Node
is guaranteed to have at
most one child because it is the minimum of sub-tree
.
It's removal is thus either of the other two
TREE-DELETE cases. When this is done,
is used
to replace
.
File translated from
TEX
by
TTM,
version 3.67.
On 31 Mar 2006, 18:12.