Docsity

Algorithms - Solved Homework 4 | CSCD 320, Assignments of Algorithms and Programming

Toggle sidebar
bg1
CScD-320, Algorithms
Homework 4
Options for submission:
In class: Thursday, 1 May
Assignment box: Friday, 2 May
Solution Set
1. Consider the tree in the figure on the right. What
node or nodes is/are
a. The tree’s root
40
b. Leaves
10, 30, 50, 70
c. Parents (i.e., of any node, showing child nodes)
in the format (parent: child[, child])
(40:20, 60), (20: 10, 30), (60: 50, 80), (80: 70)
d. Siblings
(20, 60), (10, 30), (50, 80)
e. Ancestors of 70 [note: parents are ancestors]
80, 60, 40
f. Descendants of 60 [note: children and …]
50, 80, 70
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 write height and depth information beside each node).
3. Give the following traversals for the tree in the figure above:
a. Preorder 40 20 10 30 60 50 80 70
b. Inorder (since it is a binary tree) 10 20 30 40 50 60 70 80
c. Postorder 10 30 20 50 70 80 60 40
d. Level order (breadth-first) 40 20 60 10 30 50 80 70
4. Give the following traversals for the general tree to the
right:
a. Preorder A B E F C G D H J
b. Postorder E F B G C H J D A
c. Level order (breadth-first) A B C D E F G H J
5. Draw this general tree as its associated binary tree.
Ht: 4
Dpth: 1
Ht: 3
Dpth: 2
Ht: 1
Dpth: 4
Ht: 2
Dpth: 3
Ht: 1
Dpth: 3
Ht: 1
Dpth: 3
Ht: 1
Dpth: 3
Ht: 2
Dpth: 2
50
30
70
10
40
80
60
20
A
B
E
H
G
F
J
C
D
pf2

Partial preview of the text

Download Algorithms - Solved Homework 4 | CSCD 320 and more Assignments Algorithms and Programming in PDF only on Docsity!

CScD-320, Algorithms

Homework 4

Options for submission:

In class: Thursday, 1 May

Assignment box: Friday, 2 May

Solution Set

  1. Consider the tree in the figure on the right. What node or nodes is/are a. The tree’s root 40 b. Leaves 10, 30, 50, 70 c. Parents (i.e., of any node, showing child nodes) in the format (parent: child[, child]) (40:20, 60), (20: 10, 30), (60: 50, 80), (80: 70) d. Siblings (20, 60), (10, 30), (50, 80) e. Ancestors of 70 [ note: parents are ancestors] 80, 60, 40 f. Descendants of 60 [ note: children and …] 50, 80, 70
  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 write height and depth information beside each node).
  3. Give the following traversals for the tree in the figure above : a. Preorder 40 20 10 30 60 50 80 70 b. Inorder (since it is a binary tree) 10 20 30 40 50 60 70 80 c. Postorder 10 30 20 50 70 80 60 40 d. Level order (breadth-first) 40 20 60 10 30 50 80 70
  4. Give the following traversals for the general tree to the right: a. Preorder A B E F C G D H J b. Postorder E F B G C H J D A c. Level order (breadth-first) A B C D E F G H J
  5. Draw this general tree as its associated binary tree. Ht: 4 Dpth: 1 Ht: 3 Dpth: 2 Ht: 1 Dpth: 4 Ht: 2 Dpth: 3 Ht: 1 Dpth: 3 Ht: 1 Dpth: 3 Ht: 1 Dpth: 3 Ht: 2 Dpth: 2 30 50 70 10 40 80 20 60 A B E (^) F G H J C D
  1. 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 odd number (that is, where node.value%2 == 1; alternatively, node.value&1 == 1). Here is the prototype: int oddNodes ( TreeNode node ); int oddNodes ( TreeNode node ) { if ( node == null ) return 0; else if ( node.value%2 == 1 ) return 1 + oddNodes(node.left) + oddNodes(node.right); else return oddNodes(node.left) + oddNodes(node.right); } Alternatives int oddNodes ( TreeNode node ) { int count = 0; while (node != null) { count += node.value % 2; count += oddNodes(node.left); node = node.right; } return count; } int oddNodes ( TreeNode node ) { return node == null? 0 : oddNodes(node.left) + oddNodes(node.right) + (node.value & 1); }