Algorithms Homework 4: Tree Traversals and Node Count, Assignments of Algorithms and Programming

Instructions for homework 4 in the cscd-320 algorithms course. Students are required to identify nodes in a tree, calculate heights and depths, and perform various traversals. Additionally, they must write a java function to count nodes with even values in a given tree.

Typology: Assignments

Pre 2010

Uploaded on 08/16/2009

koofers-user-bp1
koofers-user-bp1 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
50
30
70
10
40
80
60
20
A
B
E
H
G
F
J
C
D

Partial preview of the text

Download Algorithms Homework 4: Tree Traversals and Node Count and more Assignments Algorithms and Programming in PDF only on Docsity!

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 50 70 10 40 80 20 60 A B E (^) F G H J C D