Second Examination Solution

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

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

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

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

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

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

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

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

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

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

Recitation 9. Prelim Review

Final Examination CSE 100 UCSD (Practice)

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

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

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

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

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

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

CS 315 Data Structures mid-term 2

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

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

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

Define the red- black tree properties Describe and implement rotations Implement red- black tree insertion

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

CSE373 Fall 2013, Midterm Examination October 18, 2013

ext Total Score /20 /20 /15 /20 /25 /5 Grader

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

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

CSL 201 Data Structures Mid-Semester Exam minutes

Section 05: Solutions

Section 1: True / False (1 point each, 15 pts total)

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

Solution READ THIS NOW! CS 3114 Data Structures and Algorithms

Prelim 2 Solution. CS 2110, April 26, 2016, 5:30 PM

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

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

CSE332 Summer 2010: Midterm Exam Sample Solutions

CS 112 Final May 8, 2008 (Lightly edited for 2012 Practice) Name: BU ID: Instructions

CSE 332, Spring 2010, Midterm Examination 30 April 2010

CSCE 2014 Final Exam Spring Version A

Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1

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

Largest Online Community of VU Students

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

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

Exam Data structures DAT036/DAT037/DIT960

Binary Search Trees Treesort

Section 05: Solutions

Practice Midterm Exam Solutions

Total Score /1 /20 /41 /15 /23 Grader

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

Prelim 2 Solution. CS 2110, April 26, 2016, 7:30 PM

Midterm solutions. n f 3 (n) = 3

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

Friday Four Square! 4:15PM, Outside Gates

CS301 - Data Structures Glossary By

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

CS 3114 Data Structures and Algorithms READ THIS NOW!

Section 1: True / False (2 points each, 30 pts total)

Total Score /15 /20 /30 /10 /5 /20 Grader

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

CS2110 Assignment 3 Inheritance and Trees, Summer 2008

Solutions to Exam Data structures (X and NV)

CS102 Binary Search Trees

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

Name Section Number. CS210 Exam #3 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

Computer Science Foundation Exam

CMSC351 - Fall 2014, Homework #2

Section 4 SOLUTION: AVL Trees & B-Trees

CLO Assessment CLO1 Q1(10) CLO2 Q2 (10) CLO3 Q4 (10) CLO4 Q3a (4)

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

Computer Science 136 Spring 2004 Professor Bruce. Final Examination May 19, 2004

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

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

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

Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Lesson 21: AVL Trees. Rotation

Exercise 1 : B-Trees [ =17pts]

CS210 (161) with Dr. Basit Qureshi Final Exam Weight 40%

Data Structures Question Bank Multiple Choice

CLOSED-BOOK-CLOSED-NOTES

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Course Review. Cpt S 223 Fall 2010

XC Total Max Score Grader

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

Total Score /15 /20 /30 /10 /5 /20 Grader

CSCI2100B Data Structures Trees

Section 05: Midterm Review

Course Review. Cpt S 223 Fall 2009

Lecture 15 Binary Search Trees

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

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

Priority Queues Heaps Heapsort

Part 1: Multiple Choice Circle only ONE answer. Each multiple choice question is worth 3.5 marks.

MLR Institute of Technology

Quiz 1 Solutions. Asymptotic growth [10 points] For each pair of functions f(n) and g(n) given below:

Computer Science E-22 Practice Final Exam

CS 3114 Data Structures and Algorithms READ THIS NOW!

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

Transcription:

University of Illinois at Urbana-Champaign Department of Computer Science Second Examination Solution CS 225 Data Structures and Software Principles Fall 2007 7p-9p, Thursday, November 8 Name: NetID: Lab Section (Day/Time): This is a closed book and closed notes exam. No electronic aids are allowed, either. You should have 5 problems total on 20 pages. The last two sheets are scratch paper; you may detach them while taking the exam, but must turn them in with the exam when you leave. Unless otherwise stated in a problem, assume the best possible design of a particular implementation is being used. Unless the problem specifically says otherwise, (1) assume the code compiles, and thus any compiler error is an exam typo (though hopefully there are not any typos), and (2) assume you are NOT allowed to write any helper methods to help solve the problem, nor are you allowed to use additional arrays, lists, or other collection data structures unless we have said you can. Please put your name at the top of each page. Problem Points Score Grader 1 20 2 20 3 20 4 20 5 20 Total 100

