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

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

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

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

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

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

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

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 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Compsci 100. Owen Astrachan April 14, 2010

Test #1. Login: Initials: 2 PROBLEM 1 : (The Sorted Link (12 points)) Using the following definition of a linked list node, write a function that inse

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

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

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

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

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

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

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

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

CSE373 Fall 2013, Midterm Examination October 18, 2013

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

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

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

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

Computer Science E-22 Practice Final Exam

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

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

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Priority Queues. 04/10/03 Lecture 22 1

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

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

CMSC 341 Lecture 15 Leftist Heaps

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

cameron grace derek cameron

CMSC 341 Lecture 15 Leftist Heaps

Midterm solutions. n f 3 (n) = 3

CS 315 Data Structures mid-term 2

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

Largest Online Community of VU Students

CMSC 341 Leftist Heaps

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

Final Examination CSE 100 UCSD (Practice)

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

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

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

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

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

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

CS61B, Fall 2011 Final Examination (corrected) P. N. Hilfinger

COS 226 Algorithms and Data Structures Fall Midterm

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

CS61B, Fall 2007 Final Examination P. N. Hilfinger

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

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

Data Structures and Algorithms for Engineers

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

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

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

Computer Science Foundation Exam

COS 226 Algorithms and Data Structures Fall Midterm

Q1 Q2 Q3 Q4 Q5 Q6 Total

CSE373 Winter 2014, Midterm Examination January 29, 2014

CS61B Fall 2015 Guerrilla Section 3 Worksheet. 8 November 2015

CMSC 341 Lecture 10 Binary Search Trees

Section I B COMPUTER SCIENCE SOLUTION

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

Fall, 2015 Prof. Jungkeun Park

INF2220: algorithms and data structures Series 4

Computer Science Foundation Exam

(the bubble footer is automatically inserted into this space)

Exam Data structures DAT036/DAT037/DIT960

Final Exam Solutions PIC 10B, Spring 2016

1. AVL Trees (10 Points)

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

ITI Introduction to Computing II

CS61B, Fall 2015 Final Examination (with corrections) P. N. Hilfinger

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

Lecture 9: Balanced Binary Search Trees, Priority Queues, Heaps, Binary Trees for Compression, General Trees

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

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

Computer Science Foundation Exam

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

ITI Introduction to Computing II

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

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

CS171 Final Practice Exam

R16 SET - 1 '' ''' '' ''' Code No: R

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

CS 2604 Data Structures Midterm Summer 2000 U T. Do not start the test until instructed to do so!

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

Computer Science 136 Exam 2

Recall: Properties of B-Trees

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

COS 226 Midterm Exam, Spring 2009

Second Examination Solution

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Binary Search Trees Part Two

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

Computer Science Foundation Exam

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Transcription:

DUKE UNIVERSITY Department of Computer Science CPS 100 Fall 2003 J. Forbes Test #2 Name: Login: Honor code acknowledgment (signature) Name Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 Problem 6 Problem 7 Problem 8 Extra credit TOTAL: value 6 pts. 8 pts. 10 pts. 9 pts. 10 pts. 8 pts. 18 pts. 6 pts. 75 pts. grade This test has 8 questions on 12 pages. Be sure your test has them all. This test is worth 75 points. You have 75 minutes to complete it. That means you should spend no more than 1 minute per point. This is an open-book test. You may consult any books, notes, or other inanimate objects (other than computers) available to you. Your answers may refer to any program text supplied in lectures, assignments, or solutions. Please write your answers in the spaces provided in the test. Make sure to put your name, and login in the space provided below. Put your login clearly on each page of this test (worth 1 point extra credit) and on any additional sheets of paper you use for your answers. Don't panic. Just read all the questions carefully to begin with, and first try to answer those parts about which you feel most confident. Do not be alarmed if some of the answers are obvious.

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 rotation. A B G E F C

Test #2. Login: 3 PROBLEM 2 : (Heaps (10 points)) Below is a vector V in which we are storing a min-heap, using the representation discussed in class (i.e. obeying the heap order property and the heap shape property). The current number of elements is 6. 12 21 31 36 32 38 a. Show the vector corresponding to the heap that results if we insert 23. b. Instead of using a heap, we decide to use a AVL tree to to represent our priority queue. What will be the worst-case big-oh of insert and deletemin for the AVL tree priority queue? How do those complexities compare to the respective values for a heap? (slower, same, or faster)

Test #2. Login: 4 PROBLEM 3 : (David's work (10 points)) 1. Given the following Huffman tree: Write the Huffman encoding for the string SENT". 2. The "shape" of a Huffman coding tree gives information about how well the algorithm will be able to compress the input. What tree shape leads to significant compression in the output? What shape that leads to little or no compression? 3. What happens if you use Huffman encoding to compress a file, image.hf, that has already been compressed with Huffman coding? Will the resulting file, image.hf.hf be smaller than the original (image.hf), the same size, or larger? You should briefly justify your answer by discussing the properties of the new encoding. What will the new Huffman Tree look like?

Test #2. Login: 5 PROBLEM 4 : (Sort it out (9 points)) Upon graduation, you are rewarded with a job applying sorting algorithms to various data sets. You remember from CPS 100 that QuickSort works very well and you use that most of the time. However, you also have a few other sorting algorithms that you can use if necessary.in what cases if any would the sorting algorithms below be more appropriate than QuickSort. Briefly justify your answers. a. Insertion Sort b. Merge Sort c. Radix Sort PROBLEM 5 : (Relations (10 points)) For each of the following two code excerpts, express the recurrence relation and the resulting big-oh. a. doubletree where n is the number of nodes in the tree rooted at node (assume average case) void doubletree(treenode* node) // pre: node points to a BST // post: for each node in the BST, creates a new duplicate node // and inserts the duplicate as the left child of the // original node. The result tree is still a BST TreeNode* oldleft; if (node==0) return; // do the subtrees doubletree(node->left); doubletree(node->right); // duplicate this node to its left oldleft = node->left; node->left = new TreeNode(node->info, 0, 0); node->left->left = oldleft;

