Lab 6 - Binary Search Trees

Due: 5:00pm on Wednesday

The purpose of this lab is to review the book's code for the binary search tree (BST) and to extend the code to support preorder (VLR) and postorder (LRV) traversals.

For this lab, you will need to implement preorder and postorder traversals in BST.h from the book. Download the book's code with the command:

wget http://cs.calvin.edu/activities/books/c++/ds/2e/SourcePrograms/Chap12/Figure12.9/BST.h

Recall from Lab 4 that the book code is in Mac format, which means it has carriage returns (\r) instead of line feeds (\n) to mark the end of the line. Another method to convert the Mac formatted file to Unix format (besides the vi command we used in Lab 4) is the following Sleipnir command (run this command BEFORE opening the file in vi):

cat BST.h | tr '\r' '\n' > BST_new.h
The tr command substitutes a single character in the input with another character. In this case, we tell it change all the carriage returns into line feeds. After running this command, confirm that BST_new.h looks correct and then rename it BST.h.

The BST code from the book does not have "using namespace std;" after including iostream, so it will not compile as provided. Add the namspace line to fix those errors.

Some items to note in the BST code. The insert routine uses a non-recursive method to search the tree for the "empty" spot to put the new node. It uses a while loop to descend through the tree and find the "parent" node and then inserts the new node to either the left or right of the "parent" node (depending on the value to be inserted). A similar while loop is used to traverse the tree for the search operation. The non-recursive implementations of these two operations are slightly faster since they avoid function call overhead.

The BST.h file from the book already has two traversals: inorder and graph. The inorder traversal visits the nodes in LVR (left-visit-right) order, which outputs the tree in sorted, ascending order. The graph output gives a visual representation of the tree structure with the root on the leftmost side of the screen and the rest of the tree growing towards the right side of the screen.

You will be adding the preorder and postorder traversals to BST.h. For the preorder traversal, use inorder as a template but instead visit the nodes in VLR order. The output will not be sorted. For the postorder traversal, visit the nodes in LRV order.

Note that inorder, preorder and postorder will all use two functions: a public interface and a private recurssive function. The public function just takes the output stream to use for printing. The private helper function takes an output stream and the root of the subtree. Your public function should call the private function passing the root of the entire tree as the subtree to parse (see the inorder public function for an example).

The main function to use for this lab is lab6main.cpp. This tests the various traversals.

Email your modified BST.h file to me.