1. [Choices, Choices! 20 points]. MC1 (2.5pts) A post-order traversal of the following tree visits the nodes in which order? G C H B E K A D F I L J (a) G C B A E D F H K I J L (b) A B C D E F G H I J K L (c) A B D F E C J I L K H G (d) All of these are valid post-order traversals. (e) None of these is a valid post-order traversal. MC2 (2.5pts) The buildtree() function in MP5 took two arguments, a BMP object source and an integer resolution, and built a quadtree to represent the image. Which of the following function declarations uses const appropriately in this context? (a) void Quadtree::buildTree(BMP const & source, int resolution); (b) void Quadtree::buildTree(BMP & source, int resolution) const; (c) void Quadtree::buildTree(BMP & const source, int resolution); (d) none of the above

MC3 (2.5pts) Suppose that we have numbers between 1 and 1000 in a binary search tree and we want to search for the number 363. Which of the following sequences can not be the sequence of nodes visited in the search? (a) 2, 252, 401, 398, 330, 344, 397, 363 (b) 924, 220, 911, 244, 898, 258, 362, 363 (c) 2, 399, 387, 219, 266, 382, 381, 278, 363 (d) 925, 202, 911, 240, 912, 245, 363 (e) 935, 278, 347, 621, 399, 392, 358, 363 MC4 (2.5pts) Which of the following is the strongest valid statement made about AVL trees? (a) They are minimal height trees (b) They are height-balanced trees (c) They are minimal height and height-balanced trees (d) They are full, minimal height, and height-balanced trees (e) None of the above are valid statements MC5 (2.5pts) Suppose an order m B-tree contains n items. In the worst-case, how many CPU operations would be required to search the tree for a specific key? (a) O(log 2 n) (b) O(log m n) (c) O(m log 2 n) (d) O(m log 2 m) (e) O(m log m n)

MC6 (2.5pts) In an array-based implementation of a Heap, the right-child of the right-child of the node at index i, if it exists, can be found at what array location? (a) 4i + 3 (b) 2i + 1 (c) 4i + 1 (d) 2i + 2 (e) 4i + 2 MC7 (2.5pts) Which of these is a Huffman Code for the character frequencies: a = 5, b = 3, c = 4, d = 10? (a) a is 10, b is 110, c is 111, and d is 0. (b) a is 10, b is 01, c is 00, and d is 11. (c) a is 1, b is 01, c is 00, and d is 0. (d) a is 111, b is 0, c is 10, and d is 110. (e) None of these. MC8 (2.5pts) For which of the following data structures does the Find function require no worse than O(log n) running time? (a) Binary Search Tree (worst case analysis) (b) Heap (worst case analysis) (c) Hashing (under simple uniform hashing assumption, and with an ideal hash function) (d) Two or more of the structures in (a) through (d) require no worse than O(log n) running time. (e) Find does not run in time O(log n) for any of these structures. The correct answer for this problem is (c), since, under the described conditions, hashing provides O(1)-time find. BUT, we did not emphasize the fact that if a function f(n) is O(g(n)), and g(n) is O(h(n)), then f(n) is O(h(n)), so we also counted (e) as a correct response.

2. [QuadTrees 20 points]. For this question, you will be writing a private member function called blockyblur whose purpose is to add very basic blur functionality to the MP5 Quadtree class. It should be implemented using the following rules: (a) Block (b) Blur i. If the quadtree is null or just a single node, don t do anything. ii. If a node is a parent of 4 leaves in the original tree, remove the leaves from the tree (do not do this recursively). This creates a blocky effect, where every group of four pixels is now represented by one pixel. i. If a node has no parent or if it has had no children removed, don t change the value of its element (Hint: your code will be simplified if you observe that the average of a number and itself is just that number). ii. If a node has had its children removed and if it has a parent, make its element the average of its own element and its parent s. Recall that in our quadtrees, the element is an RGBApixel. You may assume that the quadtree is complete and that it has been built from an image that has size 2 k 2 k. You need not assume anything in particular about the value of the element in any node, except that it contains some valid pixel (presumably assigned when the tree was built). Finally, you MAY write private Quadtree helper functions to solve this problem, but you are not required to do so. We have provided the public member function blurtree which calls blockyblur on the root node of the quadtree, and another function avg which computes the average of two RGBApixel objects (you are welcome to use this function). // public interface for blurring a Quadtree void Quadtree::blurTree(void) { // note that in this function call, we re using root->element as the value // of the parentpixel of the root because we want the element of a single // node quadtree to remain unchanged. That is, if the root HAD a parent, // it s element would have the same value as root->element. blockyblur(root, root->element); // returns an RGBApixel representing the average of two RGBApixels RGBApixel avg(rgbapixel p1, RGBApixel p2); Write your code for blockyblur and any helper functions on the next page(s).

