CS106X Handout 39 Autumn 2012 November 28 th, 2012 CS106X Midterm Examination II

Similar documents
CS106X Handout 25 Autumn 2012 October 23 rd, 2012 CS106X Midterm Examination

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

CS106X Handout 24 Winter 2018 February 9 th, 2018 CS106X Midterm Examination

CS106X Handout 17 Winter 2015 January 28 th, 2015 CS106X Practice Exam

Winter 2018 March 12 th, 2018 CS106X Midterm Examination Solution

PRACTICE MIDTERM EXAM #2

Autumn 2012 November 28 th, 2012 CS106X Midterm Examination II Solution

PRACTICE FINAL EXAM 2

PRACTICE FINAL EXAM 3

PRACTICE FINAL EXAM #4 (DRESS REHEARSAL)

PRACTICE MIDTERM EXAM #1

CS106X Handout 35 Autumn 2012 November 12 th, 2012 Section Handout

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

CS301 - Data Structures Glossary By

CS106X Handout 26 Winter 2018 February 12 th, 2018 Section Handout

Lecture Notes on Tries

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

Lecture Notes on Tries

Lecture Notes on Tries

Autumn 2012 October 29 th, 2012 CS106X Midterm Examination Solution

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

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CSE100. Advanced Data Structures. Lecture 12. (Based on Paul Kube course materials)

CS 104 (Spring 2014) Final Exam 05/09/2014

CS-301 Data Structure. Tariq Hanif

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

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

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Priority Queues and Huffman Trees

Practice Final Examination #2

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

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

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

You must include this cover sheet. Either type up the assignment using theory5.tex, or print out this PDF.

CS107 Handout 26 Spring 2007 May 9, 2007 CS107 Midterm Exam

CS106X Handout 23 Winter 2018 February 7 th, 2018 Assignment 5: Priority Queue

Programming Abstractions

Section Handout #4: Classes, Pointers, and Dynamic Memory

CSE030 Fall 2012 Final Exam Friday, December 14, PM

CS102 Binary Search Trees

CS106B Handout 26 Autumn 2012 October 29 th, 2012 Section Handout

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

CS106X Handout 27 Winter 2015 February 18 th, 2015 Section Handout. list after call braid(list) 3 3 à 3

Largest Online Community of VU Students

CSE100. Advanced Data Structures. Lecture 13. (Based on Paul Kube course materials)

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

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

You must print this PDF and write your answers neatly by hand. You should hand in the assignment before recitation begins.

Final Examination CSE 100 UCSD (Practice)

Exam Principles of Imperative Computation, Summer 2011 William Lovas. June 24, 2011

cameron grace derek cameron

COS 226 Algorithms and Data Structures Fall Midterm

Lecture 15 Binary Search Trees

CSCI-1200 Data Structures Fall 2018 Lecture 22 Hash Tables, part 2 & Priority Queues, part 1

Lecture 15 Notes Binary Search Trees

Computer Science Foundation Exam

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

Cpt S 122 Data Structures. Data Structures

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

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

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

EXAMINATIONS 2016 TRIMESTER 2

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

CMSC 341 Lecture 10 Binary Search Trees

The questions will be short answer, similar to the problems you have done on the homework

COMP 250 Midterm #2 March 11 th 2013

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

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

You have this page and the next page to present your solution.

Name CPTR246 Spring '17 (100 total points) Exam 3

Advanced Programming Handout 6. Purely Functional Data Structures: A Case Study in Functional Programming

UNIVERSITY REGULATIONS

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

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

MCS 360 Exam 2 11 November In order to get full credit, you need to show your work.

Chapter 20: Binary Trees

Draw a diagram of an empty circular queue and describe it to the reader.

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

Tutorial Letter 102/1/2012 Programming: Data Structures

Computer Science Foundation Exam

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

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

Practice Final Examination #2

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

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

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

An Introduction to Trees

CS302 Data Structures using C++

CS107 Handout 43 Spring 2007 June 8 th, 2007 CS107 Final Exam

CS171 Midterm Exam. October 29, Name:

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

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

CS 2150 Final Exam, Spring 2018 Page 1 of 10 UVa userid:

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

Binary Search Trees Treesort

Transcription:

CS106X Handout 39 Autumn 2012 November 28 th, 2012 CS106X Midterm Examination II This is closed book, closed notes, closed reader, closed everything exam. If you re taking the exam remotely, you can telephone Jerry at 415-205-2242 while taking the exam to ask questions. Good luck! Section Leader: Last Name: First Name: I accept the letter and spirit of the honor code. I promise to write more neatly than I ever have before in my entire life. (signed) Score Grader 1. Equivalence Classes [10] 2. New Jersey Counties [10] 3. Linked Lists and Sets [10] 4. Tries and Removing Words [8] 5. Patricia Tree Traversal [7] 6. Short Answer [5] Total [50]

