lab_trees

Tempestuous Trees

Assignment Description

In this lab we’ll explore some cool helper functions for binary trees, learn about creating helper functions and thinking both iteratively and recursively, and hopefully see some fancy ascii trees on the terminal!

Lab Insight

Trees are a very powerful structure for lookups and finding data quickly. Examples of use cases for this data structure includes search engine optimization and fast sorted data retrieval. CS 410, Text Information Systems, is a course that delves into topics involving text manipulation such as test search lookups. Trees can even be used for syntax and language grammar analysis which relates a lot with CS 421, Programming Languages and Compilers.

A new C++ thing

We’ll be using templates again in this lab, and unfortunately, this means we have to walk into a dark scary corner of C++. But hopefully we can take our lantern and cast some light here before any compiler errors bite.

The following function definitions won’t compile:

template <typename T>
Node * BinaryTree<T>::myHelperFunction(Node * node)
// The compiler doesn't know what a Node is, since the return type isn't scoped
// with the function.

So we have to scope it:

template <typename T>
BinaryTree<T>::Node * BinaryTree<T>::myHelperFunction(Node * node)

Using g++, the latter will show an error such as:

error: expected constructor, destructor, or type conversion before '*' token

Clang gives a more helpful error message:

fatal error: missing 'typename' prior to dependent type name 'BinaryTree<T>::Node'

Without going into the ugly details of it, this is happening because your compiler thinks Node is a member variable of the BinaryTree<T> class (since it’s a template). We can resolve this issue simply by adding a nice, friendly, typename keyword before our BinaryTree<T>::Node type. This lets the compiler know that Node really is a type, not a variable:

template <typename T>
typename BinaryTree<T>::Node * BinaryTree<T>::myHelperFunction(Node * node)

The above, fixed, definition compiles correctly. Since you’ll probably want to create your own helper functions for this lab, this is important to remember when you see the strange error above. You won’t be responsible for this nuance of templates on any exam in this class.

Getting Set Up

From your CS 225 git directory, run the following on EWS:

git pull
git fetch release
git merge release/lab_trees -m "Merging initial lab_trees files"

If you’re on your own machine, you may need to run:

git pull
git fetch release
git merge --allow-unrelated-histories release/lab_trees -m "Merging initial lab_trees files"

Part of good “git hygiene” is to run git pull before you do anything else.

Upon a successful merge, your lab_trees files are now in your lab_trees directory.

The code for this activity resides in the lab_trees/ directory. Get there by typing this in your working directory:

cd lab_trees/

A reference for the lab is provided for you in Doxygen form.

Testing Your Code

To test your code, compile using make:

make

Then run it with:

./treefun color

You will see that the output is colored — green means correct output, red means incorrect output, and underlined red means expected output that was not present. This mode is a bit experimental, and it might cause problems with your own debugging output (or other problems in general). To turn it off, simply leave off the “color” argument:

./treefun

Helper Functions and Recursion

You’ll want to be thinking about the following problems recursively. To do this, though, you’ll have to make your own helper functions to help implement the functions, so that you can recursively act differently on different nodes. There is room in the .h file for you to declare these extra functions. A helper function stub for height() has been provided for you.

The height() Function

There is a function called height() that returns the height of the binary tree. Recall that the height of a binary tree is just the length of the longest path from the root to a leaf, and that the height of an empty tree is -1.

We have implemented height() for you (see binarytree.cpp) to help you get a sense of recursive functions. Please read through the code, and ask questions if you are unsure of how it finds the height of a tree.

The printLeftToRight() Function

There is a function called printLeftToRight() that prints out the values of the nodes of a binary tree in order. That is, everything to the left of a node will be printed out before that node itself, and everything to the right of a node will be printed out after that node.

We have implemented printLeftToRight() for you (see binarytree.cpp). Please read through the code, and ask questions if you are unsure of how it works. Note that printLeftToRight() uses an in-order-traversal to print out the nodes of a tree. You will need to use one of the three traversals covered in lecture for some of the following functions.

The mirror() Function

The mirror() function should flip our tree over a vertical axis, modifying the tree itself (not creating a flipped copy).

For example, if our original tree was

                        ______ 8 ______
                 ______/               \______
            __ 5 __                            9 __
         __/       \__                             \__
       2               7                               10
     /   \           /
   1       4       6
          /
         3

Our mirrored tree would be

                        ______ 8 ______
                 ______/               \______
            __ 9                            __ 5 __
         __/                             __/       \__
       10                              7               2
                                         \           /   \
                                           6       4       1
                                                    \
                                                     3

The TreeTraversals Family

Class Hierarchy for TreeTraversals family:

We’ve already implemented PreorderTraversal class for you (see TreeTraversals/PreorderTraversal.h). Read the constructor and operator++ of TreeTraversal::Iterator to understand how they interact with TreeTraversals. Notice that TreeTraversal and TreeTraversal::Iterator are two separate classes. You can read more about iterators here. Your task is to implement the the following constructors and functions for InorderTraversal class:

  • InorderTraversal(typename BinaryTree<T>::Node* root)
  • void add(typename BinaryTree<T>::Node *& treeNode)

Test your InorderTraversal class:

clang++ -std=c++1y -stdlib=libc++ -lc++abi main.cpp -o main #run these commands INSIDE TreeTraversals folder
./main

The isOrdered() Family

The isOrdered() family includes two functions, one should be implemented iteratively, the other should be implemented recursively. Both functions return true if an in-order traversal of the tree would produce a nondecreasing list output values, and false otherwise. (This is also the criterion for a binary tree to be a binary search tree.)

For example, isOrdered() should return true on the following tree:

           __ 5 __
        __/       \__
      1               8
        \
          2
           \
            4

but false for

           __ 5 __
        __/       \__
      1               8
        \
          2
           \
            11

You’ll need to implement the following functions:

  • bool isOrderedIterative() const
  • bool isOrderedRecursive() const

Hint: What conditions need to be true for a tree to be in order (as defined above)? How can we check this iteratively? (Your Iterator class might help) How can we check this recursively? You might want to write your own helper functions for this exercise.

Testing Your Code with Catch

Run the Catch tests as follows (this requires your code to compile when run simply as make):

make test
./test

Submitting Your Work

The following files are used in grading:

  • binarytree.h
  • binarytree.cpp
  • TreeTraversals\InorderTraversal.h

All other files including any testing files you have added will not be used for grading.

Good luck!

Thanks to Nick Parlante/Stanford, Princeton’s CS 126, and CS 473 Spring 2011 for the exercises and inspiration.