Professional Documents
Culture Documents
1) Vocabulary Answer the following question based on the above tree (Fig 8.3)
a. Which node is the root?
Sol. /user/rt/courses/
Sol. grades, hw1, hw2, hw3, pr1, pr3, pr3, buylow, sellhigh, market, grades
Sol. 9
Sol. 1
Sol. 2
Sol. 4
2) Finish the method sumOfLeaves, which calculates the sum of the leaf nodes' data fields. It
should not include the internal nodes in the sum. Use recursion.
/* Class containing left and right child of current
node and key value*/
class Node {
int data;
Node left, right;
Sol
// populating stack
st.push("Java");
st.push("Source");
st.push("code");
/*
To remind you how Stack works, here is the output of the above program:
*/
int data;
Node left, right;
class BinaryTree {
Node root;
// non-recursive method for inorder traversal
void inorder() {
if (root == null) {
return;
}
// use stack to keep track of the Nodes you still need to visit.
// Keep in mind that the Stack starts empty
Stack<Node> stack = new Stack<Node>();
Node node = root;
//Your code here
while (node != null) {
stack.push(node);
node = node.left;
}
// traverse the tree
while (stack.size() > 0) {
// visit the top node
node = stack.pop();
System.out.print(node.data + " ");
if (node.right != null) {
node = node.right;
// the next node to be visited is the leftmost
while (node != null) {
stack.push(node);
node = node.left;
}
}
}
}
Reflection: why did we need a data structure (like Stack) to write the non-recursive traversal but we
didn't need one for the recursive method sumOfLeaves?
5) Two ordered trees T’ and T’’ are said to be "isomorphic" if one of the following holds:
a. Both T’ and T’’ are empty
b. Both T’ and T’’ consist of a single node
c. The roots of T’ and T’’ have the same number of k >= 1 of subtrees, and the ith such
subtree of T’ is isomorphic to the ith such subtree of T’’ for i = 1,…..,k
Design an algorithm that tests whether two given ordered trees are isomorphic. What is the
running time of your algorithm?
class BinaryTree
{
Node root1, root2;
/* Given a binary tree, print its nodes in reverse level order */
boolean isIsomorphic(Node n1, Node n2)
{
// Both roots are NULL, trees isomorphic by definition
if (n1 == null && n2 == null)
return true;
// Exactly one of the n1 and n2 is NULL, trees not isomorphic
if (n1 == null || n2 == null)
return false;
if (n1.data != n2.data)
return false;
// There are two possible cases for n1 and n2 to be isomorphic
// Case 1: The subtrees rooted at these nodes have NOT been
// "Flipped".
// Both of these subtrees have to be isomorphic.
// Case 2: The subtrees rooted at these nodes have been "Flipped"
return (isIsomorphic(n1.left, n2.left) &&
isIsomorphic(n1.right, n2.right))
|| (isIsomorphic(n1.left, n2.right) &&
isIsomorphic(n1.right, n2.left));
}
Time complexity is O(m + n) where m and n are number of nodes in given trees
6) Give an O(n) time algorithm for printing the depths of all nodes of a tree T, where n is the
number of nodes of T.
Sol.
This can be done using a preorder traversal. When doing a "visit" in the traversal, simply store the
depth of the node’s parent incremented by 1. Now, every node will contain its depth.
Assuming depth is a variable associated with each node, we can compute node’s depth
computingDepths(v, d)
{
v.depth=d;
if ( v has no child )
return;
else
for each child v′ do
computingDepths(v′, d+1);
return;
}
To compute the depth of each node, simply call computeDepth(root, 0). This algorithm visits
each node only once and each visit takes constant time. Therefore, it’s running time is O(n)