final-exam2-solution

final-exam2-solution - CSE 143 SUMMER 2010 FINAL EXAM...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CSE 143 SUMMER 2010 FINAL EXAM SOLUTION (PART 2) 1. (8 points) Draw the tree of strings that would give the following traversals: pre-order: the accomplishment Every with starts decision to try in-order: Every accomplishment starts with the decision to try the accomplishment decision Every with to starts try Write the post-order 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); } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

final-exam2-solution - CSE 143 SUMMER 2010 FINAL EXAM...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online