Download 5 Solved Questions on Computer Science ll - Exam | CIS 203 and more Exams Computer Science in PDF only on Docsity!
Name CIS 203 Midterm II: Chapters 69 , 11.
November 19, 2007
Directions: This is a closed book, closed notes examination. Place your answers in the space provided. The point
value of each question is indicated. There are a total of 54 points. Your total out of 54 will be weighted to 100
points. You have 55 minutes for this examination.
1. (12 pts ) We can use a stack to convert an infix expression into a postfix expression. Show the stack and the output
postfix expression after each operand, operator, and parenthesis is processed in the following infix expression.
2. (12 pts) Review the code in Appendix A for an array implementation of a queue.
a) (3) Write a new accessor method, last(), that returns the item last entered into the queue.
Throw an exception of the queue is empty.
b) (6) Is it possible to eliminate manyItems as a private data member? If so, show how to rewrite size(). If
it is not possible to eliminate manyItems, clearly illustrate why not.
c) (3) Consider the following client code:
ArrayQueue a = new ArrayQueue();
a.add(“dog”);
a.add(“cat”);
a.add(“horse”);
a.remove();
a.add(“fish”);
a.remove();
a.add(“goat”);
a.remove();
a.add(“zebra”);
Show the state of all the private data members of the ArrayQueue after the above code is executed.
4. (9 pts) Consider the following tree.
a) (3 ) Show the results of a postorder traversal of the tree.
b) (3 ) To remove the value 45 from the tree, which values, if any are copied to where?
b) (3 )To remove the value 45 from the tree, which node is actually removed? Please circle in the above
diagram.
45 (^25 ) 50 53 30 27
5. (9 pts) Assume we have the implementation of the BTNode class in Appendix B. Assume the add() method is
implemented so that data is stored in a way that the resulting tree forms a binary search tree.
Complete the method countNodes(). When called on a node, the node returns the number of nodes in
its subtree including itself. For example, when the following client code is run, the output should
be 7. Write you implementation of countNodes() on this page.
public static void main (String [] args) { BTNode r = new BTNode("moo", null, null); r.add("cat"); r.add("cow"); r.add("fish"); r.add("raven"); r.add("hamster"); r.add("marmet"); System.out.println(r.countNodes()); }
public E remove( ) { E answer; if (isEmpty()) throw new NoSuchElementException("Queue underflow"); answer = data[front]; front = nextIndex(front); manyItems; return answer; } public int size( ) { return manyItems; } private int nextIndex(int i) { if (++i == data.length) return 0; else return i; } private void ensureCapacity(int minimumCapacity) { if (data.length >= minimumCapacity) return; E[] biggerArray = (E[]) new Object[minimumCapacity]; if (isEmpty()) else if (front <= rear) { System.arraycopy(data, front, biggerArray, front, manyItems); } else { int n1 = data.length front; int n2 = rear + 1; System.arraycopy(data, front, biggerArray, 0, n1); System.arraycopy(data, 0, biggerArray, n1, n2); front = 0; rear = manyItems1; } data = biggerArray; } }
Appendix B
public class BTNode<E extends Comparable> { private E data; private BTNode left; private BTNode right; public BTNode (E data, BTNode left, BTNode right) { this.data = data; this.left = left; this.right = right; } public int countNodes() { // you implement for this question } public void add(E data) { // assume the code is here and works correctly } }