PROBLEM 1 : (I'll huff and I'll puff and I'll... (10 pts)) The Huffman tree below is used for this problem. e s r o t Part A(4 points) Using this tree

Similar documents
Test 2: CPS 100. Owen Astrachan. April 3, Name: Login: (1 pt)

Test 2: CPS 100. Owen Astrachan. November 15, 2000

Test 1: CPS 100. Owen Astrachan. February 23, 2000

Compsci 100. Owen Astrachan April 14, 2010

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

Test 1: CPS 100. Owen Astrachan. October 11, 2000

Test 2: Compsci 100. Owen Astrachan. November 12, 2008

"apple" "grape" "grape" "grape" "apple"

// a stack is printed from bottom (leftmost) to top (rightmost) System.out.println(stk);

Test 2: Compsci 100. Owen Astrachan. November 14, 2006

"sort A" "sort B" "sort C" time (seconds) # of elements

Test 2: CPS 100. Owen Astrachan. November 10, 2004

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

PROBLEM 1 : (Vocabulary: 8 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the context

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

PROBLEM 1 : (ghiiknnt abotu orsst (9 points)) 1. In the Anagram program at the beginning of the semester it was suggested that you use selection sort

PROBLEM 1 : (Short Answer: 13 points) Three points each, except for last two which are two points 1. Both bubble-sort and selection sort are O(n 2 ) s

Test 2: Compsci 100. Owen Astrachan. November 11, 2009

Suppose we want to create a data structure called DataStore that has the following operations:

Test 2: CPS 103 Owen Astrachan November 19, 1993 Name: Honor code acknowledgement (signature) Problem 1 value 9 pts. grade Problem 2 12 pts. Problem 3

cameron grace derek cameron

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

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

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

PRACTICE MIDTERM EXAM #2

Test 2: Compsci 100. Owen Astrachan. April 2, 2008

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

CompSci 201 Tree Traversals & Heaps

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

Exam Data structures DAT036/DAT037/DIT960

class for simulating a die (object "rolled" to generate a random number) Dice(int sides) -- constructor, sides specifies number of "sides" for the die

DUKE UNIVERSITY Department of Computer Science. Test 2: CompSci 100

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

DUKE UNIVERSITY Department of Computer Science. Test 2: CompSci 100e

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

CS 223: Data Structures and Programming Techniques. Exam 2

PROBLEM 1 : (And the winner is...(12 points)) Assume you are considering the implementation of a priority queue that will always give you the smallest

CS 216 Exam 1 Fall SOLUTION

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

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

CMSC 341 Lecture 10 Binary Search Trees

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

Largest Online Community of VU Students

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

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

Test 1: CPS 100. Owen Astrachan. October 1, 2004

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID#

No aids allowed. No books. No notes. No calculators. No computers. No communicating devices.

UNIVERSITY REGULATIONS

COMP : Trees. COMP20012 Trees 219

CS302 Midterm Exam Answers & Grading James S. Plank September 30, 2010

CSE373 Fall 2013, Midterm Examination October 18, 2013

CSE373 Winter 2014, Midterm Examination January 29, 2014

PROBLEM 1 : (Vocabulary: 8 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the context

Test 1: Compsci 100. Owen Astrachan. February 15, 2006

PRACTICE MIDTERM EXAM #1

Draw the resulting binary search tree. Be sure to show intermediate steps for partial credit (in case your final tree is incorrect).

CSE 332, Spring 2010, Midterm Examination 30 April 2010

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}

CSE332 Summer 2010: Midterm Exam Sample Solutions

Computer Science Foundation Exam

UNIVERSITY REGULATIONS

Programming Abstractions

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

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

CSCE 2014 Final Exam Spring Version A

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination

Huffman, YEAH! Sasha Harrison Spring 2018

15 July, Huffman Trees. Heaps

CMSC 341 Leftist Heaps

CS106X Handout 35 Winter 2018 March 12 th, 2018 CS106X Midterm Examination

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

Exam Data structures DIT960/DAT036

Final Exam Solutions PIC 10B, Spring 2016

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

2016 Spring COP 3502 Final Exam. Date: 4/28/2016. Name:

Before calling Prepend(list,13) After calling Prepend(list,13) After calling RemoveLast(list)

PROBLEM 1 : (Trouble?: (14 pts)) Part A: (6 points) Consider the function Trouble below. int Trouble (const Vector<int> & numbers, int size) if (size

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Programming Abstractions

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

(the bubble footer is automatically inserted into this space)

CS 61B, Spring 1996 Midterm #1 Professor M. Clancy

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

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

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

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

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

Test 2: CPS Owen Astrachan. November 17, Name: Honor code acknowledgement (signature)

Second Examination Solution

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Data Structure (CS301)

CS 216 Fall 2007 Final Exam Page 1 of 10 Name: ID:

CS 315 Data Structures mid-term 2

Test 2 Practice : Compsci 201

CS 315 April 1. Goals: Heap (Chapter 6) continued review of Algorithms for Insert DeleteMin. algoritms for decrasekey increasekey Build-heap

Trees. A tree is a directed graph with the property

Midterm solutions. n f 3 (n) = 3

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

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

Transcription:

Test 2: CPS 100 Owen Astrachan April 4, 2002 Name: (1 pt) Login: Honor code acknowledgment (signature) Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 Problem 6 TOTAL: value 10 pts. 12 pts. 8 pts. 10 pts. 29 pts. 10 pts. 80 pts. grade This test has 10 pages, be sure your test has them all. Do NOT spend too much time on one question remember that this class lasts 75 minutes. In writing code you do not need to worry about specifying the proper #include header files. Assume that all the header files we've discussed are included in any code you write. Some common recurrences and their solutions. T(n) = T(n/2) + O(1) O(log n) T(n) = T(n/2) + O(n) O(n) T(n) = 2T(n/2) + O(1) O(n) T(n) = 2T(n/2) + O(n) O(n log n) T(n) = T(n-1) + O(1) O(n) T(n) = T(n-1) + O(n) O(n 2 ) 1

PROBLEM 1 : (I'll huff and I'll puff and I'll... (10 pts)) The Huffman tree below is used for this problem. e s r o t Part A(4 points) Using this tree, decode the following Huffman-encoded bitstream 111110011001011010111 Part B(3 points) Briefly, what was the reason for using the PSEUDO EOF character in the Huffman coding program? Part C(3 points) The tpqueue class uses a heap for implementing a priority queue. In a heap, insert is O(log n) and deletemin is O(log n). Suppose you replace this heap-implementation in your Huffman compression program with an implementation that uses an unsorted vector where the complexity of insert is O(1) and the complexity of deletemin is O(n). You will not notice any substantial change in the time it takes your program to run on nearly all files. Why? 2

PROBLEM 2 : (AVL (12 points)) Part A: 4 points The strings below are inserted, in the order shown, into an initially empty AVL tree. Draw the resulting tree. Note: there should be no rotations necessary when inserting these strings. "Monkey", "Giraffe", "Tiger" "Cheetah", "Kangaroo", Part B: 4 points Using the tree you drew in Part A, insert the string "Donkey" into the tree so that the tree remains an AVL tree. Draw the resulting tree. Part C: 4 points Using the tree you drew in Part A, insert the string "Lion" into the tree so that the tree remains an AVL tree. Draw the resulting tree. 3

PROBLEM 3 : (Like finding a needle in a Haystack (8 points)) The code below finds the smallest value in a stack, but empties the stack in finding the minimal value. int min(tstack<int>& s) // pre: s.size() >= 1 // post: returns smallest element in stack // stack is empty after function executes int min = s.top(); s.pop(); while (s.size() > 0) int value; s.pop(value); if (value < min) min = value; return min; Write a version of min so that the stack passed in contains the same elements in the same order after the function executes. You may only define int variables in writing min, you may not define stacks, queues, vectors, etc. You'll need to make the function recursive to solve this problem. Note: the stack will be changed during the execution of min, but it should be restored to its original contents before min finishes. int min(tstack<int>& s) // pre: s.size() >= 1, stack = (s1, s2,..., sn) // post: returns smallest element in stack, stack = (s1, s2,..., sn) 4

PROBLEM 4 : (Heap o' Trouble (10 points)) Assume min-heaps are implemented using a vector as we discussed in class so that the root of the heap has index 1 and the children of the value stored at index k have indexes 2 k (left-child) and 2 k +1 (right-child). Here is part of the class declaration for a heap that stores strings. class StringHeap // public declarations not shown private: ; tvector<string> mylist; int mysize; // number of elements in heap The public member functions StringHeap::getMin() and StringHeap::getMax() follow (note: getmax is not a standard priority queue/heap function.) string StringHeap::getMin() const // post: returns minimal element in heap return mylist[1]; string StringHeap::getMax() const // post: returns maximal element in heap int maxindex = 1; for(int k=maxindex+1; k <= mysize; k++) if (mylist[k] > mylist[maxindex]) maxindex = k; return mylist[maxindex]; Part A(3 points) In a heap with n elements, what is the big-oh complexity of StringHeap::getMin() and why? Part B(3 points) In a heap with n elements, what is the big-oh complexity of StringHeap::getMax() and why? Part C(4 points) Describe a simple change to the implementation of StringHeap::getMax() that will make the function roughly twice as fast although the big-oh complexity will remain the same. 5

PROBLEM 5 : (Multiset (29 points)) Amultiset is similar to a set but stores duplicates. The code below on the left generates the output shown on the right and illustrates some of the multiset functions. MultiSet::unique returns the number of different elements in a MultiSet (ignoring duplicates). MultiSet::size returns the total number of elements in a MultiSet (including duplicates). MultiSet::count returns the number of occurrences of a specifiec string. code output MultiSet multi; multi.insert("apple"); multi.insert("cherry"); multi.insert("apple"); multi.insert("cherry"); multi.insert("apple"); 3 2 0 total = 5 unique = 2 cout << multi.count("apple") << endl; cout << multi.count("cherry") << endl; cout << multi.count("orange") << endl; cout << "total = " << multi.size() << endl; cout << "unique = " << multi.unique() << endl; Suppose member functions MultiSet::insert and MultiSet::size are implemented as follows where mylist is a vector of strings. void MultiSet::insert(const string& s) mylist.push_back(s); int MultiSet::size() const return mylist.size(); Part A (4 pts) Given these implementations, what is the big-oh complexity of the function MultiSet::count(). Justify your answer. Part B (4 pts) Given these implementations, describe a method for implementing MultiSet::unique that runs in O(n log n) time. Justify why your solution is O(n log n). None of the other methods should change, but MultiSet::unique should run in O(n log n) time. 6

The code below is a partial implementation of a MultiSet class implemented using an HMap object. This implementation is used for the rest of this question (this code is duplicated on the separate code handout). This is a different implementation than used in parts A and B. class MultiSet public: MultiSet(); void insert(const string& s); // add element to MultiSet int count(const string& s) const; // returns # occurrences of s int size() const; // returns total # elements int unique() const; // returns # unique elements ; private: tmap<string,int> * mymap; MultiSet::MultiSet() : mymap(new HMap<int>(10001)) // post: this MultiSet is empty int MultiSet::unique() const // post: returns # unique elements (repeats not counted) in this MultiSet return mymap->size(); void MultiSet::insert(const string& s) // post: s is added to this MultiSet if (mymap->contains(s)) mymap->get(s)++; else mymap->insert(s,1); Part C (4 pts) Given this implementation write MultiSet::count so that it runs in O(1) time in the average case. int MultiSet::count(const string& s) const // post: returns # times s occurs in this MultiSet // (returns 0 if s not in this MultiSet) 7

Part D (6 pts) The member function MultiSet::size is implemented as follows: int MultiSet::size() const // post: returns total # elements (including repeated) in MultiSet int count = 0; Iterator<pair<string,int> > * it = mymap->makeiterator(); for(it->init(); it->hasmore(); it->next()) count += it->current().second; return count; Describe modifications to the given MultiSet implementation that permit MultiSet::size to execute in O(1) time. You must describe and write code for every change needed so that MultiSet::size runs in O(1) time. None of the other methods should have their running time change. You can write the code on the previous page, or describe the changes in detail below (hint: add something to the private section that will engender changes in the class). Part E(3 points) If the MultiSet constructor creates a BSTMap instead of an HMap, will the implementations of the MultiSet member functions change? Why? 8

Part F (8 pts) The code below creates a map for processing web-logs: files that store information about who visits web pages. The input file is in the format shown where the first string on a line is an Internet Protocal (IP) address and the second string is a URL for a web page visited by the corresponding IP address. 32.101.160.49 http://www.cs.duke.edu/~ola 32.101.160.49 http://www.cs.duke.edu/~ola/ethan 172.157.127.206 http://www.cs.duke.edu/education/courses/cps100/spring02 172.157.127.206 http://www.cs.duke.edu/education/courses/cps130/fall98 216.35.116.42 http://www.cs.duke.edu/~stefann 32.101.160.49 http://www.cs.duke.edu/~magda 32.101.160.49 http://www.cs.duke.edu/~ola Here's the code for reading/storing log information from a file. tmap<string, MultiSet *> * map = new HMap<MultiSet *>(10001); ifstream input("logfile.txt"); while (input >> ip >> url) if (map->contains(ip)) map->get(ip)->insert(url); else MultiSet * ss = new MultiSet(); ss->insert(url); map->insert(ip,ss); Write the function maxuniquevisits that prints the IP address that visits the most different web pages. If there is more than one such IP address your code can print any one of them. In the sample web log above the IP 32.101.160.49 visits 3 different web pages which is moe than the number of pages visited by other IP addresses, so 32.101.160.49 should be printed. void maxuniquevisits(tmap<string, Multiset *> * map) // pre: map stores information about IP -> web-page visits // post: IP address that visits the most different web pages printed 9

PROBLEM 6 : (Encore une fois (10 points)) A map of strings to ints associates an 8-bit/chunk (the value) with its Huffman encoding (the key). For example, "10" could be mapped to 's', and "11" mapped to 't' as shown in three below. d s t Complete the body of the function map2tree below that returns a pointer to the root of the Huffman tree representing the encodings in the map parameter. You will most likely find it useful to write a helper function called by map2tree, there's a blank page at the end of this test. You don't have to use the call of the helper function shown, but it's a good start. However, you're welcome to change completely the body of map2tree. For example, given the mappings below, the tree above should be returned. "0" 'd' "10" 's' "11" 't' struct TreeNode int info; TreeNode * left; TreeNode * right; TreeNode(int value, TreeNode * lptr, TreeNode * rptr) : info(value), left(lptr), right(rptr) ; void maptreehelper(treenode * t, const string& path, int value); // helper function prototype TreeNode * map2tree(tmap<string,int> * map) // pre: map represents string->int encodings for a Huffman tree // map contains at least 2 (string,int) pairs // post: returns pointer to root of Huffman tree representing the encodings Iterator<pair<string,int> > * it = map->makeiterator(); for(it->init(); it->hasmore(); it->next()) maptreehelper(,, ); 10

blank page 11