## Assignment 7, Due at 11:59pm, Sunday Nov 16

### Forewords

This assignment looks deceptively simple but it might take you any where from a few hours to infinity depending on how well you really understand recursion. Like all of the previous assignments in this course, this assignment does not require a lot of coding. (When I taught CSE 489/589, the programming projects required something like 3000-5000 lines of codes. The assignments we have seen thus far involved about 100-200 lines top.) But I strongly encourage you to think deeply "on paper" before delving into writing the code. No amount of "hacking around" is going to lead you anywhere if you don't know what you're doing on this assignment.

By now, I have discussed everything you needed to know to do this assignment. In particular, the assignment is not complex language-wise. The difficulty you will face is definitely not on the C++ side. There is an article by Joel Spolsky (co-founder of stackoverflow) that is a bit controversial, but well worth a read. I strongly recommend it.

Thus far looking things up on Google has been permitted. However, for this particular assignment please do not read/copy/modify anybody's code from any source. If the plagiarim software detects so, zero point shall be given (and F for the course for the second-time offenders). You are welcome to discuss "on paper", "on the board" with classmates!

### Objectives

• So far we have learned how to deal with pointers, traverse pointers along a linear chain like in a linked list, or along binary structures such as a binary tree, and doing recursion. This assignment combines those two techniques. They are crucial for dealing with data structures and algorithms operating on data structures.
• We have discussed several common ways of traversing binary trees. (In fact, most if not all of you have seen those *-order traversals from 115/116/119.) So let's put what we have learned into practice. To do this assignment well, you'll need to understand those *-order traversals very well. Here, * stands for pre, post, in, level, and their reverses.
• We don't traverse binary trees just to traverse them. Very often we need to traverse a binary tree (or any other data structure) to do some kind of computation on them. This assignment is an example of the kind of computation one does on binary trees.

### The Setting

We will develop a program that does the following. The program takes commands from users, as usual; the following commands are supported:

• `exit`: quits the program
• `newtree`: asks the user to enter an inorder sequence and a preorder sequence (of strings) of a binary tree. As we have discussed in class, these two orders are sufficient to uniquely reconstruct the tree. Once the user has entered the two sequences, the tree is the "current tree", and the old tree (from a previous typing of `newtree` command) is discarded.
• `preorder`: prints the current tree in preorder
• `inorder`: prints the current tree in inorder
• `postorder`: prints the current tree in postorder
• `levelorder`: prints the current tree in levelorder
• `ver`: prints the current tree vertically in a "pretty format."
• `hor`: prints the current tree horizontally in a "pretty format"
• `sym`: prints the current tree symmetrically in a "pretty format"
• I have written program called `hqn-printtree` following the above specification and compiled it under `timberlake`. You can download and run it (in `timberlake`) to see how it is supposed to work. To download it, use `wget` as usual:
```wget http://www.cse.buffalo.edu/~hungngo/classes/2014/Fall/250/assignments/hqn-printtree
chmod 700 hqn-printtree
```
Here is a sample run of the program:
```UB Tree Program. Version 0.7
Author: Hung Q. Ngo
Report bugs to hungngo@buffalo.edu
> newtree
Enter the preorder and inorder sequences for your tree, use unique names
Preor. seq.: root thisisverylong short 34 longer aaaaaaaaa bbbbbbb c
Inor. seq. : thisisverylong short root aaaaaaaaa longer bbbbbbb 34 c
> inorder
thisisverylong short root aaaaaaaaa longer bbbbbbb 34 c
> postorder
short thisisverylong aaaaaaaaa bbbbbbb longer c 34 root
> levelorder
root thisisverylong 34 short longer c aaaaaaaaa bbbbbbb

> preorder
root thisisverylong short 34 longer aaaaaaaaa bbbbbbb c
> ver
root
|__34
|  |__c
|  |
|  |__longer
|     |__bbbbbbb
|     |
|     |__aaaaaaaaa
|
|__thisisverylong
|__short
|
|__x

> hor
root__________________
|                     \
thisisverylong         34___________________
|             \        |                    \
x              short   longer_____           c
|          \
aaaaaaaaa   bbbbbbb

> sym
_________root__________
/                       \
thisisverylong                __34__
/              \              /      \
x              short       longer      c
/      \
aaaaaaaaa bbbbbbb

> newtree
Enter the preorder and inorder sequences for your tree, use unique names
Preor. seq.: a b c
Inor. seq. : a b c
> ver
a
|__b
|  |__c
|  |
|  |__x
|
|__x

> hor
a
|\
x b
|\
x c

> sym
a
/ \
x   b
/ \
x   c

>
```
Note that `NULL` nodes are represented by an `x`, so you can assume the input does not have any node whose payload is `x`. If a node has two `NULL` children then we don't print the two `x`'s. A `NULL` child is only printed when the node has at least one non-NULL child.

### What to do

The real task: as in many of the previous assignments, you are provided with the skeleton of the program with a few blank functions. Your task is to understand the code and implement the three empty-body functions. To download the code base, click here, or from your `timberlake` account type

```wget http://www.cse.buffalo.edu/~hungngo/classes/2014/Fall/250/assignments/printtree.tar.gz
gunzip printtree.tar.gz
tar -xvf printtree.tar
```

In the `printtree` directory you will find the source codes for the program. All commands are already implemented except for three commands `ver`, `hor`, and `sym`. Your job is to do the following:

1. Carefully read and understand the source codes.
2. Implement the three member functions `vertical()`, `horizontal()`, and `symmetric()`, left blank in `BinaryTree.cpp`.
3. You can modify two files: `BinaryTree.h` and `BinaryTree.cpp`. In particular, feel free to add more functions or data members to the `BinaryTree` class. (You will likely have to!)
When you submit your assignment, you will only submit two files: `BinaryTree.cpp` and `BinaryTree.h`. We will copy this file to a directory containing all other files and compile and grade. Don't modify any other files. This is to illustrate how you work within a code base that someone else has written. `ver` is much easier, so it would be wise to implement that first. Type `make` to compile the program and all commands will work. For `ver`, `hor` and `sym`, a line `TBD` will be printed.

### How to submit

```submit_cse250 BinaryTree.cpp
submit_cse250 BinaryTree.h
```
Note that the last line only works if you logged in to your CSE account and the files are there. All previous things can be done at home, as long as you remember to upload the final file to your CSE account and run the submit script from there.

• `0` point if your `BinaryTree.{h,cpp}` does not compile with the other files intact, using the same `Makefile` under `timbelake`'s `/usr/bin/g++`. Please make sure that your source files compile under `Timberlake`
• `5` points if your `BinaryTree.{h,cpp}` does compile. In other words, if you don't do anything, just re-submit the file, you'll get 5 points.
• `10` points if your code is well-indented, with a maximum of 80 characters per column. Each indentation is between 3 to 8 spaces. Absolutely no tab character occurs in the code.
• `30` points if `ver` works.
• `30` points if `hor` works.
• `25` points if `sym` works.