
CScD-320, Algorithms (Rolfe)
Homework 4
Options for submission:
In class: Thursday, 30 April
Assignment box: Friday, 1 May
Name:
1. Consider the tree in the figure on the right. What
node or nodes is/are
a. The tree’s root
b. Leaves
c. Parents (i.e., of any node, showing child
nodes) in the format (parent: child[, child])
d. Siblings (show as parenthesized pairs)
e. Ancestors of 70 [note: parents are ancestors]
f. Descendants of 60 [note: children are …]
2. Give the height and depth of each node of the tree in the figure (either as a table or by drawing
the tree and writing height and depth information beside each node).
3. Give the following traversals for the tree in the figure above:
a. Preorder
b. Inorder (since it is a binary tree)
c. Postorder
d. Level order (breadth-first)
4. Give the following traversals for the general tree to the
right:
a. Preorder
b. Postorder
c. Level order (breadth-first)
5. Draw this general tree as its associated binary tree.
6. Assume the following class attributes (note that you may not need to use some):
public class TreeNode
{ TreeNode left, parent, right;
int value;
Write the Java function to determine the number of nodes whose value field contains an even
number (that is, where node.value%2 == 0; alternatively, (node.value&1) == 0).
Here is the prototype:
int evenNodes ( TreeNode node );
Hint: Recursion is your friend.
Printed 2020 12 月 5 at 11:30