CS-240
Lab 11

Purpose:

Introduction to Binary Search Trees

Reading:

Nyhoff, Chapter 12, pp. 651-694

Due Date:

Have completed lab uploaded by your next lab session, bring hard copy to lab (either Tues. April 24 or Thurs. April 26)

Description:

Using the files:  BST.h and treetester.cpp
BST.h contains a Binary Search Tree class template.
Please study the following methods that are provided already:
Constructor() - constructs empty BST
Empty() - Check if BST is empty
Search() - Determine if item is in BST
Insert() - Insert item into BST
Inorder() - Perform inorder traversal of BST, displaying values stored in nodes
Delete() - Delete item from BST

Note that this template contains a BinNode class that represents the nodes of the binary tree.   BinNode has three data members:  data, left, and right, and two constructors:  default and explicit-value.

A typedef statement in the BST class declares type BinNodePointer as a pointer to BinNode (BinNode *).

Add the following to the BST class:
  1. Add preorder and postorder traversal functions.  Document in the same manner as Inorder.  Use auxiliary functions as well (as in Inorder()).
    Test by changing the comments around "Part 1" in treetester.cpp to single-line comments (//), compile, and run.
  2. Add a destructor to the class.  Unlike a linked list, a Binary Search Tree is done recursively using a postorder traversal.
    Algorithm: 
    1. Destructor - just passes the BST root to a recursive function Destroy()
    2. Destroy() - If the BST is not empty:
      • Call Destroy() recursively to destroy root's left child
      • Call Destroy() recursively to destroy root's right child
      • Delete root
    To test the destructor first put an output statement in the destructor to show when it gets called, and then change the comments around "Part 2" (as above), compile, and run treetester.cpp.
  3. Add a copy constructor to the class.  In order to do this, perform a preorder traversal, copying nodes in the process.
    Algorithm:
    1. Copy constructor - pass root of tree to be copied and root (i.e., this root, as reference parameter) to recursive function CopyTree().
    2. CopyTree() - If the original tree is empty, then 0 should be passed back to root to make it empty, Else:
      • Get a new node for root that contains the data in original's root.
      • Pass original's left subtree and root's left pointer to CopyTree, which will make root's left subtree a copy of the original's left subtree.
      • Repeat (b) for the right subtrees.
    Test the copy constructor by changing the comments around "Part 3", compile, and run treetester.cpp.
  4. Add an overloaded assignment operator function.  It should check against self-assignment, and if this is false, destroy the old BST and copy the new one.
    Test the overloaded assignment operator by changing the comments around "Part 4", compile, and run treetester.cpp.
  5. Add a function called Level() to the class.  It should retrun the level in the BST at which a specified item is located (e.g., the root is at level 0, its children are at level 1, etc.).
    Test this function by adding appropriate statements to treetester.cpp.
  6. Write function LevelByLevel() to traverse a tree level by level (breadth first) from left to right within each level (e.g., visit the root, then all nodes on level 1, then all nodes on level 2, etc.).  (Hint: the algorithm for this is not recursive, and uses a queue).
    Test this function by adding appropriate statements to treetester.cpp.

Last updated 04/16/2007 - RMW