Homework Assignment VI Solution


1(18 points) For each of the three trees shown here, list the order in which the nodes are visited in:
a. preorder
b. postorder
c. inorder
d. level order


tree1
Pre: E R L I T S D G H
Post: L T S I R G H D E
In: L R T I S E G D H
Level: E R D L I G H T S

tree2
Pre: A B C D E F G H I
Post: C H F I G E D B A
In: B C D E F H G I A
Level: A B D C E G F I H

tree3
Pre: * 2 - + 4 / 9 3 1
Post: 2 4 9 / 3 + 1 - *
In: 2 * 4 + 9 / 3 - 1
Level: * 2 - + 1 4 / 9 3

2. Define the height of an empty tree as 0 and the height of a nonempty tree as the number of nodes on the longest root-to-leaf path.
a. (1 point) What is the minimum height of a binary tree that contains 31 nodes?
height = 5

b. (1 point) What is the maximum height of a binary tree that contains 31 nodes?

height = 31, all in a line

c. (5 points) Write a recursive C++ function that inputs, as an argument, a binary tree (i.e. a pointer to the root node) and outputs the height of the tree.

int height (Node* t){
	if (!t) return 0;
	int l = height(t->left);
	int r = height(t->right);
	if (l > r) return l+1; else return r+1;
}


Last Updated: May 115, 1997, 1997 7:52 pm by

Harriet Fell
College of Computer Science, Northeastern University
360 Huntington Avenue #161CN,
Boston, MA 02115
Internet: fell@ccs.neu.edu
Phone: (617) 373-2198 / Fax: (617) 373-5121
The URL for this document is: http://www.ccs.neu.edu/home/fell/COM1101/HW/HW6/hw6Solution.html