Question 1 - Binary Tree Traversals¶
Preorder traversal 0, 4, 7, 9, 2, 6, 1, 8, 5, 3
Inorder traversal 4, 0, 2, 9, 7, 8, 1, 6, 5, 3
Postorder traversal 4, 2, 9, 8, 1, 3, 5, 6, 7, 0
Question 2 - Binary Search Trees¶
+------------+
| Moira |
+------------+
/ \
/ \
/ \
+------------+ +------------+
| Alexis | | Stevie |
+------------+ +------------+
\ / \
\ / \
\ / \
+------------+ +------------+ +------------+
| Johnny | | Roland | | Ted |
+------------+ +------------+ +------------+
/
/
/
+------------+
| David |
+------------+
Question 3 - Inheritance/Polymorphism¶
Statement Output
------------------------------------------------------------
var1.method1(); compiler error
var2.method1(); Poptart 1
var1.method2(); Hotdog 2/Sandwich 2/Hotdog 3
var2.method2(); Poptart 2
var3.method2(); Hotdog 2/Sandwich 2/SeattleDog 3
var4.method2(); compiler error
var5.method2(); Hotdog 2/Sandwich 2/SeattleDog 3
var1.method3(); Hotdog 3
var2.method3(); Sandwich 3
var3.method3(); SeattleDog 3
var4.method3(); compiler error
var5.method3(); SeattleDog 3
((Poptart)var3).method1(); runtime error
((SeattleDog)var5).method1(); SeattleDog 1
((Hotdog)var3).method1(); compiler error
((Hotdog)var3).method3(); SeattleDog 3
((SeattleDog)var6).method3(); runtime error
((Sandwich)var4).method2(); Sandwich 2
((Hotdog)var4).method3(); runtime error
((Poptart)var6).method3(); Sandwich 3
Question 4 - Comparable¶
One possible solution appears below.
public class BookData implements Comparable<BookData> {
private String title;
private String author;
private int reviews;
private double total;
public BookData(String title, String author) {
this.title = title;
this.author = author;
this.reviews = 0;
this.total = 0.0;
}
public void review(double rating) {
reviews++;
total += rating;
}
public String getTitle() {
return title;
}
public double getRating() {
if (reviews == 0) {
return 0.0;
} else {
return total / reviews;
}
}
public String toString() {
double rating = (int) (10.0 * getRating()) / 10.0;
String result = title + ", by " + author + ", " + rating + " (";
if (reviews == 1) {
result += "1 review)";
} else {
result += reviews + " reviews)";
}
return result;
}
public int compareTo(BookData other) {
double delta = other.getRating() - getRating();
if (delta < 0) {
return -1;
} else if (delta > 0) {
return 1;
} else { // delta == 0
return other.reviews - reviews;
}
}
}
Question 5 - Collections¶
One possible solution appears below.
public static Map<Point, List<Integer>> pointMap(List<Point> data) {
Map<Point, List<Integer>> map = new HashMap<>();
for (int i = 0; i < data.size(); i++) {
Point p = data.get(i);
if (!map.containsKey(p)) {
map.put(p, new LinkedList<>());
}
map.get(p).add(i);
}
return map;
}
Question 6 - Binary Trees I¶
One possible solution appears below.
public void printLevel(int target) {
if (target < 1) {
throw new IllegalArgumentException();
}
System.out.print("nodes at level " + target + " =");
printLevel(overallRoot, target, 1);
System.out.println();
}
private void printLevel(IntTreeNode root, int target, int level) {
if (root != null)
if (level == target)
System.out.print(" " + root.data);
else {
printLevel(root.left, target, level + 1);
printLevel(root.right, target, level + 1);
}
}
}
Question 7 - Binary Trees II¶
One possible solution appears below.
public void add(IntTree other) {
overallRoot = add(overallRoot, other.overallRoot);
}
private IntTreeNode add(IntTreeNode root1, IntTreeNode root2) {
if (root2 != null) {
if (root1 == null) {
root1 = new IntTreeNode(0);
}
root1.data += root2.data;
root1.left = add(root1.left, root2.left);
root1.right = add(root1.right, root2.right);
}
return root1;
}
Question 8 - LinkedList¶
One possible solution appears below.
public void switchPairsOfPairs() {
if (front != null && front.next != null &&
front.next.next != null && front.next.next.next != null) {
ListNode current = front.next.next;
front.next.next = current.next.next;
current.next.next = front;
front = current;
current = current.next.next.next;
while (current.next != null && current.next.next != null &&
current.next.next.next != null &&
current.next.next.next.next != null) {
ListNode temp = current.next.next.next;
current.next.next.next = temp.next.next;
temp.next.next = current.next;
current.next = temp;
current = temp.next.next.next;
}
}
}