Name: _____________________ Netid: _________________ Name: _____________________ Netid: _________________ Name: _____________________ Netid: _________________
recitation code submitted, use oct23-rec to submit code for student review.
TreeNode
definition below.
For this problem, the root is at level zero, the root's children are at level one, and for any node N its level is one more than N's parent's level. For example, for the bean-tree diagrammed below, the call levelCount(t,1) should return 2 (chickpea and navy are on level 1); the call levelCount(t,2) should return 3; and the call levelCount(t,4) should return 0.
Hint: when the level is 0, there is always just one node at that level, the root node (assuming t isn't empty), so return 1. If the level isn't zero, recursive calls will be used to determine the number of nodes at the requested level, and the level-requested should change in the recursive calls.
For example, in the tree shown below there are exactly four root-to-leaf paths. The sums of the paths are 27, 22, 26, and 18. Thus the value of hasPathSum(t,27) should be true and the value of hasPathSum(t,30) should be false (assuming t points to the root of the tree --- the node whose info field has value 5.)
Hint: You should make two recursive calls, you must decide on the value of target for each recursive call so that you can use the return values of the recursive calls to solve the problem. Note: in a tree with one node (a leaf) you can determine immediately if there's a path that sums to the target based on whether the target and the node are equal. So identification of a leaf-node will likely be a base case in your recursive method.
It can be shown that the diameter of a tree T is the largest of the following quantities:
Here's code that's almost a direct translation of the three properties
above and the method height
. The height of a tree
is the length of the longest root-to-node path in the
tree. For example, in the diagrams above the height of the
both trees is six.
height
? In writing
the recurrence let T(n) be the complexity of
calling height
with an n-node tree. Assume trees are
roughly balanced so that there are about n/2
nodes in each subtree. What is the recurrence if the
tree is completely unbalanced, e.g., all nodes in the
left sub-tree?
diameter
as written above does not run in
O(n) time.
Write a recurrence for this implementation and what the solution
to the recurrence is. Assume trees are roughly balanced in writing
the recurrence.
Fill in the missing code in diameterHelper
so that it works
as intended.
Write a method isIsomorphic that returns true if its two tree parameters are isomorphic and false otherwise. You must also give the big-Oh running time (in the average case, assuming each tree is roughly balanced) of your method with a justification. Express the running time in terms of the number of nodes in trees s and t combined, i.e., assume there are N nodes together in s and t with half the nodes in each tree. Write a recurrence to justify the runtime complexity.
Hint: a null trees is not isomorphic to a non-null tree, but two null trees are isomorphic. That's most-to-all of the base case you'll need. If the two trees have isomorphic left subtrees and isomorphic right subtrees, then they must be isomorphic.
Write a method isQuasiIsomorphic that returns true if two trees are quasi-isomorphic. You must also give the big-Oh running time (in the average case, assuming each tree is roughly balanced) of your method with a justification. Express the running time in terms of the number of nodes in trees s and t combined as with the previous problem. Write a recurrence and solve it! You'll need to do this because the recurrence is not one of the five-to-six recurrences we've seen before.
The difference in quasi-isomorphism compared to isomorphism is that if the left subtree of one tree is quasi-isomorphic to the right subtree of the other, the trees might be quasi-isomorphic. Of course if the left subtrees are quasi-isomorphic the trees could be quasi-isomorphic as well.