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

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

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

CS 3114 Data Structures and Algorithms Test 1 READ THIS NOW!

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

Solution printed. Do not start the test until instructed to do so! CS 2504 Intro Computer Organization Test 2 Spring 2006.

CS 3114 Data Structures and Algorithms READ THIS NOW!

Midterm solutions. n f 3 (n) = 3

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

Basic Data Structures (Version 7) Name:

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

CS 3114 Data Structures and Algorithms READ THIS NOW!

Solution READ THIS NOW! CS 3114 Data Structures and Algorithms

Final Exam in Algorithms and Data Structures 1 (1DL210)

CSE373 Fall 2013, Midterm Examination October 18, 2013

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

Lower Bound on Comparison-based Sorting

CmpSci 187: Programming with Data Structures Spring 2015

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Question 7.11 Show how heapsort processes the input:

Do not start the test until instructed to do so!

CSE 247 Data Structures and Algorithms Fall Exam I

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Final Exam in Algorithms and Data Structures 1 (1DL210)

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key and satisfies the conditions:

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.

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

Quiz 1 Practice Problems

CS 2506 Computer Organization II Test 2

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

COMP Data Structures

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

Computer Science 302 Spring 2007 Practice Final Examination: Part I

CS24 Week 8 Lecture 1

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

CS 2505 Computer Organization I

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

Data Structures Brett Bernstein

4.4 Algorithm Design Technique: Randomization

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

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

Name: UTLN: CS login: Comp 15 Data Structures Midterm 2018 Summer

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

Dr. Amotz Bar-Noy s Compendium of Algorithms Problems. Problems, Hints, and Solutions

Prelim 2. CS 2110, April 26, 2016, 5:30 PM Total Question True/False Complexity Heaps Trees Graphs Max Score Grader

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

Data Structures and Algorithms Chapter 4

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

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

Do not start the test until instructed to do so!

Sample Questions for Midterm Exam 2

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

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! printed

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

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

ECE242 Data Structures and Algorithms Fall 2008

Questions Total Points Score

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

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

Data Structures and Algorithms Week 4

ECE368 Exam 2 Spring 2016

Spring 2016 Algorithms Midterm Exam (Show your work to get full credit!)

Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file.

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

EECS Sample Midterm Exam

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

Sorting and Selection

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

ECE250: Algorithms and Data Structures Midterm Review

CPSC 320 Midterm 2 Thursday March 13th, 2014

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

Examination Questions Midterm 2

Data Structures Question Bank Multiple Choice

Computer Science E-22 Practice Final Exam

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

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

1 Tree Sort LECTURE 4. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

Binary Trees and Binary Search Trees

COS 226 Algorithms and Data Structures Fall Midterm

Multiple-choice (35 pt.)

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

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

Do not start the test until instructed to do so!

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

Binary Search Trees Treesort

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

Next. 1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

CMPSCI 187: Programming With Data Structures. Lecture #34: Efficient Sorting Algorithms David Mix Barrington 3 December 2012

Largest Online Community of VU Students

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

CSCI Trees. Mark Redekopp David Kempe

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

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

Do not start the test until instructed to do so!

CISC 621 Algorithms, Midterm exam March 24, 2016

Technical University of Denmark

Transcription:

VIRG INIA POLYTECHNIC INSTITUTE AND STATE U T PROSI M UNI VERSI TY Instructions: Print your name in the space provided below. This examination is closed book and closed notes. No calculators or other computing devices may be used. Answer each question in the space provided. If you need to continue an answer onto the back of a page, clearly indicate that and label the continuation with the question number. If you want partial credit, justify your answers, even when justification is not explicitly required. There are 11 questions, priced as marked. The maximum score is 100. When you have completed the test, sign the pledge at the bottom of this page and turn in the test. Note that failure to return this test, or to discuss its content with a student who has not taken it, is a violation of the Honor Code. Do not start the test until instructed to do so! Name Solution printed Pledge: On my honor, I have neither given nor received unauthorized aid on this examination. signed Page 1 of 6

1. [10 points] For each of the following sorting algorithms, give big-theta notation for the average and worst-case running times (swaps plus compares) on input of size N: Algorithm Average Worst InsertionSort n^2 n^2 QuickSort n log n n^2 HeapSort n log n n log n 2. [5 points] The implementation of QuickSort in Kruse/Ryba selects the pivot value from the middle of the sublist, rather than from one of the ends. Explain a reasonable motivation for that decision. The worst case for QuickSort is achieved if each pivot is either the maximum or minimum value in the current sublist. If the list is already sorted, then choosing either the first or last element guarantees the worst case is achieved. Choosing from the middle is a hedge against that. 3. [5 points] Aside from switching to a nonrecursive implementation, describe one modification of the implementation of QuickSort given in the course notes that will improve its performance. When sublists become relatively short, say length 10, apply an efficient alternative, such as an insertion sort to complete the sorting of that sublist. This eliminates many recursive calls. Choose the pivot value more carefully. For example, find three distinct values in the current sublist and take their median value. If there are not three distinct values, then the sublist is nearly sorted and can be efficiently finished off using another sort, such as insertion. 4. [10 points] Indicate whether each of the following statements is true or false: a) f(n) = 5n 2 + 3n + 2 is Θ (n) TRUE FALSE b) f(n) = 5n 2 + 3n + 2 is Ω(n) TRUE FALSE c) g(n) = 3n + 100 log n is O(n 2 ) TRUE FALSE d) g(n) = 3n + 100 log n is Ω(n) TRUE FALSE e) g(n) = 3n + 100 log n is O(log n) TRUE FALSE Page 2 of 6

