








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 2009;
Typology: Exams
1 / 14
This page cannot be seen from the preview
Don't miss anything!









7p-9p, Tuesday, November 3
Suppose that a client performs an intermixed sequence of stack push and pop operations. The push operations push the integers 0 through 9 in order on to the stack; the pop operations print out the return value. Which of the following sequences could not occur?
(a) 4 3 2 1 0 9 8 7 6 5 (b) 2 1 4 3 6 5 8 7 9 0 (c) 0 4 6 5 3 8 1 7 2 9 (d) 4 6 8 7 5 3 2 9 1 0 (e) All of these sequences are possible.
A queue cannot be implemented using only for holding data.
(a) a stack (b) a linked list (c) an array (d) More than one of a), b), c) can be used to fill in the blank. (e) None of a), b), c) can be used to fill in the blank.
Suppose we have implemented the Stack ADT as a singly-linked-list with head and tail pointers and no sentinels. Which of the following best describe the running times for the functions push and pop, assuming there are O(n) items in the list, and that the bottom of the stack is at the head of the list (all pushing and popping occurs at the tail)?
(a) O(1) for both functions. (b) O(n) for both functions. (c) O(1) for push and O(n) for pop. (d) O(n) for push and O(1) for pop. (e) None of these is the correct choice.
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 list consists of the integers 1, 2, 3, 2, 1 in that order, with the first item on the left, then the output is “yes”. (d) If the list consists of the integers 1, 2, 3, 2, 1, 4 in that order, with the first item on the left, then the output is “yes”. (e) None of these options describes the behavior of this code.
Which of the following statements is true for a B-tree of order m containing n items?
(i) The height of the B-tree is O(logm n) and this bounds the total number of disk seeks. (ii) A node contains a maximum of m − 1 keys, and this bounds the number of disk seeks at each level of the tree. (iii) Every Binary Search Tree is also an order 2 B-Tree.
Make one of the following choices.
(a) Only item (i) is true. (b) Only item (ii) is true. (c) Only item (iii) is true. (d) Two of the above choices are true. (e) None of choices (i), (ii), or (iii) are true.
To justify the use of arrays for data structures of unbounded size (stacks, queues, lists, etc.), we proved that the following strategy results in O(n) running time over a sequence of n inserts for an average of O(1) per insertion.
(a) When an array of size n fills, create a new array of size n, and maintain all the arrays. Do not copy any data. (b) When an array of size n fills, create a new array of size 2n, and maintain all the arrays. Do not copy any data. (c) When an array of size n fills, create a new array of size n+d for some large fixed constant d, and copy the data into the new array. (d) When an array of size n fills, create a new array of size 2n and copy the data into the new array. (e) None of these strategies give the performance we describe.
Objects of type iterator promise to implement each of the following except...
(a) operator+ (b) operator* (c) operator== (d) operator= (e) All of these are implemented in an iterator.
In an array-based implementation of a stack we achieve efficient push and pop operations by .
(i) placing the top of the stack at the start of the array. (ii) placing the bottom of the stack at the start of the array.
Make one of the following choices.
(a) Only item (i) can be used to fill in the blank. (b) Only item (ii) can be used to fill in the blank. (c) Either item (i) or item (ii) can be used to fill in the blank. (d) Neither item (i) or item (ii) can be used to fill in the blank.
For this question, consider the following partial class definition for the Quadtree class, which uses a quadtree to represent a square bitmap image as in MP5.
class Quadtree { public: // constructors and destructor; all of the public methods from MP5, including:
void buildTree(BMP const & source, int resolution); 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: class 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* root; // pointer to root of quadtree, NULL if tree is empty. };
You may assume that the quadtree is complete and that it has been built from an image that has size 2k^ × 2 k. As in MP5, the element field of each leaf of the quadtree stores the color of a square block of the underlying bitmap image; for this question, you may assume, if you like, that each non-leaf node contains the component-wise average of the colors of its children. You may not use any methods or member data of the Quadtree or QuadtreeNode classes which are not explicitly listed in the partial class declaration above. You may assume that each child pointer in each leaf of the Quadtree is NULL.
(a) (3 points) Write a public member function void Quadtree::flipVert(), which reflects an image across the horizontal axis running across the center of the image. For example, if the image is a portrait of a person standing upright, the result will be the image of the person upside down. Your function should call ONE private helper function that you will be writing in the next part of the problem. Your implementation must work correctly for Quadtrees which have been pruned as described in MP5. Write the function as it would appear in the quadtree.cpp file for the Quadtree class.
void Quadtree::flipVert(){ // Your code goes here
(b) (7 points) Write the private helper method you invoked in the previous part. For this one, you may choose the return value and the number and types of parameters. Note that our skeleton below should have sufficiently many lines for your solution, but you are welcome to add more if you need to do so. Please try to comment your code inline and to the right of the skeleton.
_________ ____________::____________(____________________) {
}
(a) (9 points) Scrutinize the following code and figure out what the function mystery does when called on a node in a binary search tree. You may assume that both croot and croot->left are non-NULL, and that the keys in the BST are unique.
treeNode * & BST
treeNode * & BST
Circle every accurate statement in the list below.
(b) (3 points) Which binary search tree public member function employs this code?
(c) (2 points) Briefly explain the context in which the code is used.
This tree will become unbalanced by the removal of some of the nodes. Fill in the table below, telling, for each node, whether its removal will unbalance the tree, at what node the first imbalance occurs, what kind of rotation would fix that imbalance, and whether or not a repair of the first imbalance invokes additional imbalance(s). Note that each node is removed from the original tree. (If removal of a node does not unbalance the tree, just leave its entry blank.)
*"
'" +" ''"
Additional Node to Unbalanced Imbalance? Remove Node Rotation to Fix (Yes or No) 1 2 3 4 5 6 7 8 9
(a) (3 points) What is the maximum number of keys we can store in a B-Tree of order 128 that has height 3?
(b) (3 points) What is the minimum number of keys we can store in a B-Tree of order 128 that has height 3?
(c) (4 points) In class we proved that the search time for finding a key in a B-Tree is O(m logm n). In this problem, we’d like you to explain each of the factors m, and logm n in that result: i. Tell as much as you can about the factor m:
ii. Tell as much as you can about the factor logm n:
scratch paper