CSE143 Key to Sample Final (Part 2) handout #34 1. Binary Trees, 10 points. One possible solution appears below. public int purebredOdds() { return purebredOdds(overallRoot); } private int purebredOdds(IntTreeNode root) { if (root == null || root.data % 2 == 0) return 0; else return 1 + purebredOdds(root.left) + purebredOdds(root.right); } 2. Comparable class, 20 points. One possible solution appears below. public class FoodData implements Comparable<FoodData> { private String name; private double fat; private double carbs; private double protein; public FoodData(String name, double fat, double carbs, double protein) { if (fat < 0 || carbs < 0 || protein < 0) throw new IllegalArgumentException(); this.name = name; this.fat = fat; this.carbs = carbs; this.protein = protein; } public String getName() { return name; } public double getCalories() { return 9 * fat + 4 * (carbs + protein); } public double percentFat() { if (getCalories() == 0) return 0.0; else return 100.0 * (9 * fat / getCalories()); } public String toString() { return name + ": " + fat + "g fat, " + carbs + "g carbohydrates, " + protein + "g protein"; } public int compareTo(FoodData other) { double difference = percentFat() - other.percentFat(); if (difference < 0) return -1; else if (difference > 0) return 1; else return name.compareTo(other.name); } } 3. Binary Trees, 20 points. One possible solution appears below. public void mirror() { mirror(overallRoot); } private void mirror(IntTreeNode root) { if (root != null) { IntTreeNode temp = root.left; root.left = root.right; root.right = temp; mirror(root.left); mirror(root.right); } }