Exam 2 with Solution for Data Structures - Spring 2012 | CS 225, Exams of Data Structures and Algorithms

Material Type: Exam; Professor: Heeren; Class: Data Structures; Subject: Computer Science; University: University of Illinois - Urbana-Champaign;

Typology: Exams

2011/2012

Uploaded on 04/16/2012

ryan9986
ryan9986 🇺🇸

2 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
University of Illinois at Urbana-Champaign
Department of Computer Science
Second Examination
CS 225 Data Structures and Software Principles
Spring 2012
7p-9p, Tuesday, April 3
Name:
NetID:
Lab Section (Day/Time):
This is a closed book and closed notes exam. No electronic aids are allowed, either.
You should have 6 problems total on 15 pages. The last sheet is scratch paper; you may
detach it while taking the exam, but must turn it in with the exam when you leave. Use
scantron forms for Problems 1 and 2.
Unless otherwise stated in a problem, assume the best possible design of a particular imple-
mentation is being used.
Unless the problem specifically says otherwise, assume the code compiles, and thus any com-
piler error is an exam typo (though hopefully there are not any typos).
We will be grading your code by first reading your comments to see if your plan is good,
and then reading the code to make sure it does exactly what the comments promise. In
general, complete and accurate comments will be worth approximately 30% of the points on
any coding problem.
Please put your name at the top of each page.
Problem Points Score Grader
125 scantron
225 scantron
320
410
510
610
Total 100
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Exam 2 with Solution for Data Structures - Spring 2012 | CS 225 and more Exams Data Structures and Algorithms in PDF only on Docsity!

University of Illinois at Urbana-Champaign

Department of Computer Science

Second Examination

CS 225 Data Structures and Software Principles

Spring 2012

7p-9p, Tuesday, April 3

Name:

NetID:

Lab Section (Day/Time):

  • This is a closed book and closed notes exam. No electronic aids are allowed, either.
  • You should have 6 problems total on 15 pages. The last sheet is scratch paper; you may detach it while taking the exam, but must turn it in with the exam when you leave. Use scantron forms for Problems 1 and 2.
  • Unless otherwise stated in a problem, assume the best possible design of a particular imple- mentation is being used.
  • Unless the problem specifically says otherwise, assume the code compiles, and thus any com- piler error is an exam typo (though hopefully there are not any typos).
  • We will be grading your code by first reading your comments to see if your plan is good, and then reading the code to make sure it does exactly what the comments promise. In general, complete and accurate comments will be worth approximately 30% of the points on any coding problem.
  • Please put your name at the top of each page.

Problem Points Score Grader

1 25 scantron

2 25 scantron

Total 100

  1. [Miscellaneous – 25 points].

MC1 (2.5pts)

Suppose a binary tree holds 127 keys. Then our node-based implementation of that tree has how many NULL pointers?

(a) 64 (b) 128 (c) 256 (d) The answer cannot be determined from the information given. (e) None of these is the correct response.

MC2 (2.5pts)

Which of the following sequences of keys cannot be the inOrder traversal of an AVL tree?

(a) 1 3 6 8 12 15 17 19 (b) 50 120 160 172 183 205 200 230 (c) 12 14 20 22 24 27 40 45 (d) More than one of these are not valid inOrder traversals. (e) All of these are valid inOrder traversals.

MC3 (2.5pts)

Which of the following data structures is used in our implementation of a level order traversal of a binary tree?

(a) linked list (b) array (c) stack (d) queue (e) hash table

MC5 (2.5pts)

Think of an algorithm that uses a Stack to eciently check for unbalanced or unpaired delimiters. What is the maximum number of left-delimiters that will appear on the stack at any time when the algorithm analyzes the string {[({ }[ ])]({ })}?

(a) 1 (b) 2 (c) 3 (d) 4 (e) 5 or more

MC6 (2.5pts)

Suppose we remove the node containing key 10 from the AVL tree below.

!"

#"

$"

%"

&" '("

')"

*"

'" +" ''"

("

What sequence of rotations will restore the balance of the tree? (Assume IOP is used for 2-child removal.)

(a) leftRotate about 9, followed by rightRotate about 8. (b) rightRotate about 8. (c) leftRotate about 12, followed by rightRotate about 8. (d) rightLeftRotate about 9, followed by rightRotate about 8. (e) None of these choices will rebalance the tree.

MC7 (2.5pts)

Examine the mysteryFunction below? (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;

}

Which of the following Dictionary functions could invoke mysteryFunction more than once?

(a) insert(key); (b) remove(key); (c) find(key); (d) Two or more of these could invoke mysteryFunction more than once. (e) None of these would invoke mysteryFunction more than once.

MC8 (2.5pts)

Consider the AVL Tree built by inserting the following sequence of integers, one at a time in the given order: 10, 15 , 20 , 13 , 11. Which of the following statements is true about the tree?

(a) 11’s left child is NULL and 15’s left child is 11. (b) 11’s left child is NULL and 15’s left child is 13. (c) 11 ’s left child is 10 and 15 ’s left child is 11. (d) 11’s left child is 10 and 15’s left child is 13. (e) None of these answers is correct.

  1. [Eciency – 25 points].

Each item below is a description of a data structure, its implementation, and an operation on the structure. In each case, choose the appropriate worst case running time from the list below. The variable n represents the number of items (keys, data, or key/data pairs) in the structure. In answering this question you should assume the best possible implementation given the constraints, and also assume that every array is suciently large to handle all items. Please use the scantron sheets for your answers.

(a) O(1) (b) O(log n) (c) O(n) (d) O(n log n) (e) O(n 2 )

(MC 11) A Enqueue for a Queue implemented with an array.

(MC 12) A Pop for a Stack implemented with an array.

(MC 13) C Find a key in a Binary Tree (not necessarily BST).

(MC 14) B Remove the root of a balanced Binary Search Tree.

(MC 15) C Find the largest key in a Binary Search Tree.

(MC 16) B Find the largest key in an AVL Tree.

(MC 17) C For each node in a Binary Search Tree, compute the length of the longest path from the node to a leaf.

(MC 18) C Make a copy of an AVL tree.

(MC 19) C Determine if two given Binary Search Trees are copies of one another.

(MC 20) C Remove all the nodes in the right subtree of a non-empty AVL tree.

  1. [Quadtrees – 20 points].

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. 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).

