This preview shows pages 1–3. Sign up to view the full content.
1
CSE 143
SUMMER 2010
FINAL EXAM SOLUTION (PART 2)
1.
(8 points) Draw the tree of strings that would give the following traversals:
preorder:
the accomplishment Every with starts decision to try
inorder:
Every accomplishment starts with the decision to try
the
accomplishment
decision
Every
with
to
starts
try
Write the postorder traversal of the tree:
Every starts with accomplishment try to decision the
2.
(13 points) Write a method for the
IntTree
class called
isBalanced
that returns
true
is the tree is balanced and
false
otherwise.
A tree is balanced if the heights of
its left and right subtrees differ by at most 1 AND the individual subtrees are themselves
balanced.
You may assume that there is a method called
height
that takes an
IntTreeNode
as a parameter and returns the height of the tree rooted at that node.
[ DESCRIPTION ABOUT
Math.abs
CUT ]
public
boolean
isBalanced() {
return
isBalanced(overallRoot);
}
private
boolean
isBalanced(IntTreeNode root) {
if
(root ==
null
) {
return
true
;
}
else
{
int
heightLeft = height(root.left);
int
heightRight = height(root.right);
if
(Math.abs(heightLeft  heightRight) > 1) {
return
false
;
}
return
isBalanced(root.left) &&
isBalanced(root.right);
}
}
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document2
3.
(13 points) Write a method for the
IntTree
class called
fill()
that replaces empty
children subtrees of a node with new nodes provided that the new nodes do not increase
This is the end of the preview. Sign up
to
access the rest of the document.
 Winter '09

Click to edit the document details