5. You must keep track of some data. Your options are: 1) a binary search tree of records (assume it is well balanced) 2) a linked-list of records stored in order of insertion 3) an array-based list of records maintained in sorted order a) [12 points] Suppose that you must first build a data structure holding 2 10 given elements, and then you must perform 2 20 searches on that data structure. For each option, use the average case big-θ time complexity results of each data structure to determine the costs associated with that data structure in this situation. Since you know exactly how many elements are stored, and how many searches must be performed, the cost values should be numbers, not expressed in terms of N. Note that space cost is not a consideration. Option Cost of construction Cost of 2 20 searches Total Cost BST 10x2 10 10x2 20 unsorted linked list 2 10 2 29 sorted array 2 20 or 11x2 10 10x2 20 BST: Building costs n log n, which would be 2 10 log 2 10 = 10x2 10. Assuming a balanced BST, each search costs on average log n, which would be log 2 10 = 10, so the total cost of 2 20 searches would be 10x2 20. LL: Building costs n or 2 10, since each insertion has cost 1. Search, on average costs n/2 or 2 9, and so the total cost of 2 20 searches would be 2 29. SA: Building cost depends on the method you assume. Inserting the elements one by one in sorted order would have cost n 2 or 2 20. Inserting the elements into an array and then sorting efficiently would cost n + n log n, which would be 2 10 + 2 10 log 2 10 or 10x2 10 + 2 10 = 11x2 10. Each search would have cost log n, or log 2 10 = 10. So the total cost of all the searches would be 10x2 20. b) [3 points] Based on your analysis above, which of the data structures would be the worst choice in the given situation? The linked list has the worst overall cost. Page 3 of 6

6. [10 points] Use limits to prove that f(n) = n log n is NOT Θ(n 2 ). By a theorem, f(n) is Θ(g(n)) if and only if f ( n) limit n g( n) = c where c is a positive, finite constant. nlog n log n 1/( nln 2) 1 Now, limit = limit = limit = limit = 0 n 2 n n n n 1 n n ln 2 so by the cited theorem, n log n is NOT Θ(n^2). 7. [10 points] Suppose that T is a full binary tree. The Full Binary Tree Theorem says that an FBT with k internal nodes has k+1 leaf nodes. Applying that fact: a) If T has 100 internal nodes then the number of leaves in T is 101. b) If T has a total of 1001 nodes, then 500 of them must have children. 8. [10 points] Draw the BST that results from inserting the following data values in the order given: 42 17 89 53 72 91 3 88 42 17 89 3 53 91 72 88 Page 4 of 6

9. [5 points] Consider the heap given below. If the heap elements were stored in an array, as usual, what element would be stored at the index 7? 0 93 1 91 2 84 3 4 5 6 67 79 83 42 7 56 61 73 21 80 10. [10 points] Consider the BST and NodeT template interfaces given at the bottom of this page. Write the body of the member function Find(), which returns true if its parameter occurs in the BST and false otherwise. template <class Data> bool BST<Data>::Find(const Data& tofind) { return FindHelper(Root, tofind); template <class Data> bool BST<Data>::FindHelper(NodeT<Data>* sroot, const Data& tofind) { if (sroot == NULL) return false; Data currentdata = sroot->getdata(); if (currentdata == tofind) return true; if (tofind < currentdata) return FindHelper(sRoot->getLeft(), tofind); else return FindHelper(sRoot->getRight(), tofind); template <class Data> class NodeT { private: Data Element; NodeT<Data>* Left; NodeT<Data>* Right; public: // irrelevant members omitted Data getdata() const; NodeT<Data>* getleft() const; NodeT<Data>* getright() const; ; template <class Data> class BSTreeT { private: NodeT<Data>* Root; // irrelevant members omitted public: // irrelevant members omitted bool Find(const Data& D); ; Page 5 of 6

11. Consider the BinaryTreeT template interface given at the bottom of this page. template <class Data> void BinaryTreeT<Data>::ClearHelper(NodeT<Data>* sroot) { if (sroot == NULL) return; ClearHelper(sRoot->getLeft()); delete sroot; ClearHelper(sRoot->getRight()); template <class Data> BinaryTreeT<Data>::~BinaryTreeT() { ClearHelper(Root); a) [2 points] What type of traversal does ClearHelper() perform on the tree? inorder b) [8 points] Will the given destructor correctly delete all of the tree nodes? No. If not, write any modifications that are necessary to fix the problem below: template <class Data> void BinaryTreeT<Data>::ClearHelper(NodeT<Data>* sroot) { if (sroot == NULL) return; ClearHelper(sRoot->getLeft()); ClearHelper(sRoot->getRight()); delete sroot; template <class Data> class BinaryTreeT { protected: NodeT<Data>* Root; NodeT<Data>* Current; //... irrelevant protected member functions omitted void ClearHelper(NodeT<Data>* sroot); public: BinaryTreeT(); BinaryTreeT(Data newdata); //... irrelevant public member functions omitted ; ~BinaryTreeT(); Page 6 of 6