Summary of Relevant Data Types class string { bool empty() const; int size() const; int find(char ch) const; int find(char ch, int start) const; string substr(int start) const; string substr(int start, int length) const; char& operator[](int index); const char& operator[](int index) const; 2 class Vector { bool isempty() const; int size() const; void add(const Type& elem); // operator+= used similarly void insert(int pos, const Type& elem); void remove(int pos); Type& operator[](int pos); class Grid { int numrows() const; int numcols() const; bool inbounds(int row, int col) const; Type get(int row, int col) const; // cascade of operator[] also works void set(int row, int col, const Type& elem); class Stack { bool isempty() const; void push(const Type& elem); Type pop(); class Queue { bool isempty() const; void enqueue(const Type& elem); Type dequeue(); class Map { bool isempty() const; int size() const; void put(const Key& key, const Value& value); void remove(const Key& key); bool containskey(const Key& key) const; Value get(const Key& key) const; Value& operator[](const Key& key); class Set { bool isempty() const; int size() const; void add(const Type& elem); // operator+= also adds elements bool contains(const Type& elem) const;

Problem 1: Equivalence Classes [10 points] Two positive integers m and n are said to be modulo-d equivalent if and only if m % d == n % d that is, both m and n produce the same remainder when divided by d. When two integers are modulo-d equivalent, we say they fall into the same equivalence class. Partitioning an array of positive integers into its modulo-d equivalence classes amounts to the creation of d new arrays and distributing the integers across them. Those integers modulo-d equivalent to 0 are placed in the 0 th array, those modulo-d equivalent to 1 are placed in the next array, and so forth. As an illustration, consider the following array of positive integers: 3 The three modulo-3 classes of this array could be represented by this array of three arrays: Note the all integers of the original array are represented exactly once, and that the perfect multiples of 3 were placed in the 0 th class, integers one more than a perfect multiple of 3 were placed in the next class, and those two more than a perfect multiple of three were placed in the final class. In general, an integer n is placed in the k th class whenever n % 3 equals k. The five modulo-5 classes of the original array would then be represented by this array of five arrays: Again, the generalization here is that an integer n is placed in the k th array whenever n % 5 equals k.

a. [4 points] First, implement the computeclasssizes function, which takes an array, the array's size, and an integer d, and computes all of the sizes of the modulo-d classes. The function should dynamically allocate space for an integer array of length d, and should then go on to write all of the class sizes to that array. The size of the modulo-0 class should be stored at index 0, the size of the modulo-1 class should be stored at index 1, and so forth. You may assume that all integer arguments are positive, so that no error checking is necessary. The base address of the dynamically allocated array should be placed at the address supplied through the last parameter. You should not use any objects or records at all, as the intent here to exercise your understanding of primitives and raw memory. Note that most of the points are dedicated to the memory directives and syntax operator new, &, *, ->, etc. that you ve learned in CS106X. static void computeclasssizes(const int array[], int size, int d, int **classsizes) { 4

b. [6 points] Now leverage your computeclasssizes function to implement the partitionintoclasses function, which distributes the integers of the specified array over the d classes. The array of classes is dynamically allocated, and each class is itself dynamically allocated to store the exact number of integers that fall into that class. Ultimately, two arrays needs to be returned, so the base address of each should be stored at the two locations specified via classes and classsizes. Again, the proper use of &, *, [], and so forth really matter here, so be very careful with them. Don t use any objects and records. static void partitionintoclasses(const int array[], int size, int d, int ***classes, int **classsizes) { 5

Problem 2: New Jersey Counties [10 points] Analyze the following code snippet, starting with a call to somerset, and draw the state of memory at the point indicated just before the bergen helper function returns. Be sure to differentiate between stack and heap memory, note values that have not been initialized, and identify if and where memory has been orphaned. Generate your final diagram on the next page, and feel free to tear this page out so you can easily refer to it. struct county { int atlantic; county *burlington[4]; county **hudson; static void somerset() { county salem[3]; salem[0].atlantic = 8; salem[1].burlington[0] = NULL; salem[1].burlington[1] = salem; salem[1].burlington[2] = &salem[1]; salem[1].burlington[3] = salem[1].burlington[1]; county **ocean = &(salem->burlington[1]); ocean = &ocean[1]; salem[1].hudson = ocean; ocean[1] = new county; ocean[1][0] = salem[1]; bergen(salem[1], ocean, ocean); } static void bergen(county& passaic, county **warren, county **& sussex) { warren = new county *; *warren = &passaic; passaic.hudson = warren; sussex = passaic.burlington; *&sussex = NULL; Draw the state of memory just before bergen returns. } 6

Problem 2: New Jersey County Pointer Trace [continued] 7

Problem 3: Linked Links and Disjoint Sets [10 points] Assume an unsorted set of distinct integers is stored in a linked list, where each node in the list stores not only the address of its successor, but also the address of the list s front node. The set itself tracks the addresses of both the head and tail nodes of the list, much like the Queue template container does. For example, the set {4, 7, 8, 1} would look like this: 8 set1 4 7 8 1 and the set {3, 9, 2} might look like this: set2 3 9 2 The data structures we ll use to support the above (we ll go with exposed structs and not worry about encapsulation or object orientation) are as follows: struct node { node *front; int value; node *next; struct set { node *head; node *tail; One nifty feature of our linked list approach is that set unioning is conceptually trivial particularly if we assume that the two sets being unioned are nonempty and have no elements in common. If, for instance, set1 and set2 as drawn above were unioned into a third set, that third set would look like this: set3 4 7 8 1 3 9 2

In particular, the tail node of the first set is updated to point to the head node of the second, and all front fields in the second set s list are updated to point to the front node of the first. No memory is allocated anywhere, as the nodes of the two original sets are donated to the third. a. [5 points] Implement the constructunion function, which accepts two sets by reference and returns their union (reusing existing nodes instead of allocating new ones). Assume the two incoming sets are nonempty and disjoint (e.g. no elements in common), and don t worry about cleaning up the two incoming sets in any way, as it s reasonable to assume that those two sets won t be used anymore, as their memory is being cannibalized and donated to a third one. static set constructunion(set& set1, set& set2) { 9

b. [5 points] Of course, one needs to be able to construct these sets in the first place. Implement the buildset function, which accepts the provided Vector<int> assumed to be nonempty and free of duplicates and constructs and returns the equivalent set as described above. Recall that the linked list backing the set need not be sorted. You ll need to dynamically allocate nodes one for each number in the referenced Vector<int> and wire everything up as described above. static set buildset(vector<int>& numbers) { 10

Problem 4: Tries and Removing Words [8 points] Recall that the primary, object-oriented data structure we used to build a trie is defined as: struct node { bool isword; Map<char, node *> suffixes; node() { isword = false; } ~node() { foreach (char ch in suffixes) delete suffixes[ch]; } 11 Implement the remove function, which removes the specified word from the trie, being careful to dispose of any nodes for prefixes that are no longer prefixes. To illustrate, let s inspect the sample trie I presented in lecture (presented on the right), which encodes five words: "be", "bed", "cab", "cage", and "caged". If the word "be" is removed, then a single true if changed to false and that s that. No nodes are deleted, because all nodes relevant to "be" are needed to encode "bed". If after removing "be" we remove "bed", then we need to remove quite a few nodes, because "b", "be", and "bed" are no longer prefixes of anything. Additionally, the 'b' entry in the root node s suffixes map would need to be removed as well. false e true d true false b c root true false a false b g false e Special care must also be taken to see if the last remaining word is removed, in which case all remaining nodes would need to be killed off and the root would need to be set to NULL. Oh, and if the word isn t actually present, then you should just return without modifying the trie. Using the next page, present your implementation of the remove function. Feel free to tear this page out so you can easily refer to it. true d true

Problem 4: Tries and Removing Words [continue] 12 static void remove(node *& root, const string& word) {

Problem 5: Patricia Tree Traversal [7 points] This problem leverages a data structure introduced in an earlier section handout, but the theory backing it is reproduced here in its entirety. Let s again consider the following illustration: 13 What s drawn above is an example of a Patricia tree similar to a trie in that each node represents some prefix in a set of words. The child pointers, however, are more elaborate, in that they not only identify the sub-tree of interest, but they carry the substring of characters that should contribute to the running prefix along the way. Sibling pointers aren t allowed to carry substrings that have common prefixes, because the tree could be restructured so that the common prefix is merged into its own connection. By imposing that constraint, that means there s at most one path that needs to be explored when searching for any given word. The children are lexicographically sorted, so that all strings can be easily reconstructed in alphabetical order. When a node contains a true, it means that the prefix it represents is also a word in the set of words being represented. [The root of the tree always represents the empty string.] So, the tree above stores the following words: cranium, crazy, go, golf, golfing, goober, peg, perky, petulance, pork, and pundit.

These two type definitions can be used to manage such a tree. 14 struct connection { string letters; struct node *subtree; // will never be NULL struct node { bool isword; Vector<connection> children; // empty if no children Implement the collectallwords function, which traverses the supplied tree as necessary and populates the referenced Vector assumed to be empty starting out so that by the end of execution it contains all of the tree s strings, sorted low to high. You should use recursion, and you ll want to implement this using a wrapper function. static void collectallwords(const node *root, Vector<string>& words) {

Problem 6: Short Answers [5 points] a. [1 point] Assume some permutation of the numbers 1 through 3 are inserted into an initially empty binary search tree, and further assume that the resulting tree is as balanced as possible, even though the binary tree doesn t use any rotation or self-balancing strategies. How many different permutations of the numbers 1 through 3 result in a perfectly balanced tree? 15 b. [1 point] What would the Huffman encoding tree for the string "bbbbaaacdcabbbb" look like? (Don t worry about the pseudo-eof, and understand that there are many correct encoding trees, not just one.) c. [1 point] Describe two key differences between a reference and a pointer (e.g. contrast int& to int*).

d. [1 point] Explain why the merge operation for binomial-heap-backed priority queues is so much faster than the merge operations for the unsorted-vector-, the sorted-doubly-linked-list-, and the binary-heap-backed priority queues. 16 e. [1 point] Briefly describe two ways to reduce the memory footprint of the trie without compromising the execution time of contains and containsprefix.