University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Similar documents
University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Second Examination Solution

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

(b) int count = 0; int i = 1; while (i<m) { for (int j=i; j<n; j++) { count = count + 1; i = i + 1; O(M + N 2 ) (c) int count = 0; int i,j,k; for (i=1

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Chapter 4: Trees. 4.2 For node B :

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

Algorithms. AVL Tree

CS 216 Exam 1 Fall SOLUTION

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CSE 332, Spring 2010, Midterm Examination 30 April 2010

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Solutions. Suppose we insert all elements of U into the table, and let n(b) be the number of elements of U that hash to bucket b. Then.

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CMSC351 - Fall 2014, Homework #2

Largest Online Community of VU Students

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

ECE250: Algorithms and Data Structures Midterm Review

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions:

Solution to CSE 250 Final Exam

CSCI2100B Data Structures Trees

Final Examination CSE 100 UCSD (Practice)

CS 315 Data Structures mid-term 2

Chapter 20: Binary Trees

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION /5:30-7:00

Computer Science E-22 Practice Final Exam

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

Midterm 2 Exam Principles of Imperative Computation. Tuesday 31 st March, This exam is closed-book with one sheet of notes permitted.

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

UNIVERSITY REGULATIONS

(D) There is a constant value n 0 1 such that B is faster than A for every input of size. n n 0.

Part 2: Balanced Trees

NET/JRF-COMPUTER SCIENCE & APPLICATIONS. Time: 01 : 00 Hour Date : M.M. : 50

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Midterm solutions. n f 3 (n) = 3

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

Midterm II Exam Principles of Imperative Computation Frank Pfenning. March 31, 2011

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Trees, Part 1: Unbalanced Trees

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Chapter 2: Basic Data Structures

COS 226 Algorithms and Data Structures Fall Midterm

Assume you are given a Simple Linked List (i.e. not a doubly linked list) containing an even number of elements. For example L = [A B C D E F].

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Trees. (Trees) Data Structures and Programming Spring / 28

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION B /13:30-14:50 MC-4021/RCH-211

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

CSCE 2014 Final Exam Spring Version A

UNIVERSITY REGULATIONS

CS 3114 Data Structures and Algorithms READ THIS NOW!

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Multiple choice questions. Answer on Scantron Form. 4 points each (100 points) Which is NOT a reasonable conclusion to this sentence:

CS 106X Sample Final Exam #2

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:30-7:00

lecture14: Tree Traversals

Data Structure (CS301)

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Part I: Short Answer (12 questions, 65 points total)

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013

Introduction to Algorithms October 12, 2005 Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson Quiz 1.

Final Exam. Name: Student ID: Section: Signature:

This examination has 11 pages. Check that you have a complete paper.

Friday Four Square! 4:15PM, Outside Gates

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

CS 251, LE 2 Fall MIDTERM 2 Tuesday, November 1, 2016 Version 00 - KEY

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Computer Science Foundation Exam

Test #2. Login: 2 PROBLEM 1 : (Balance (6points)) Insert the following elements into an AVL tree. Make sure you show the tree before and after each ro

Suppose that the following is from a correct C++ program:

Course Review. Cpt S 223 Fall 2009

Prelim 2 SOLUTION. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

Data Structures and Algorithms

Tutorial AVL TREES. arra[5] = {1,2,3,4,5} arrb[8] = {20,30,80,40,10,60,50,70} FIGURE 1 Equivalent Binary Search and AVL Trees. arra = {1, 2, 3, 4, 5}

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

EECS 311 Data Structures Midterm Exam Don t Panic!

Transcription:

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 implementation is being used. Unless the problem specifically says otherwise, assume the code compiles, and thus any compiler 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 3 20 4 10 5 10 6 10 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

MC4 (2.5pts) Consider the following recursive C++ function, and assume our standard node-based implementation of the BinaryTree class which includes a private definition of a Node class. #include <vector> #include <iostream> using namespace std; template <typename T> void BinaryTree<T>::mystery(const Node * croot, vector<t> & unk) const { if (croot!= NULL) { unk.push_back(croot->elem); if (croot->left == NULL && croot->right == NULL) { for (size_t i = 0; i < unk.size(); i++) cout << << unk[i]; cout << endl; else { mystery(croot->left, unk); mystery(croot->right, unk); unk.pop_back(); Among the following choices, print the best name for the function. (That is, we are asking for a name that describes what the function does.) (a) printmirror - prints the mirror image of the binary tree. (b) printtree - prints each node in the tree exactly once, in preorder. (c) printlevel - prints each node in the tree exactly once in level order from bottom to top. (d) printpaths - prints every path from the root to a leaf in the tree. (e) None of these options is a good name for the function.

MC5 (2.5pts) Think of an algorithm that uses a Stack to efficiently 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.

MC9 (2.5pts) What is the maximum number of keys in a binary search tree of height 30? (a) 2 29 1 (b) 2 30 1 (c) 2 31 1 (d) The answer cannot be determined by the given information. (e) None of these answers is correct. MC 10 (2.5pts) Use the following 3 code examples to answer the question below. arrays and images have been properly initialized to hold valid data. Please assume that all (i) RGBAPixel colorarray[100]; loadcolors(colorarray); // initialize array values #pragma omp parallel for for (int i = 1; i < 100; i++) { colorarray[i] = colorarray[i-1]; (ii) PNG image("dogpic.png"); // load from file #pragma omp parallel for for (int i = 0; i < image.width(); i++) { for (int j = 0; j < image.height(); j++) { RGBAPixel temp = *image(i, j); *image(i, j) = *image(i, image.height() - 1 - j); *image(i, image.height() - 1 - j) = temp; (iii) int table [10][10]; #pragma omp parallel for for(int i = 0; i < 10; i++) for(int j = 0; j < 10; j++) table[i][j] = (i+1)*(j+1); Which of the code examples above is/are NOT correctly parallelized? (a) Only item (i) is incorrect. (b) Only item (ii) is incorrect. (c) Only item (iii) is incorrect. (d) Two of the above examples are incorrect. (e) All statements (i), (ii), and (iii) are correct.

2. [Efficiency 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 sufficiently 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) (MC 12) (MC 13) (MC 14) (MC 15) (MC 16) (MC 17) (MC 18) (MC 19) (MC 20) Enqueue for a Queue implemented with an array. Pop for a Stack implemented with an array. Find a key in a Binary Tree (not necessarily BST). Remove the root of a balanced Binary Search Tree. Find the largest key in a Binary Search Tree. Find the largest key in an AVL Tree. For each node in a Binary Search Tree, compute the length of the longest path from the node to a leaf. Make a copy of an AVL tree. Determine if two given Binary Search Trees are copies of one another. Remove all the nodes in the right subtree of a non-empty AVL tree.

3. [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; QuadtreeNode* nechild; QuadtreeNode* swchild; QuadtreeNode* sechild; // pointer to northwest child // pointer to northeast child // pointer to southwest child // 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.

(a) (3 points) Write a public member function void buildtree(png const & source, int d), which creates a Quadtree rooted at private member root representing the d-by-d block in the upper left corner of the image in source. Your function can call the private helper function that you will be writing in the next part of the problem, and it can use any of the other private member functions listed above. You may assume that the image is at least d-by-d, that d 1, and that d is a power of 2. Write the function as it would appear in the quadtree.cpp file for the Quadtree class. void buildtree(png const & source, int d) { // Your code goes here (b) (6 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 comment your code. :: ( ) {

(c) (2 points) In our implementation of a Quadtree we have said that if a node has one child, 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 (d) (3 points) Suppose a pruned Quadtree has height h 0. What is the least number of nodes it contains in terms of h? (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.) 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. iii. (2 points) clockwiserotate: This function rotates the Quadtree object s underlying image clockwise by 90 degrees.

4. [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. (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. (c) (3 points) What data structure was used to order the points for filling in function DFSfillSolid?

5. [AVL Tree Height 10 points]. In this problem you will complete the proof of the following theorem: The height of an n-node AVL Tree is O(log n). Fill in the blanks to complete the proof. Preliminaries: Let H(n) denote the maximum height of an n-node AVL Tree, and let N(h) denote the minimum number of nodes in an AVL tree of height h. To prove that H(n) = O(log n), we argue that H(n) 2 log 2 n, for all n. Rather than prove this inequality directly, we ll show equivalently that N(h). Proof: For an arbitrary value of h, the following recurrence holds for all AVL Trees: N(h) = + + and N(0) =, N(1) = This expression for N(h) simplifies to the following inequality which is a function of N(h 2): N(h) By an inductive hypothesis which states: we now have: which was what we wanted to prove. N(h) 2 =,

6. [Hashing 10 points]. The following diagram is intended to illustrate the behavior of a given hash function. Specifically, 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. 0 k1 k2 k3 1 2 3 4 (a) (2 points) Complete the diagram below to illustrate a perfect hash function. 0 k1 k2 k3 k4 k5 1 2 3 4 5 6 7 (b) (2 points) Complete the diagram below to illustrate a hash function that clearly and extremely violates the Simple Uniform Hashing Assumption. 0 k1 k2 k3 k4 k5 1 2 3 4 5 6 7

(c) (2 points) Complete the diagram below to illustrate a hash function with exactly two collisions on the given set of keys. 0 k1 k2 k3 k4 k5 1 2 3 4 5 6 7 (d) (2 points) What is the load factor for each of the hash tables in parts (a) through (c)? (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