Computer Science 173
Intermediate Computer Programming

Denison

Homework 11: The Binary Search Tree ADT
 Linked Implementation

Overview

Linked List implementations of collections of data have advantages in both storage and operation efficiency for insert and remove over their array implementation counterparts. They are not, however, well suited for efficient search.

This deficiency can be corrected with the use of a tree structure in which to store our data.  

Binary Search Tree (BST) ADT

BST ( )

Preconditions: None.
Postconditions: Creates an empty BST capable of holding DT type data items.

BST ( const BST & src )

Preconditions: None
Postconditions: Creates a new BST which is a deep copy of the passed src BST.

~BST ( )

Preconditions: None.
Postconditions: Destroys the BST, cleaning up all resources associated with the object.

void insert ( const DT & item )

Preconditions: None.
Postconditions: The item has been inserted into the BST. Location of the insert occurs in such a way that the binary search tree invariant is maintained:  For all vertices, the subtree rooted at the left child is comprised of elements less than the value at the current vertex (item), and the subtree rooted at the right child is comprised of elements greater than the value at the current vertes (item).

void remove ( const DT & item )

Preconditions: item is present in the BST
Postconditions: The item has been removed from the BST. Remove must maintain the binary search tree invariant.

bool find ( const DT & searchitem )

Preconditions: None.
Postconditions: Searches the BST for a data item with key equal to that of searchItem. If the data item is found,  the function returns true. Otherwise, the function returns false.

int leafCount ( )

Preconditions: None.
Postconditions: Returns the number of leaves in the current BST. Implementation of this function (perhaps through a private auxiliary function) should be recursive.

BST & operator= ( const BST & rhs )

Preconditions: None.
Postconditions: Previous BST object resources have been returned to the system and the current BST has become a copy of the given rhs. The current BST is returned.

bool isEmpty ( ) const

Preconditions: None.
Postconditions: Return value is true if the BST contains no items, and false otherwise.

void inorder ( ostream & stream ) const

Preconditions: DT type supports stream insertion to an output stream.
Postconditions: Outputs the contents of the BST to the given stream. Form of the output must be strictly adhered to. The output uses '(' and ')' to denote the beginning and the end of the BST. The output uses space separation between elements of the BST as well as between the beginning '(' and the first element, and the ending ')' and the last element. The elements themselves are output in inorder, so this output should be an ascending order display of the elements in the BST.

void displayPreorder ( ostream & stream ) const

Preconditions: DT type supports stream insertion to an output stream.
Postconditions: Outputs the contents of the BST to the given stream. Elements are output in preorder, so the root of the tree would be the first item output. Each output line consists of three values: first the data value of the current node, next the data value of
the left child, finally the value of the right child. Any node whose left or right child is NULL should output a hyphen ('-') in place of the data value of the child. The output should also include a header line specifying the three columns. See exercise 21 (sec 12.4, pg. 697) for an example.

Your implementation should also include operator overload for both >> and <<, and should use an inner class for a private binary node (BinNode) used by the BST class. Format for the input when overloading stream extraction should be a '(' followed by the set of elements to insert followed by a ')'. All tokens are whitespace separated.

Extra credit is also available for this assignment. Choices for extra credit include building a template class version of the BST ADT; realizing an array based implementation of the BST ADT, and smaller extra credit for additional functions of sufficient complexity. See, for example, the levelByLevel() function dexcribed in exercise 27, and the nonrecursive remove() function of exercise 28.

Driver

The BST driver program should allow the notion of two BST objects, the "current" BST and the "alternate" BST. Single BST object operations pertain to the "current" BST. For the copy constructor and the assignment overload, the "current" BST is the one that gets updated (is logically the lhs), and the "alternate" BST is the one that is copied from (is logically the rhs). There is also a command to switch the roles of the two BSTs, making the current the alternate and the alternate the current.

The submitted driver should be designed for the int version of the BST class.

Test BST Driver Commands
Command
Action
C
Create a new BST object (using the new C++ operator), making it the current BST. This always invokes the default constructor.
D
Destroy the current BST object
+x
The '+' command performs an insert operation on the current BST. The value to insert (x) immediately follows the '+' and is of type consistent with the type of the BST (an integer BST for your submission). In general, this should be read into an DT typed variable.
-x
Perform a remove operation of the specified item.
*
Display to cout the entire BST in inorder. Output should conform to the specification given previously, and should be implementable in the driver through stream insertion.
T
Display to cout the preorder table version of the BST, as generated by the displayPreorder() function.
?x
Search for the given item (x). Report the value of the search function, TRUE if x was found or FALSE if x was not found.
E
Report whether the BST is empty, printing TRUE or FALSE.
N
Report the number of leaves in the BST.
=
Demonstrate assignment overload, assigning the alternate BST to the current BST.
&
Demonstrate copy constructor, creating a new current BST from the alternate BST.
~
Switch the current BST with the alternate BST.
#
Ignore all following characters up to the following newline, outputing these characters as its argument.
X
Delete all elements in the BST.
H
Print a help message with the supported commands and a brief description of their actions.
Q
Quit the test program.

You will also detail a test plan that gives good coverage demonstrating that your implementation works.