CMSC 10700 Spring 2004
Sharon Salveter

Homework #5
BINARY TREES
Due Wednesday 5 May 11:59pm

A specification for a binary tree with N > 0 nodes, labeled 1...N, is presented to you as follows:

For example 6 3 2 1 5 4 6 1 2 3 4 5 6
represents the tree:
                        1
                       / \
                      /   \
                     2     4
                    /     / \
                   /     /   \
                  3     5     6

You are to write a program to read an arbitrary number of records, each representing the specification for a binary tree. For each tree specification, build the specified tree using dynamic allocation, and print information about the tree.

The information to print about each tree is:

You must write a separate, recursive function to compute each piece of information. While it is possible to compute the required information without actually building the tree, you must build the tree. As usual, you should maintain your own freespace.

The program you turn in must run on the data I provide, which will be available on the website on Tuesday 4 May.

Don't forget input error checking. For example, there may not be enough numbers on the record, or the label of some node may not be in the range 1...N. In such cases, print an appropriate message and go on to process the next record.

Think about: Does every sequence of 2N+1 numbers represent a binary tree? For example, the sequence 5 2 1 4 3 5 1 5 2 4 3 has the correct number of values in the right range. Is there a 5-node binary tree with these inorder and preorder traversals?