// performs a basic blur on the tree, starting at the parameter node void Quadtree::blockyBlur(QuadtreeNode* current, RGBApixel parentpixel) { Ideal Solution: // performs a basic blur on the tree, starting at the parameter node void Quadtree::blockyBlur(QuadtreeNode* current, RGBApixel parentpixel) { setavg(current); combined(current, parentpixel); void Quadtree::setAvg(QuadtreeNode* current) { if(current->nechild == NULL) { return; setavg(current->nechild); // recurse on all four children setavg(current->nwchild); setavg(current->swchild); setavg(current->sechild); // set the current nodes element to avg of its children current->element.red = (current->nwchild->element.red + current->nechild->element.red + current->sechild->element.red + current->swchild->element.red) / 4; current->element.green = (current->nwchild->element.green + current->nechild->element.green + current->sechild->element.green + current->swchild->element.green) / 4; current->element.blue = (current->nwchild->element.blue + current->nechild->element.blue + current->sechild->element.blue + current->swchild->element.blue) / 4; void Quadtree::combined(QuadtreeNode* current, RGBApixel parentpixel) { if(current->nechild == NULL) { // handle single node tree return; // you must have four children at this point if(current->nechild->nechild == NULL) { // current is parent of four leaves removeleaf(current->nechild); removeleaf(current->nwchild); removeleaf(current->sechild); removeleaf(current->swchild); // since children have been removed at this point and if current // doesnt have a parent, its pixel wont be changed

