









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Professor: Earls; Class: Data Structures; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Fall 2010;
Typology: Exams
1 / 15
This page cannot be seen from the preview
Don't miss anything!










7p-9p, Tuesday, November 2
Which of the following is not a reasonable choice for implementing the Dictionary Abstract Data Type?
(a) AVL Tree (b) B-Tree (c) Binary Search Tree (d) Stack (e) All of these are reasonable choices for implementing a Dictionary.
Suppose we do a level order traversal of the following binary tree. What is the maximum number of non-null nodes on the queue at one time in the execution of the algorithm?
(a) 4 (b) 5 (c) 6 (d) 12 (e) None of these is the correct number.
Consider the following partial C++ code:
#include
template <class Iter, class Formatter> void mystery(Iter front, Iter end, Formatter doSomething) {
while (front != end) { doSomething(*front, *end); front++; end--;} }
template
int main() { vector
vector
dunno
for (int i = 0; i < v.size(); i++) cout << v[i] << endl; return 0; }
If you assume that all iterators are bi-directional and non-constant, which of the following statements is true?
(a) This code does not compile because of a type mismatch in the mystery parameter list. (b) This code does not compile because of a syntax error in the template instantiation for mystery. (c) If the vector consists of the integers 1, 2, 3, 4, 5, 6, 7 in that order, with the first item on the left, then the output is 1, 2, 3, 4, 5, 6, 7. (i.e. the code does not change the vector.) (d) If the vector consists of the integers 1, 2, 3, 4, 5, 6, 7 in that order, with the first item on the left, then the output is 8, 8, 8, 4, 5, 6, 7. (e) None of these options describes the behavior of this code.
What is the real name of mysteryFunction? Note that in context, t->right will not be null.
void mysteryFunction(treeNode * & t) {
treeNode * y = t->right; t->right = y->left; y->left = t; y->height = max( height(y->right), height(y->left)) + 1; t->height = max( height(t->right), height(t->left)) + 1; t = y;
}
(a) leftRotate (b) rightRotate (c) rightLeftRotate (d) leftRightRotate (e) None of these choices are correct.
Consider the Binary Search Tree built by inserting the following sequence of integers, one at a time: 5, 4 , 7 , 9 , 8 , 6 , 2 , 3 , 1. If the node containing 5 were removed from the tree, what would be the right child of the node containing 2?
(a) Null (b) The node containing 3. (c) The node containing 4. (d) The node containing 6. (e) None of these answers is correct.
(a) O(1) (b) O(log n) (c) O(n) (d) O(n log n) (e) None of these running times is appropriate.
(MC 9) Enqueue for a Queue implemented with a Singly Linked List with a tail pointer, where the front of the queue is at the end (tail) of the list. (Enqueue occurs at the head.)
(MC 10) Dequeue for a Queue implemented with a Singly Linked List with a tail pointer, where the front of the queue is at the end (tail) of the list. (Enqueue occurs at the head.)
(MC 11) Worst case for finding a given key in a Binary Search Tree.
(MC 12) Worst case for inserting a single key into an AVL Tree.
(MC 13) Worst case for an algorithm to find the smallest key that is within fixed distance d from a given key in a Binary Search Tree (if such a key exists).
(MC 14) Worst case for an algorithm to find the smallest key that is within fixed distance d from a given key in an AVL Tree (if such a key exists).
(MC 15) Given a Binary Tree, check to see if it is a Binary Search Tree.
(MC 16) Remove all the nodes from an AVL Tree (as is done by the destructor).
(MC 17) Compute the height of every subtree in a Binary Search Tree.
(MC 18) Given a Binary Search Tree whose subtree heights have been computed determine if the Binary Search Tree is an AVL Tree.
For this question, consider the following partial class definition for the Quadtree class, which uses a quadtree to represent a d-by-d square bitmap image as in MP5. As a simplifying assumption for this problem, you may assume that only leaf nodes contain valid RGBApixel elements, and that the element field in all non-leaf nodes is not initialized to any particular value (we will not be doing any pruning of this tree, though it may already have been pruned). Just as in MP5, you may assume that d is a power of 2.
class Quadtree { public: // constructors and destructor; all of the public methods from MP5, including:
void buildTree(BMP const & source, int newResolution); RGBApixel getPixel(int x, int y) const; BMP decompress() const; void clockwiseRotate(); // 90 degree turn to the right void prune(int tolerance); int pruneSize(int tolerance) const; int idealPrune(int numLeaves) const;
private: struct QuadtreeNode { QuadtreeNode* nwChild; // pointer to northwest child QuadtreeNode* neChild; // pointer to northeast child QuadtreeNode* swChild; // pointer to southwest child QuadtreeNode* seChild; // pointer to southeast child
RGBApixel element; // the pixel stored as this node’s "data" QuadtreeNode(RGBApixel const & elem); QuadtreeNode(); };
QuadtreeNode* root; // ptr to root of quadtree, NULL if tree is empty. int resolution; // number of pixels on a side of the image (assume 2^k)
void copy(QuadtreeNode *& firstNode, QuadtreeNode * secondNode); void clear(QuadtreeNode *& curNode); // plus many helper functions of your own design
};
You may assume that each child pointer in a new QuadtreeNode is NULL, and that the Quadtree has been accurately constructed so that the resolution is set.
(b) (7 points) Write the private helper function that actually does the work for the getPixel function in part (a). (Note that if your approach to this problem is markedly different than ours, just write your solution on the back of the previous page. Your entire solution to getPixel will then be graded out of ten points.)
_________ ____________::_________________
{
}
(c) (5 points) Analyze and give a tight asymptotic bound on the running time of your implementation for part (b). Your bound should be stated in terms of d, the number of pixels in the width (or height) of the original image represented by the Quadtree. Briefly justify your answer.
(d) (2 points) Suppose a pruned Quadtree has height h ≥ 0. What is the least number of nodes it contains in terms of h? (Briefly explain or draw a helpful sketch to assure partial credit, and be exact.)
(e) (2 points) Suppose a Quadtree has n nodes. What is its greatest possible height in terms of n? (Please be exact.)
(f) In this part of the problem we will derive an expression for the maximum number of nodes in a Quadtree of height h, and prove that our solution is correct. Let N (h) denote the maximum number of nodes in a Quadtree of height h. i. (3 points) Give a recurrence for N (h). (Don’t forget appropriate base case(s).)
We solved the recurrence and found a closed form solution for N (h) to be:
N (h) = 4 h+1^ − 1 3
, h ≥ − 1
ii. (8 points) Prove that our solution to your recurrence from part (a) is correct by induction: Consider a maximally sized Quadtree of arbitrary height h.
we have N ( ) = nodes.
so that N (h) = = , which was what we wanted to prove.
Here are the Stack and Queue class definitions you used in MP4:
template
template
Write the recursive function verifySame whose function prototype is below. The function should return true if the parameter stack and queue contain only elements of exactly the same values in exactly the same order, and false otherwise (see example below). You may assume the stack and queue contain the same number of items!
We’re going to constrain your solution so as to make you think hard about solving it elegantly:
Example: This stack and queue are considered to be the same. Note that we match the bottom of the stack with the front of the queue. No other queue matches this stack.
template< class T> bool verifySame(Stack
}