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

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

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

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

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

cameron grace derek cameron

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

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 2: CPS 100. Owen Astrachan. April 3, Name: Login: (1 pt)

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

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

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

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

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

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

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

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

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

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

Prelim 2. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Chapter 20: Binary Trees

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

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

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

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

Prelim 2 SOLUTION. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

(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

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

Computer Science E-22 Practice Final Exam

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

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

Prelim 2, CS2110. SOLUTION

York University AK/ITEC INTRODUCTION TO DATA STRUCTURES. Final Sample II. Examiner: S. Chen Duration: Three hours

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

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

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

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

CPSC 331 Term Test #2 March 26, 2007

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

CS301 - Data Structures Glossary By

Compsci 100. Owen Astrachan April 14, 2010

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

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Course goals. exposure to another language. knowledge of specific data structures. impact of DS design & implementation on program performance

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

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

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

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

CSCE 2014 Final Exam Spring Version A

Recitation 9. Prelim Review

Q1 Q2 Q3 Q4 Q5 Q6 Total

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

A set of nodes (or vertices) with a single starting point

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

Summer Final Exam Review Session August 5, 2009

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

(the bubble footer is automatically inserted into this space)

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. AVL Trees (10 Points)

Second Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

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

Largest Online Community of VU Students

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

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

Course Review for Finals. Cpt S 223 Fall 2008

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

INF2220: algorithms and data structures Series 1

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

Computer Science Foundation Exam

CMSC 341 Lecture 10 Binary Search Trees

Review of the Lectures 21-26, 30-32

Programming II (CS300)

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

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

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

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

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

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

Programming II (CS300)

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

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

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

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

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY THIRD SEMESTER B.TECH DEGREE EXAMINATION, JULY 2017 CS205: DATA STRUCTURES (CS, IT)

CSE 2320 Section 002, Fall 2015 Exam 2 Time: 80 mins

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

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

CS171 Midterm Exam. October 29, Name:

UNIT III BALANCED SEARCH TREES AND INDEXING

Midterm solutions. n f 3 (n) = 3

Chapter 5. Binary Trees

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

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

Data Structure and Algorithm Homework #3 Due: 2:20pm, Tuesday, April 9, 2013 TA === Homework submission instructions ===

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

ECE242 Data Structures and Algorithms Fall 2008

Data Structures Question Bank Multiple Choice

CSL 201 Data Structures Mid-Semester Exam minutes

Transcription:

Test 2: CPS 100 Owen Astrachan April 3, 1996 Name: Honor code acknowledgement (signature) Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 EXTRA TOTAL: value 13 pts. 14 pts. 13 pts. 12 pts. 15 pts. 8 pts. 67 pts. grade This test has 8 pages, be sure your test has them all. Do NOT spend too much time on one question remember that this class lasts 50 minutes. Declarations node is made. For all problems on this test you can assume the following declaration for a templated tree template <class Kind> struct TNode // "standard binary tree declaration" Kind info; TNode * left; TNode * right; TNode (const Kind & val, TNode * lchild = 0, TNode * rchild = 0) : info(val), left(lchild), right(rchild) ; 1

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 ) sorts. Briey, why is the running time of bubble sort so much slower than selection sort, and why is this dierence more pronounced when strings are sorted rather than integers? 2. Linear-probing is one method for resolving collisions when hashing. Explain in one or two sentences one problem with using linear probing to resolve collisions. 3. A database of names will be stored in memory for a computer program. Names will be added occasionally, and deleted occasionally, but the names will need to be printed in alphabetical order very frequently. Why is a balanced binary search tree a better choice for storing the data than a hash table? 4. If n items are stored in an array, beginning at position 1 (the zero-th item isn't used) what is the complexity, using big-oh, of turning the n items into a binary-heap with the smallest element at index one. 5. If you do NOT use a makele, but run the g++ compiler without specifying a name for the resulting executable (e.g. g++ foo.cc), what will be the name of the executable le? 2

PROBLEM 2 : (Trees I 14 points) Part A Give the postorder traversal of the binary search tree below. p g u d q z b f t w v y Postorder: Part B Add nodes containing x, s, m and j, in that order, to the search tree shown above. Part C 6points If a node with two children in a binary search tree is deleted, it is replaced by its inorder successor, i.e., the node that comes next in an inorder traversal. For example, in the original tree above (before any values are added as in part B), node p (the root) would be replaced by q; and node u would be replaced by nodev. Deleting a node with no children is straightforward, the node is simply removed. Deleting a node with one child results in \pulling" up the child, e.g., to delete node g above just replace it by d. Write the function ReplacePtr below that returns a pointer to the node that will replace tree. ReplacePtr should return 0/NULL if tree has no children, a pointer to tree's only child if there is only one child, and a pointer to the inorder successor of tree if tree has two children. template <class Kind> TNode<Kind> * ReplacePtr(TNode<Kind> * tree) // postcondition: returns pointer to node that will replace node tree // if node tree is deleted 3

PROBLEM 3 : (gnitros Two 13 points) Part A 3points Is the following statement true or false, why (briey)? There are absolutely no sorts that have running times better than O(n log n) Part B Give one reason to prefer mergesort to quicksort and also one reason to prefer quicksort to mergesort (be brief). 4

Part C 6points The function DoMerge from sortall.cc is modied (as shown below) to divide a vector into three parts, recursively sort each of the three parts, then merge the parts back together. One line is missing from the function DoMerge. Add the one line to DoMerge. Write a recurrence relation for the version of mergesort described above and implemented in DoMerge. What is the solution of this recurrence relation, i.e., the big-oh complexity of the new version of mergesort? You do NOT need to solve the recurrence, a one sentence justication is sucient (although you may solve the recurrence). template <class Type> void Merge(Vector<Type> & a, int left,int mid,int right) // precondition: a sorted from a[left]... a[mid] and from // a[mid+1] to a[right] // postcondition: a sorted from a[left]... a[right] template <class Type> void DoMerge(Vector<Type> & a, int left,int right) // postcondition: a[left] <=... <= a[right] int onethird = (right - left)/3; // [left..first] is first interval int first = left + onethird; // [first..second] is second interval int second = right - onethird; // [second..right] is third interval if (left < right) DoMerge(a,left,first); DoMerge(a,first+1,second); DoMerge(a,second+1,right); Merge(a,left,first,second); // one call of Merge missing below 5

PROBLEM 4 : (Heap o' Trouble 12 points) The values stored in the array below form a min-heap. 4 12 7 20 37 38 27 26 32 0 1 2 3 4 5 6 7 8 9 10 11 Part A Draw the complete binary tree that corresponds to the heap in the array above. Part B If 39 is added to the heap, it will be stored at index 10, with no movement of items in the heap. However, if 8 is added to the heap, items will be moved. What two items will be moved if 8 is added to the heap? Part C If a DeleteMin operation is performed, the item at the root/index 1 will be removed and data will be moved. Draw either the tree or the array that results from a DeleteMin operation in the original heap from part A. 6

PROBLEM 5 : (HR HunStu 15 points) A le contains seven dierent characters (shown below) will be compressed using Human coding. trees are a treasure 't' 'r' 'e' 'a' 's' 'u' space One possible Human tree for the characters above is shown below. r e t a _ (space) u s Part A 6points Label each non-leaf node in the tree above with the priority/weight given to it as a result of building the Human tree. Part B 5points What bits are written to the compressed le as a result of compressing the word \trees"? Part C Using the same tree, what message is coded by the bit sequence below? 1 0 0 1 0 1 1 1 0 1 0 0 1 1 0 7

PROBLEM 6 : (Fruit Trees 8 points EXTRA) This problem is Extra Credit, it is NOT required The tree below is a binary search tree. lemon cherry papaya apple guava orange tangerine watermelon Write the function DoPrintPath that prints, for each leaf in tree, one line of output consisting of the values stored on the root-to-leaf path. For example, for the tree above the output is shown below. lemon cherry apple lemon cherry guava lemon papaya orange lemon papaya tangerine watermelon The function DoPrintPath is called by PrintPath as shown below. You may assume a boolean-valued function IsLeaf exists. Assume that there at most 200 strings stored in any tree. void DoPrintPath(TNode<string> * tree, Vector<string> & path, int depth) // precondition: depth = depth of tree (# edges from root, root has depth 0) // path stores values on root-to-leaf path // postcondition: for each leaf of tree, print values on root-to-leaf path // one line of output for each leaf void PrintPath(TNode<string> * tree) if (tree!= 0) Vector<string> path(200); path[0] = tree->info; DoPrintPath(tree,path,0); 8