current->element = avg(current->element, parentpixel); else { combined(current->nechild, current->element); // recurse on all four children combined(current->nwchild, current->element); combined(current->swchild, current->element); combined(current->sechild, current->element); void Quadtree::removeLeaf(QuadtreeNode*& current) { delete current; current = NULL; Grading Rubric: Setting element in non-leaf nodes to average of children (1 point) Handling tree with single node (1 point) Recursion (4 points) Correct base case (2 points) Correct recursive calls (2 points) Block Functionality (4 points) Correctly checking if node is a parent of four leaves (2 points) Proper memory management (2 points) Correctly deallocating dynamic nodes (1 point) Setting deallocated pointers to NULL (1 point) Blur Functionality (4 points) Correctly checking if node had its children removed and if it has a parent or using the hint to simplify this check(2 point) Correctly setting element to the correctly computed average (2 point) Comments (6points) 1 point for commenting about when to recurse 1 point for commenting about the base cases 2 points for commenting about how and when to block 2 points for commenting about how and when to blur

3. [Analysis 20 points]. (a) Suppose we have a min-heap A and a max-heap Z, each of which store integers. Assume also that all the keys in A are less than all the keys in Z, and A and Z have the same number of keys. (Note: a max-heap is just a Heap where all paths from root to leaf are decreasing.) Finally, assume that we have a Binary Search Tree class called BST with all the usual member functions. Consider this function play: BST play(minheap A, maxheap Z){ BST B; while (!A.isEmpty &&!Z.isEmpty) { B.insert(Z.removeMax()); B.insert(A.removeMin()); return B; i. Circle the illustration that best describes the BST returned by play. we have: A: min-heap Z: max-heap B: BST two rules of the play: 1) all the keys in A are less than all the keys in Z 2) A and Z have the same number of keys (let it be n) We start building BST B by removing the max (let call b1) from Z, and adding it to B as it s root. (note that each removing from maxheap or minheap needs heapifying to preserve the max or min heap structure). The next node that we will insert into the BST is the minumum of the minheap, let call it b2. As it mentioned in the rules all the keys of minheap are less than all the keys in maxheap, so b2 is less than b1, and it will be the left child b1. Suppose that there are still some nodes remain in min and maxheap. In the next iteration of while again we remove the

max from the maxheap, b3. This would be less than b1, because it is second max of maxheap, (remember when we removemax nodes from maxheap one by one, they are sorted in decreseaing order). so we must add it somewhere in left child of b1. But regarding the rule of play b3 b2, so we must add it as the right child of b2. ii. Analyze the total running time of play if each Heap contains n data items. Solution: For simplifying the runtime analysis we can write the code as following: BST play(minheap A, maxheap Z) { BST B; while(!a.isempty() &&!Z.isEmpty()) { N=Z.removeMax(); B.insert(N); N=Z.removeMin(); B.insert(N); return B; It is obvious that the while loop goes through all n nodes of minheap and maxheap. As it is shown all the operations inside the loop are done sequentially, so we need to find the maximum order of the operations inside the loop and just multiply the maximum order by n for finding the total running time. removemax and removemin from heap each takes O(log(n)). Insert into BST takes O(n) in the worst case. Therefore the maximum order of operations inside the loop is O(n). By multiplying n O(n) the order is O(n 2 ).

(b) Suppose we have a min-heap A and a max-heap Z, each of which store integers. Assume also that all the keys in A are greater than all the keys in Z, and A and Z have the same number of keys. (Note: a max-heap is just a Heap where all paths from root to leaf are decreasing.) Finally, assume that we have an AVL Tree class called AVLTree with all the usual member functions. Consider this function work: AVLTree work(minheap A, maxheap Z){ AVLTree B; while (!A.isEmpty &&!Z.isEmpty) { B.insert(Z.removeMax()); B.insert(A.removeMin()); return B; i. Circle the illustration that best describes the AVL Tree returned by work. we have: A: min-heap Z: max-heap B: AVL tree two rules of the play: 1) all the keys in A are greater than all the keys in Z 2) A and Z have the same number of keys (let it be n) We start building AVLTree B by removing the max (let call b1) from Z, and adding it to B as it s root. (note that each removing from maxheap or minheap needs heapifying to preserve the max or min heap structure). The next node that we will insert into the BST is the minumum of the minheap, let call it b2. As it mentioned in the rules all the keys of minheap are greater than all the keys in maxheap. We just need to remember that after each insertion into AVLTree we need to re-balance it, so the final tree is balanced.

ii. Analyze the total running time of work if each Heap contains n data items. Solution: For simplifying the runtime analysis we can write the code as following: AVLTree work(minheap A, maxheap Z) { AVLTree B; while(!a.isempty() &&!Z.isEmpty()) { N=Z.removeMax(); B.insert(N); N=Z.removeMin(); B.insert(N); return B; like previous section, It is obvious that the while loop goes through all n nodes of minheap and maxheap. As it is shown all the operations inside the loop are done sequentially, so we need to find the maximum order of the operations inside the loop and just multiply the maximum order by n for finding the total running time. removemax and removemin from heap each takes O(log(n)). Insert into AVLTree takes O(log(n)). Therefore the maximum order of operations inside the loop is O(log(n)). By multiplying n O(log(n)) the order is O(n log(n)). My Rubric for grading this question: There are four parts that each has 5 points. So highlighting the correct structure of the tree in the first parts of a and b gets 5 points for each, all or nothing. In the analysis part: 1.5 points if you detect the loop. 1.5 points if you detect the correct order of operations inside the loop (O(n) for part a-ii & O(log(n)) for part b-ii ). 1 point if you multiply n by whatever you got from the previous step 1 point if the final answer is correct Also, minus 1 point for wrong statements.

4. [All about AVL trees 20 points]. (a) Recall that AVL Trees perform rotations to maintain height balance. Draw a picture of a 7 node binary search tree that would be an AVL tree after a right-left double rotate, and show that it becomes an AVL tree after those rotations. In your illustrations, be sure to include a valid key in each node, and an indication of which node was originally out of balance. Figure 1: right-left rotation Sol. 4(a). Figure 1 gives one possible solution. Many other solutions are also possible. Grading scheme: total 5 points Correct example of tree = 2 points Valid keys in tree =.5 point Writing balance to indicate which node was out of balance =.5 point First correct rotation = 1 point Second correct rotation = 1 point (b) In this part of the problem you will implement the rotateleft member function for the AVLTree class. Here is the partial AVLTree class definition: class AVLTree {

public:... private: class AVLTreeNode { public:... int element; int height; AVLTreeNode* left; AVLTreeNode* right; ; int max(int a, int b) const; int height(avltreenode const * x) const;... // function declarations for rotations go here. // We re not telling you what they look like. ; The function height takes an AVLTreeNode pointer argument and returns the integer height of the subtree rooted at the parameter node. The function max takes two integer arguments and returns an integer that is the larger of the two parameters. Each of the rotation functions takes a reference to an AVLTreeNode pointer, performs its rotation, and returns nothing. Using this information, implement the rotateleft member function here: Sol. 4(b). Figure 2 gives the diagram for left rotation. One solution is given below (other solutions are also possible): void AVLTree::rotateLeft(AVLTreeNode * & node) { // no need to check for conditions, as they are supposed // to be checked before calling this function AVLTreeNode* y = node; node = node->right; y->right = node->left; node->left = y; y->height = height(y); Grading scheme: total 5 points Considering possible presence of T2 (see the figure) = 1 point Correct rotation process = 3 points Updating height = 1 point

Figure 2: left rotation (c) Prove by induction that the number of null pointers in an n node AVL tree is n + 1. Let T be an arbitrary AVL tree with n nodes. Assume (Inductive Hypothesis) that any AVL tree with j < n nodes has j + 1 null pointers. Base Case (n = 0): Suppose T is a tree with zero nodes (an empty tree). Then, since we represent T using one null pointer, the number of null pointers is one more than the number of nodes. Inductive Case (n > 0): By definition of AVL trees, T s left and right children, T L and T R, are AVL trees. Denote by a and b the number of nodes in T L and T R respectively. Since a < n and b < n we can apply the inductive hypothesis to T L and T R, and thus T L has a + 1 null pointers and T R has b + 1 null pointers. The total number of null pointers in T is (a + 1) + (b + 1), and this is n + 1 since the total number of nodes is n = a + b + 1. Grading Rubric: Almost no one approached this the correct way, so I gave full credit for some incorrect

responses (what was I thinking!!??). The common error was to remove a single node from a tree of size n + 1, apply the inductive hypothesis to the resulting tree, and adjust the pointer count by adding back in the removed node. The problem is that you cannot apply the inductive hypothesis to the tree in this case, because you can t be sure that it s an AVL tree. 1 point for base case 2 points for stating inductive hypothesis 2 points for the inductive argument (d) Suppose your study partner suggests that the two of you should implement an AVL tree using an array, much like we use an array to implement a Heap. First, explain why your partner thinks this might be a good idea. Second, give your argument that it really isn t such a good idea after all. In your dialogue, you should address basic functionality (find, insert, remove) and rotations. You may also wish to discuss space usage and running times. The graders will be delighted if you illustrate your arguments using small example trees. Partner: Though an n node AVL tree isn t complete, it has height which is O(log n), which isn t so bad. If we store the elements in an array like we do for heaps (or any other complete tree) we probably won t be wasting too much space. (An analysis that I didn t expect shows that the space required is no more than n 2 for an n node AVL tree.) The simplicity of the parent and child relationships makes this really attractive! You: Nah. While find would work in O(log n) time, insert and remove would be disastrous because of the rearrangement that occurs with rotations. It s possible that every single element of the array would need to be moved to a different spot upon a rotation, even if the relative positions of nodes in subtrees didn t change. This is not a problem in a pointer based AVL tree. Grading rubric: Maximum of 2 points if there was no explanation of how an AVL tree would be stored in a manner similar to a Heap. To detect what you were imagining, I looked for pictures, or for mention of the indices of children (2i and 2i +1), or for mention that there may be gaps in the array if the tree is not complete. An argument similar to the correct one but with no mention of rotations received 3 points. Beyond that, i basically just gave a point for each correct observation, and subtracted a point for ea ch incorrect observation.

5. [Make Hash of It 20 points]. Suppose you are given a table T of size 11 and a set S = {5, 40, 18, 22, 16, 30, 27 to hash into the table, using the hash function h(k) = k%11. (a) Show T after the values from S are entered into it, using separate chaining. Solution: (b) Show T after the values from S are entered into it, using linear probing with the function h(k,i) = (h(k)+2i)%11. Solution: (c) For this particular T and S, which is the better choice, and why? Your answer should discuss the number of probes necessary to Find a particular key. Solution: Separate chaining is the better choice for this set S. The longest list in the separate chaining case contains 3 elements to search through, while linear probing requires as many as 6 probes to insert or find elements.

(d) Give an example of a set S with 7 elements for which the other method is the better choice. Solution: A good set for linear probing is one that has no collisions, e.g., {1, 2, 3, 4, 5, 6, 7. In this case, the extra space required for the lists of separate chaining is a waste of memory, as a find is equally fast for either method. Rubric: (a) 7 pts -1 pt if array is not used to hold header nodes -1 pt if elements are not inserted at the head of the list -1 pt each for arithmetic errors in placing elements -3 pts if no lists were used -2 pts if collisions were resolved using h(k, i) = (h(k) + i)%11 (b) 7 pts -1 pt each for arithmetic errors in placing elements -4 pts if consistently misusing h(k, i) = (h(k) + 2i)%11 with respect to i -3 pts if h(k, i) = (h(k) + i)%11 was used correctly (c) 4 pts 2 pts for a correct answer 2 pts for a correct reason -3 pts if answer was wrong because (a) or (b) was done incorrectly (d) 2 pts -1 pt for right set, wrong reason -1 pt if correct relative to (c) (and (c) was wrong) and a reason was given