Test #2. Login: 6 b. isbst3 where n is the number of nodes in the tree (assume worst case) bool ValsLess(TreeNode *t, string key) // post: returns true iff all nodes in t are less than key if (t == 0) return true; return t->info < key && ValsLess(t->left, key) && ValsLess(t->right, key); bool ValsGreater(TreeNode *t, string key) // post: returns true iff all nodes in t are less than key if (t == 0) return true; return t->info < key && ValsGreater(t->left, key) && ValsGreater(t->right, key); bool IsBST3(Tree * t) // postcondition: returns true if t represents a binary search // tree containing no duplicate values; // otherwise, returns false. if (t == NULL) return true; // empty tree is a search tree return ValsLess(t->left,t->info) && ValsGreater(t->right,t->info) && IsBST3(t->left) && IsBST3(t->right);

Test #2. Login: 7 PROBLEM 6 : (Searching, searching (8 points)) Given the following definition of a graph: Graph g; g.addvertex("0"); g.addvertex("1"); g.addvertex("2"); g.addvertex("3"); g.addvertex("4"); g.addvertex("5"); g.addvertex("6"); g.addundirectededge("0", "2"); g.addundirectededge("0", "5"); g.addundirectededge("1", "2"); g.addundirectededge("3", "4"); g.addundirectededge("3", "5"); g.addundirectededge("4", "5"); g.addundirectededge("2", "4"); a. Give the order that nodes are visited when performing a depth-first search on the above graph starting at vertex 0. The node order will not be unique. Drawing the graph may be useful but is not required. b. Give the order that nodes are visited when performing a breadth-first search on the above graph starting at vertex 0. Once again, the order will not necessarily be unique.

Test #2. Login: 8 PROBLEM 7 : (Trie again (18 points)) Given the following definition of Trie: const int ALPH_SIZE = 129; // # "real" chars /** * standard trie node, links * to all children, initialized to NULL/0 */ struct Trie bool isword; // true iff node ends a word tvector<trie *> links; // links to child nodes Trie() : isword(false), links(alph_size,0) ; a. Write a function to add a word to a trie. void AddWord(Trie * & t, string word) // post: word added to Trie t

Test #2. Login: 9 b. In this problem, you will write a function TrieIntersect that returns a trie that is the intersection of two tries. You may assume a function CopyTrie is already written. Its prototype is below: Trie * CopyTrie(Trie * root); // post: returns copy of trie pointed to by root You might want to model your function after the following definition of TrieUnion: Trie * TrieUnion(Trie * lhs, Trie * rhs) // postconditon: returns a trie that is the union of lhs and rhs // i.e., every word in lhs or in rhs is in returned trie Trie temp; Trie * ptr = 0; int k; bool somethingbelow = false; if (lhs == 0) return CopyTrie(rhs); else if (rhs == 0) return CopyTrie(lhs); else // make recursive calls for(k=0; k < ALPH_SIZE; k++) if (temp.index[k] = TrieUnion(lhs->index[k],rhs->index[k])) somethingbelow = true; // something found temp.isword = lhs->isword sec->isword; // in union? if (somethingbelow temp.isword) // something to return ptr = new Trie; // make new node *ptr = temp; // copy temp to ptr return ptr; Steps in writing TrieIntersect (a) Make the recursive calls to see what is in the intersection below (b) Examine isword fields to determine if the current node/path is in the intersection (c) Return an empty trie unless there is something below or the current node/path is in the intersection

Test #2. Login: 10 Trie* TrieIntersect(Trie * a, Trie * b) // postconditon: returns a trie that is the intersection of a and b // i.e., every word in a AND in b is in returned trie

Test #2. Login: 11 PROBLEM 8 : (Rankings (6 points)) You are givenavector of n ints (where n is very large) and are asked to find the largest m of them (where m is much less than n). a. Design an efficient algorithm to do this. You can just write the algorithm in English, you do not have to use C++ code. However, your language should be precise. For example, Put them all in a map and take out the top m" is not a good answer. You would need to specify what kind of map and exactly how you would obtain the top m. Assume that you have all available data structures described in class. Your algorithm should work well for all values of m and n, from very small to very large. b. What is the running time of your algorithm? What is it for small m? What is it as m! n? Extra Credit Name a member of the Duke programming team that finished second in the ACM Mid-Atlantic USA Programming Contest on November 8, 2003 and qualified for the world finals in Prague. Hint: one of them is a UTA for this course.

Test #2. Login: 12 Definitions 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 ) // Tree definition struct TreeNode string info; TreeNode * left; TreeNode * right; TreeNode (int val, TreeNode * lt, TreeNode * rt) : info(val), left(lt), right(rt) ; // Graph definition class Graph public: Graph(); ~Graph(); int addvertex(const string& s); void addedge(const string& from, const string& to); void addedge(const string& from, const string& to, double weight); void addundirectededge(const string& from, const string& to); void addundirectededge(const string& from, const string& to, double weight); void removeedge(const string& from, const string& to); int getnum(const string& s) const; string getname(int index) const; int vertexsize() const; int edgesize() const; double getweight(const string& from, const string& to) const; double getweight(int from, int to) const; bool hasedge(const string& from, const string& to) const; void getvertices(tvector<string>& list) const; void getadjacent(const string& vertex, tvector<string>& list) const; void clear(); private: //... ;