Docsity

Solution to Quiz #3 for CMSC132 Summer 2008 - Binary Search Tree, Quizzes of Computer Science

Toggle sidebar
bg1
CMSC132 Summer 2008 Quiz #3 Solution, Duration 25 Minutes
Total 30 pts
Implement the methods below based on the following Java class definitions. You may not add any instance
variables or static variables to the BinarySearchTree class. Non-recursive solutions will receive zero
credit.
public class BinarySearchTree <E extends Comparable<E>> {
private class Node {
private E data;
private Node left, right;
public Node(E data) { this.data = data; }
}
private Node root;
public int getInteriorNodesCnt() { //YOU MUST IMPLEMENT }
public ArrayList<E> getDecreasingOrderList(){ //YOU MUST IMPLEMENT }
}
1. Implement the method getInteriorNodesCnt() that returns the number of interior nodes in the tree. A
tree with one element has no interior nodes.
2. Implement the method getDecreasingOrderList() that returns an ArrayList with the data elements of
the tree inserted into the list based on decreasing key order. For example, if the tree stores the keys 5,
10, and 3, then the first ArrayList object entry will have the data element associated with 10, the second
entry the data element associated with 5, and the last entry will have the data element corresponding to
3.
Problem 1 (12 pts)
public int getInteriorNodesCnt() {
return getInteriorNodesCntAux(root);
}
private int getInteriorNodesCntAux(Node rootAux) {
if (rootAux == null)
return 0;
else if (rootAux.left == null && rootAux.right == null)
return 0;
else
return 1 + getInteriorNodesCntAux(rootAux.left) +
getInteriorNodesCntAux(rootAux.right);
}
Problem 2 (18 pts)
public ArrayList<E> getDecreasingOrderList() {
ArrayList<E> L = new ArrayList<E>();
getDecreasingOrderListAux(root, L);
return L;
}
private void getDecreasingOrderListAux(Node ra, ArrayList<E> L) {
if (ra != null) {
getDecreasingOrderListAux(ra.right, L);
L.add(ra.data);
getDecreasingOrderListAux(ra.left, L);
}
}

Partial preview of the text

Download Solution to Quiz #3 for CMSC132 Summer 2008 - Binary Search Tree and more Quizzes Computer Science in PDF only on Docsity!

CMSC132 Summer 2008 Quiz #3 Solution, Duration 25 Minutes

Total 30 pts

Implement the methods below based on the following Java class definitions. You may not add any instance variables or static variables to the BinarySearchTree class. Non-recursive solutions will receive zero credit.

public class BinarySearchTree <E extends Comparable> { private class Node { private E data; private Node left, right; public Node(E data) { this.data = data; } } private Node root; public int getInteriorNodesCnt() { //YOU MUST IMPLEMENT } public ArrayList getDecreasingOrderList(){ //YOU MUST IMPLEMENT } }

  1. Implement the method getInteriorNodesCnt() that returns the number of interior nodes in the tree. A tree with one element has no interior nodes.
  2. Implement the method getDecreasingOrderList() that returns an ArrayList with the data elements of the tree inserted into the list based on decreasing key order. For example, if the tree stores the keys 5, 10, and 3, then the first ArrayList object entry will have the data element associated with 10, the second entry the data element associated with 5, and the last entry will have the data element corresponding to

Problem 1 (12 pts)

public int getInteriorNodesCnt() { return getInteriorNodesCntAux(root); }

private int getInteriorNodesCntAux(Node rootAux) { if (rootAux == null) return 0; else if (rootAux.left == null && rootAux.right == null) return 0; else return 1 + getInteriorNodesCntAux(rootAux.left) + getInteriorNodesCntAux(rootAux.right); }

Problem 2 (18 pts)

public ArrayList getDecreasingOrderList() { ArrayList L = new ArrayList(); getDecreasingOrderListAux(root, L); return L; }

private void getDecreasingOrderListAux(Node ra, ArrayList L) { if (ra != null) { getDecreasingOrderListAux(ra.right, L); L.add(ra.data); getDecreasingOrderListAux(ra.left, L); } }