L11

advertisement
Binary Search Trees
BSTs 1
The general binary tree shown in the previous chapter is not terribly useful in practice.
The chief use of binary trees is for providing rapid access to data (indexing, if you will)
and the general binary tree does not have good performance.
Suppose that we wish to store data elements that contain a number of fields, and that
one of those fields is distinguished as the key upon which searches will be performed.
A binary search tree or BST is a binary tree that is either empty or in which the data
element of each node has a key, and:
1.
All keys in the left subtree (if there is one) are less than the key in
the root node.
2.
All keys in the right subtree (if there is one) are greater than (or
equal to)* the key in the root node.
3.
The left and right subtrees of the root are binary search trees.
* In many uses, duplicate values are not allowed.
Computer Science Dept Va Tech June 2006
Data Structures & OO Development I
©2006 McQuain & Ribbens
BST Insertion
BSTs 2
Here, the key values are characters (and only key values are shown).
Inserting the following key values in the given order yields the given BST:
D
D G H E B D F C
B
G
In a BST, insertion is always
at the leaf level. Traverse
the BST, comparing the new
value to existing ones, until
you find the right spot, then
add a new leaf node holding
that value.
C
E
D
H
F
What is the resulting tree if the (same) key values are inserted in the order:
B C D D E F G H
Computer Science Dept Va Tech June 2006
or
E B C D D F G H
Data Structures & OO Development I
©2006 McQuain & Ribbens
Searching in a BST
BSTs 3
Because of the key ordering imposed by a BST, searching resembles the binary search
algorithm on a sorted array, which is O(log N) for an array of N elements.
A BST offers the advantage of purely dynamic storage, no wasted array cells and no
shifting of the array tail on insertion and deletion.
D
B
A
G
C
E
H
Trace searching for the key value E.
Computer Science Dept Va Tech June 2006
Data Structures & OO Development I
©2006 McQuain & Ribbens
BST Deletion
BSTs 4
Deletion is perhaps the most complex operation on a BST, because the algorithm must
result in a BST. The question is: what value should replace the one deleted? As with
the general tree, we have cases:
-
Removing a leaf node is trivial, just set the relevant child pointer in the parent
node to NULL.
-
Removing an internal node which has only one subtree is also trivial, just set
the relevant child pointer in the parent node to target the root of the subtree.
D
B
G
NULL
C
E
D
Computer Science Dept Va Tech June 2006
Data Structures & OO Development I
H
F
©2006 McQuain & Ribbens
BST Deletion
-
BSTs 5
Removing an internal node which has two subtrees is more complex…
Simply removing the node would
disconnect the tree. But what value
should replace the one in the
targeted node?
To preserve the BST property, we
must take the smallest value from
the right subtree, which would be
the closest succcessor of the value
being deleted.
D
B
G
C
F
E
H
F
Fortunately, the smallest value will always lie in the left-most node of the subtree.
Computer Science Dept Va Tech June 2006
Data Structures & OO Development I
©2006 McQuain & Ribbens
BST Deletion
BSTs 6
So, we first find the left-most node
of the right subtree, and then swap
data values between it and the
targeted node.
E
B
Note that at this point we don’t
necessarily have a BST.
G
C
Now we must delete the copied
value from the right subtree.
F
E
H
F
That looks straightforward here since the node in question is a leaf. However…
-
the node will NOT be a leaf in all cases
-
the occurrence of duplicate values is a complicating factor
-
so we might want to have a DeleteRightMinimum() function to clean up at
this point
Computer Science Dept Va Tech June 2006
Data Structures & OO Development I
©2006 McQuain & Ribbens
Deleting the Minimum Value
BSTs 7
Suppose we want to delete the
value ‘E’ from the BST:
E
After swapping the ‘F’ with the ‘E’,
we must delete
We must be careful to not confuse
this with the other node containing
an ‘F’.
B
G
C
F
H
F
Also, consider deleting the value ‘G’. In this case, the right subtree is just a leaf node,
whose parent is the node originally targeted for deletion.
Moral: be careful to consider ALL cases when designing.
Computer Science Dept Va Tech June 2006
Data Structures & OO Development I
©2006 McQuain & Ribbens
BST Template Interface
BSTs 8
Here’s a partial BST template:
template <typename T> class BST {
private:
BinNodeT<T>* Root;
Arguably, one could
derive this from a
general binary tree
type, but there is
little merit in doing
so.
bool InsertHelper(const T& D, BinNodeT<T>*& sRoot);
bool DeleteHelper(const T& D, BinNodeT<T>* sRoot);
bool DeleteRightMinimum(BinNodeT<T>* sRoot);
T* const FindHelper(const T& D, BinNodeT<T>* sRoot);
const T* const FindHelper(const T& D, BinNodeT<T>* sRoot) const;
// additional member fn's not shown
public:
BST();
// create empty BST
BST(const T& D);
// root holds D
// deep copy support not shown
bool Insert(const T& D);
// insert element
bool Delete(const T& D);
// delete element
T* const Find(const T& D);
// return access to D
const T* const Find(const T& D) const; // return access to D
void Clear();
void Display(std::ostream& Out) const;
~BST();
};
// . . . continues with member function implementations . . .
Computer Science Dept Va Tech June 2006
Data Structures & OO Development I
©2006 McQuain & Ribbens
BST Constructor and Destructor
BSTs 9
The default BST constructor just initializes an empty tree by setting Root to NULL:
template <typename T> BST<T>::BST() {
Root = NULL;
}
The second BST constructor is entirely similar, merely calling the corresponding base
constructor.
The BST destructor is simply a trivial modification of the one shown earlier for the
general binary tree.
Computer Science Dept Va Tech June 2006
Data Structures & OO Development I
©2006 McQuain & Ribbens
BST Search Implementation
BSTs 10
The BST Find() function takes advantage of the BST data organization:
template <typename T> T* const BST<T>::Find(const T& toFind) {
if (Root == NULL) return NULL;
return (FindHelper(toFind, Root));
}
template <typename T>
T* const BST<T>::FindHelper(const T& toFind, BinNodeT<T>* sRoot) {
if (sRoot == NULL) return NULL;
if (sRoot->Element == toFind) {
return &(sRoot->Element);
}
if (toFind < sRoot->Element)
return FindHelper(toFind, sRoot->Left);
else
return FindHelper(toFind, sRoot->Right);
}
Computer Science Dept Va Tech June 2006
Data Structures & OO Development I
Uses operator== for the type T,
which is customized by the client for
the particular application at hand.
Search direction is
determined by
relationship of target
data to data in
current node.
©2006 McQuain & Ribbens
BST Insert Implementation
BSTs 11
The public Insert() function is just a stub to call the recursive helper:
template <typename T>
bool BST<T>::Insert(const T& D) {
return InsertHelper(D, Root);
}
Warning:
the BST definition in these
notes allows for duplicate
data values to occur, the
logic of insertion may need
to be changed for your
specific application.
The stub simply calls the helper function..
The helper function must find the appropriate place in the tree to place the new node.
The design logic is straightforward:
- locate the parent "node" of the new leaf, and
- hang a new leaf off of it, on the correct side
Computer Science Dept Va Tech June 2006
Data Structures & OO Development I
©2006 McQuain & Ribbens
BST Insert Helper
BSTs 12
The InsertHelper() function:
template <typename T>
bool BST<T>::InsertHelper(const T& D, BinNodeT<T>*& sRoot) {
if (sRoot == NULL) {
// found the location
BinNodeT<T>* Temp = new(nothrow) BinNodeT<T>(D);
if (Temp == NULL) return false;
sRoot = Temp;
return true;
}
// recursive descent logic goes next . . .
}
When the parent of the new value is found, one more recursive call takes place, passing
in a NULL pointer to the helper function.
Note that the insert helper function must be able to modify the node pointer parameter,
and that the search logic is precisely the same as for the find function.
Computer Science Dept Va Tech June 2006
Data Structures & OO Development I
©2006 McQuain & Ribbens
BST Delete Implementation
BSTs 13
The public Delete() function is very similar to the insertion function:
template <typename T>
bool BST<T>::Delete(const T& D) {
if ( Root == NULL ) return false;
return DeleteHelper(D, Root);
}
The DeleteHelper() function design is also relatively straightforward:
- locate the parent of the node containing the target value
- determine the deletion case (as described earlier) and handle it:
- parent has only one subtree
- parent has two subtrees
The details of implementing the delete helper function are left to the reader…
Computer Science Dept Va Tech June 2006
Data Structures & OO Development I
©2006 McQuain & Ribbens
Parent Pointers
BSTs 14
Some binary tree implementations employ parent pointers in the nodes.
- increases memory cost of the tree (probably insignificantly)
- increases complexity of insert/delete/copy logic (insignificantly)
- provides some unnecessary alternatives when implementing insert/delete
- may actually simplify the addition of iterators to the tree (later topic)
It is also useful to have some instrumentation during testing. For example:
- log the values encountered and the directions taken during a search
This is also easy to add, but it poses a problem since we generally do not want to see
such output when the BST is used.
I resolve this by adding some data members and mutators to the template that enable
the client to optionally associate an output stream with the object, and to turn logging
of its operation on and off as needed.
Computer Science Dept Va Tech June 2006
Data Structures & OO Development I
©2006 McQuain & Ribbens
Some Refinements
BSTs 15
The given BST template may also provide additional features:
- a function to provide the size of the tree
- a function to provide the height of the tree
- a function to display the tree in a useful manner
It is also useful to have some instrumentation during testing. For example:
- log the values encountered and the directions taken during a search
This is also easy to add, but it poses a problem since we generally do not want to see
such output when the BST is used.
I resolve this by adding some data members and mutators to the template that enable
the client to optionally associate an output stream with the object, and to turn logging
of its operation on and off as needed.
Computer Science Dept Va Tech June 2006
Data Structures & OO Development I
©2006 McQuain & Ribbens
Instrumentation
BSTs 16
The extended BST template:
template <typename T> class BST {
protected:
. . .
ostream* Log;
bool
loggingOn;
. . .
public:
. . .
bool setLogStream(ostream* const L);
bool logOn();
bool logOff();
};
The client may entirely ignore the
ability to log an execution trace, or
enable it and turn logging on and off at
will.
template <typename T>
bool BST<T>::logOn() {
if ( Log != NULL )
loggingOn = true;
return loggingOn;
}
Of course, it's important to avoid
dereferencing a null pointer…
template <typename T> T* BST<T>::InsertHelper(. . .) {
. . .
if ( Elem < (sRoot->Element) ) {
if ( loggingOn )
*Log << "Insert: going left from " << (sRoot->Element) << endl;
return InsertHelper(Elem, sRoot->Left);
}
. . .
}
Computer Science Dept Va Tech June 2006
Data Structures & OO Development I
©2006 McQuain & Ribbens
Adding an Iterator
BSTs 17
A BST iterator can be added in a very similar way to that shown earlier for the linked
list iterator.
As before:
- add a declaration of a class iterator within the BST declaration
- an iterator will store a pointer to a tree node; dereferencing it will return the address
of the data element within that node.
- implement the expected increment/decrement operators for the iterator
- add the expected iterator-supplying functions to the BST
- there's no case for an iterator-based insertion function in a BST, but there may be a
case for an iterator-based deletion function
- add a search function that returns an iterator
There are two considerations:
- what traversal pattern should the iterator provide?
- is it necessary to modify the BST implementation aside from adding support
functions?
Computer Science Dept Va Tech June 2006
Data Structures & OO Development I
©2006 McQuain & Ribbens
Iterator-based Find
BSTs 18
The public Insert() function is just a stub to call the recursive helper:
template <typename T>
typename BST<T>::iterator BST<T>::Find(const T& toFind) {
return FindHelper(toFind, Root);
}
The public stub simply calls the helper function:
template <typename T>
typename BST<T>::iterator BST<T>::FindHelper(const T& toFind,
BinNodeT<T>* sRoot) {
if (sRoot == NULL) return iterator(NULL);
if (sRoot->Element == toFind) {
return iterator(sRoot);
}
// recursive descent logic goes next . . .
}
Computer Science Dept Va Tech June 2006
Data Structures & OO Development I
©2006 McQuain & Ribbens
Iterator Increment Logic
BSTs 19
The only issue that is handled differently from the the linked list iterator is the pattern by
which an iterator steps forward or backwards within the BST.
Consider stepping forward as in an inorder traversal:
C
A
I
begin()
B
E
D
J
end()
G
F
H
The pattern is reasonably straightforward, but how can we move up from a node to its
parent within the BST?
Computer Science Dept Va Tech June 2006
Data Structures & OO Development I
©2006 McQuain & Ribbens
Client-side Use
BSTs 20
Here is a somewhat trivial use of the iterator-supplied traversal:
BST<int>::iterator It;
for (It = T.begin(); It != T.end(); It++) {
if ( *It <= Floor ) {
*It = Floor;
}
}
Note also that the client can implement an inorder search of the BST using the same
approach. In fact, the BST itself could do that instead of recursion.
One point here is that the code above shows how the client can traverse the structure
without knowing anything about the actual physical layout… compare to a traversal of an
STL vector, or even of a simple array.
Computer Science Dept Va Tech June 2006
Data Structures & OO Development I
©2006 McQuain & Ribbens
Balance in a BST
BSTs 21
However, a BST with N nodes does not always provide O(log N) search times.
D
B
A
G
C
E
H
B
A well-balanced BST. This
will have log(N) search
times in the worst case.
A
G
C
What if we inserted the
values in the order:
A B C D E G H
Computer Science Dept Va Tech June 2006
A poorly-balanced BST. This
will not have log(N) search
times in the worst case.
Data Structures & OO Development I
H
D
E
©2006 McQuain & Ribbens
Search Cost in a BST
BSTs 22
From an earlier theorem on binary trees, we know that a binary tree that contains L nodes
must contain at least 1 + log L levels.
If the tree is full, we can improve the result to imply that a full binary tree that contains N
nodes must contain at least log N levels.
So, for any BST, the there is always an element whose search cost is at least log N.
Unfortunately, it can be much worse. If the BST is a "stalk" then the search cost for the
last element would be N.
It all comes down to one simple issue: how close is the tree to having minimum height?
Unfortunately, if we perform lots of random insertions and deletions in a BST, there is no
reason to expect that the result will have nearly-minimum height.
Computer Science Dept Va Tech June 2006
Data Structures & OO Development I
©2006 McQuain & Ribbens
Cost of Insertion/Deletion in a BST
BSTs 23
Clearly, once the parent is found, the remaining cost of inserting a new node in a BST is
constant, simply allocating a new node object and setting a pointer in the parent node.
So, insertion cost is essentially the same as search cost.
For deletion, the argument is slightly more complex. Suppose the parent of the targeted
node has been found.
If the parent has only one subtree, then the remaining cost is resetting a pointer in the
parent and deallocating the node; that's constant.
But, if the parent has two subtrees, then an additional search must be carried out to find
the minimum value in the right subtree, and then an element copy must be performed, and
then that node must be removed from the right subtree (which is again a constant cost).
In either case, we have no more than the cost of a worst-case search to the leaf level, plus
some constant manipulations.
So, deletion cost is also essentially the same as search cost.
Computer Science Dept Va Tech June 2006
Data Structures & OO Development I
©2006 McQuain & Ribbens
Download