Tree Program Assignment


Write a C++ program that will manipulate a binary tree. Your menu driven program should allow the following functions:

  1. Create a binary tree with N nodes where the integer data is stored in the text file tree.dat.
  2. Display the binary tree in Preorder.
  3. Display the binary tree in Inorder.
  4. Display the binary tree in Postorder.
  5. Count the number of nodes in the tree.
  6. Count the number of leaves in the tree.
  7. Sum the node values in the tree.
  8. Compute the height of the tree.
  9. Compute the width of the tree.
  10. Search for a value in the tree.
  11. Delete a node from the binary tree.
  12. Destroy the tree.
  13. Draw the tree with the root at the top and the leaves on the bottom.
Use the following data structure in your program:

	struct TreeNode;
	typedef TreeNode* TreePtr;
	struct TreeNode 
	{
		int data;
		TreePtr left;
		TreePtr right;
	};

When deleting a node from a binary tree, replace the deleted node with the greatest value off the left subtree of the node being deleted. Read these notes for a complete description.

To compute the Height and Width of a tree you will also need to write a Max(x,y) function. The function Max will return the greater value of x and y.

The Height of a binary tree is defined to be the number of nodes in the longest path from the root to a leaf of the binary tree.

The Width of a binary tree is defined to be the number of nodes in the longest path in the binary tree (which does not have to go through the root). The Width of an empty tree is 0. The Width of a non-empty tree is the maximum of the following three quantities.

Turn in your program with the following test data: