Download Epoka University CEN Department CS254 Data structures (Spring

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Epoka University CEN Department
CS254 Data structures (Spring 2013)
Midterm Exam
April 23, 2013
This is a closed book exam. Only a single hand-written crib sheet can be
used.
Make answers short and to the point. Any ambiguous statement will be
ignored even if both meanings are correct for different reasons.
Even if we ask for just a yes/no answer, you must always give a proof
(irrefutably convincing argument). You can refer to theorems, definitions
and formulae proven/mentioned in lectures and problem sessions, provided
that you can state them correctly.
You don’t need to perform all computations. Leaving things like 7! · 28 is
fine.
Please explain any syntactic sugar that you might use.
Use the back of each page as scratch paper. If you need more, please ask
me for.
Please print if your handwriting is hard to read.
Good luck!
NAME:
Problem
Score
1 2 3 4 5 Ex. cr. 3
i
Ex.cr.5
Σ
Problem 1 Recall the Stack interface given below.
public interface Stack <E>{
public int size();
public boolean isEmpty();
public E top() throws EmptyStackException;
public void push(E element);
public E pop() throws EmptyStackException;
}
1. (5 pts) Describe (in English) how to implement pre-order traversal of
a BST using a stack?
Answer:
Proposed solution
The algorithm is as follows.
(a) Create an empty stack S.
(b) Initialize current node curr as root
(c) Push the current node to S and set curr to the left child of curr until curr
==NULL
(d) If curr ==NULL and stack is not empty then
a) Pop the top item from stack.
b) Print the popped item, set curr current = to the right child of curr c) Go
to step 3.
(e) If curr ==NULL and stack is empty then we are done.
End of the Proposed solution
2. (15 pts) Now do the same using Java, assuming that you are given a
tree class as in Lab05 which is sketched below.
ii
public class BinaryTree {
private Node root;
private static class Node {
Node left;
Node right;
int data;
//the constructor skipped
}
public boolean lookup(int data) {
return(lookup(root, data));
}
private boolean lookup(Node node, int data) {
//the implementation skipped
}
public void insert(int data) {
//the implementation skipped
}
private Node insert(Node node, int data) {
//the implementation skipped
}
public int size() {
return(size(root));
}
}
Answer:
iii
Problem 2 (20 pts) Suppose we add a new operation to the stack ADT
called findMinimum that returns a reference to the smallest element in the
stack.
Is it possible to provide an implementation for this operation that has a
worst case running time of O(1)?
Answer:
Proposed solution
Yes. Add a new field m that is a stack whose top is the minimum of the other one.
Modify the push procedure such that when an element is added, if it is smaller than the
value of the top of m, then we add a reference to this object on m. When popping, we
need to check if the element is referred by the top of m. If yes, we pop it.
End of the Proposed solution
Problem 3 (20 pts) Suppose you are asked to use two stacks, YinStack and
YangStack, as your only instance variables to implement the Queue abstract
data type.
Describe in English or pseudo-code how you would implement the methods
enqueue() and dequeue().
Extra credit (5 pts): Analyze the complexity in terms of n, the number of
elements in the queue.
Answer:
Proposed solution
To enqueue an element, we simply add it to the YinStack. To dequeue an element,
until YinStack gets empty, pop each element from it and push it onto YangStack. The
element that needs to be returned not is on the top of the YangStack so we pop it. Then,
until YangStack is empty, pop element from it and push it into YinStack.
End of the Proposed solution
iv
(Continuation of the Answer to Problem 3)
Proposed solution
Enqueue takes only O(1), while dequeue takes O(n).
End of the Proposed solution
Problem 4
1. (10 pts) Describe how to implement the stack ADT using
two queues.
Answer:
Proposed solution
Let Q1 and Q2 be the two queues that we have. To push an element onto the stack,
we just enqueue it in Q1 . To pop an element from the stack, then we need to dequeue
element by element from Q1 and enqueue it in Q2 until the last one which is not
enqueued to Q2 but returned. Then, we dequeue element by element from Q2 and
enqueu them in Q1 .
End of the Proposed solution
2. (10 pts) What is the running time of the push() and pop() methods
in this case?
Proposed solution
push() takes only O(1), while pop() takes O(n).
End of the Proposed solution
v
Problem 5 Recall the definition of a bitonic array of integers from the first
homework. Besides the storage, it has the size and a reference to the peak of
the array.
1. (20 pts) Modify this datastructure such that you can implement a priority queue out of it.
Answer:
Proposed solution
You can just use it without modification such that you for the extraction of the
smallest, you compare the first and the last and remove the smallest. If you want
to modify, then you can make it “inverse bitonic” that is, now there is no peak but
there is dip instead — the smallest element of the structure.
End of the Proposed solution
2. (Extra credit (5 pts)) Suppose that you are implementing this datastructure. You have two choices: use arrays or doubly linked lists. Analyze pros and cons of each.
Solution:
Proposed solution
See the textbook.
End of the Proposed solution
End of the Midterm Exam
vi