class Quadtree { public: // constructors and destructor; all of the public methods from MP5, including:

void buildTree(PNG const & source, int d); RGBApixel getPixel(int x, int y) const; PNG 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); };

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 a new QuadtreeNode is NULL.

then all four children must exist–even in a pruned tree. Which one of the following terms is an appropriate description of this characteristic of our Quadtree? (Hint: we discussed this term in the context of binary trees, but it applies to Quadtrees, as well.)

perfect complete full balanced binary

(3 points, or 1 point for incorrect answer with explanation)

(d) (3 points) Suppose a pruned Quadtree has height h 0. What is the least number of nodes it contains in terms of h? 3 d 4 ⇤ h + 1

(3 points, or 1 point for incorrect answer with explanation)

(e) Analyze and give tight asymptotic bounds on the running time of each of the following functions from MP5. Your bound should be stated in terms of N , the number of nodes in the Quadtree, and you can assume that the functions are called on an unpruned tree. Briefly justify your answers. i. (2 points) buildTree: This function takes two arguments, a PNG object by reference and an integer d, and creates a Quadtree representing the upper-left d by d block of the PNG. (N is the number of nodes after the tree is built.)

O(n)

(2 points, or 1 point for incorrect answer with explanation)

ii. (2 points) getPixel: This function takes two arguments, x and y, and returns the RGBApixel corresponding to the pixel at coordinates (x, y) in the bitmap image which the quadtree represents. O(log(n)) (2 points, or 1 point for incorrect answer with explanation)

iii. (2 points) clockwiseRotate: This function rotates the Quadtree object’s underlying image clockwise by 90 degrees. O(n) (2 points, or 1 point for incorrect answer with explanation)

  1. [Flood Fill – 10 points].

(a) (4 points) Suppose we execute function DFSfillSolid, one of two main fill routines from MP4, on the following 8x8 pixel image, beginning at the circled node, (3,5), and changing white pixels to solid red. Place the numbers 1 through 12, in order, in the first 12 pixels whose colors are changed by the function. Assume that we start the algorithm by adding the circled cell to the ordering structure, and that we add the four neighboring pixels to the structure clockwise beginning on the right, followed by down, left, and up.

6 5 7 4 3 8 9 2 1 10 11 12

  • 1point: for avoiding the black squares (irrespective of traversal).
  • 1point: for a DFS.
  • 1point: in the correct direction (pixels should be to the left of the starting pixel).
  • 1point: all pixels are labeled in the correct order. (b) (3 points) Suppose we want to fill some part of a arbitrary n-by-n image. What is the worst-case running time of DFSfillSolid if we start from an arbitrary location? Be sure to give your running time in terms of n, the length of one side of the square image.

O(n 2 )

(c) (3 points) What data structure was used to order the points for filling in function DFSfillSolid?

Stack

  • 3 points: ‘Stack’
  • 3 points: ‘Queue’ and BFS in part (a)
  • 1 point: ‘Stack’ but performed a BFS in part (a)
  1. [Hashing – 10 points].

The following diagram is intended to illustrate the behavior of a given hash function. Specif- ically, our diagram illustrates the hash function defined by h(k1) = 3, h(k2) = 2, and h(k3) = 0, when mapped to a table of size 5. In the first 3 parts of this problem we will ask you to complete diagrams for hash functions with particular characteristics.

k

k

k

(a) (2 points) Complete the diagram below to illustrate a perfect hash function.

k

k k k k

Full credit if there are no collisions.

(b) (2 points) Complete the diagram below to illustrate a hash function that clearly and extremely violates the Simple Uniform Hashing Assumption.

k k

k k k

  • Full credit if all keys map to the same value
  • 1 point if the keys map to a few values

(c) (2 points) Complete the diagram below to illustrate a hash function with exactly two collisions on the given set of keys.

k

k k k k

  • Full credit if there are 2 collisions
  • 1 point if there is 1 collision

(d) (2 points) What is the load factor for each of the hash tables in parts (a) through (c)?

5 8

  • Full credit for the right answer
  • 1 point for 8/

(e) (2 points) We argue that the dictionary function find runs in constant time using a hash table. This argument depends on two things. List them here:

scratch paper