UMBC CMSC202, Computer Science II, Spring 1998,
Sections 0101, 0102, 0103, 0104 and Honors
Thursday April 30, 1998
Assigned Reading:
- A Book on C:
- Programming Abstractions in C:
Handouts (available on-line):
Topics Covered:
In this Lecture, we discuss the details of the implementation of the BSTree
class.
- After much discussion in the previous
lecture, we arrived at an interface for the BSTree class. (See
header file bstree.h.) In this
arrangement, a binary search tree (BST) is represented by an object of
type BSTree. The only data member of this object is a pointer to
BSTreeNode called root. All binary search tree operations are
member functions of BSTree, not of BSTreeNode. In fact, most BSTreeNode
members are hidden from the client. An empty BST is represented by
storing NULL in the root pointer.
- A BSTreeNode object has several data members: key,
stuff, left and right. Of these, only
stuff is publicly accessible. The BST is ordered according to
the int value stored in the key field. The stuff field
contains "auxiliary" data. In a real application, the stuff
field would be a pointer to a complex object, holding whatever data you
really want to store in the BST. The left and right
fields are BSTree objects (not pointers to BSTree) which represent the
left and right subtrees of this node.
- Back to the BSTree class, note that the return values of the
publicly accessible member functions have type void, int, BSTree*, or
data*. In the design of classes, you should consider making the
interface as uniform as possible. In this design, for example, the
member functions Search(), Min() and Max()
all return values of type BSTree*. The exception is the
RootData() which returns a pointer to data. This allows the
client to modify the stuff field of a node. Note that we do
not return a pointer to BSTreeNode in the publicly accessible member
functions. Thus, the client of this interface need to know nothing
about BSTreeNode objects. The functions ExtractMin() and
ExtractRoot() are used only by other member functions. So, it
makes sense to make ExtractMin() and ExtractRoot()
protected members.
- The implementation of the BSTree member functions are fairly
straightforward. (See bstree.C.)
The Insert() function is a typical example:
void BSTree::Insert(int x, data c) {
if (root == NULL) {
root = new BSTreeNode(x, c) ;
return ;
}
if (x <= root->key) {
root->left.Insert(x, c) ;
} else {
root->right.Insert(x, c) ;
}
}
Here we rely on recursion to recursively insert in either the left or the
right subtrees. In the base case, if we try to insert into an empty tree, we
make the root pointer point to a new BSTreeNode. Note that the
base case correctly handles inserting at a leaf and inserting the first
node into the tree.
- The most complex operation is the delete operation. The delete
operation is called RemoveRoot() in this interface because
deleting a node involves a two-step process. First you use
Search() to find a node, then you use RemoveRoot() to
delete that node. Since Search() returns a pointer to BSTree,
the proper way to think of the situation is that Search()
returns a pointer to a subtree. The node you are looking for is the
root of that subtree. In that case, removing the root of the subtree
would delete the node. The RemoveRoot() function simply calls
the ExtractRoot() member function. The ExtractRoot()
function follows the delete algorithm described in the previous lecture.
- Note that the destructor for the BSTreeNode class does not have to
do anything. Since the left and right fields are not
pointers, we don't have to (and can't) delete these. The destructors for
left and right are called automatically. Thus, the
entire tree is destroyed recursively.
- Testing the BSTree class:
- In our first test
program, we simply insert a few nodes in the BST and do
an inorder traversal. Note that since the key field of a BSTreeNode is
protected, we need to use the RootPrint() member function to
dump out the contents of a node. Also note that the two variables
in this program both have type BSTree*. In the last step, we explicitly
delete the BSTree. The sample run
shows that every node in the tree is destroyed.
- In our second test program,
we allow the user to search for a node and edit the stuff field of
that node. See sample run.
- In our third test program,
we allow the user to delete nodes from the binary search tree. The
sample run shows that deleting the
last node of the tree (leaving an empty tree) is handled correctly.
- In our last test program,
we point out that the client program cannot declare variables of
type BSTreeNode, because the BSTreeNode constructors are not
publicly accessible. We can declare pointers to BSTreeNode, but
the new statement
Nptr = new BSTreeNode ;
causes compilation problems. See sample
run for the compiler generated error statements.
- Student Course Evaluation Questionnaires (SCEQ) were handed out.
Last Modified:
22 Jul 2024 11:27:45 EDT
by
Richard Chang
Back up
to Spring 1998 CMSC 202 Section Homepage