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

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

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

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

CS 3114 Data Structures and Algorithms READ THIS NOW!

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

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

CS 3114 Data Structures and Algorithms READ THIS NOW!

Do not start the test until instructed to do so!

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

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

Solution READ THIS NOW! CS 3114 Data Structures and Algorithms

CS 2505 Computer Organization I

Chapter 20: Binary Trees

Do not start the test until instructed to do so!

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

CS 2504 Intro Computer Organization Test 1

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CSE 247 Data Structures and Algorithms Fall Exam I

INF2220: algorithms and data structures Series 1

CS 2506 Computer Organization II Test 2

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

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

Data Structures and Algorithms

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

Midterm solutions. n f 3 (n) = 3

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

Do not start the test until instructed to do so!

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

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Do not start the test until instructed to do so!

CS 2505 Computer Organization I

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

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

Programming II (CS300)

CSL 201 Data Structures Mid-Semester Exam minutes

ECE250: Algorithms and Data Structures Midterm Review

INF2220: algorithms and data structures Series 1

Multiple choice questions. Answer on Scantron Form. 4 points each (100 points) Which is NOT a reasonable conclusion to this sentence:

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

CSE 373 Spring Midterm. Friday April 21st

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.

CSE 131S Introduction to Computer Science Summer SON Exam I

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

Largest Online Community of VU Students

Code No: R Set No. 1

CSE 143 Final Part 1, August 18, 2011 Sample Solution

Do not start the test until instructed to do so!

Basic Data Structures (Version 7) Name:

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

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

Problem Score Max Score 1 Syntax directed translation & type

CS 2506 Computer Organization II Test 2. Do not start the test until instructed to do so! printed

a graph is a data structure made up of nodes in graph theory the links are normally called edges

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

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

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

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

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Data Structures Question Bank Multiple Choice

Second Examination Solution

CS 2506 Computer Organization II Test 1

Summer Final Exam Review Session August 5, 2009

CMSC 341 Lecture 10 Binary Search Trees

Binary Trees and Binary Search Trees

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

CS 3114 Data Structures and Algorithms DRAFT Project 2: BST Generic

UNIVERSITY REGULATIONS

CS 2506 Computer Organization II Test 2. Do not start the test until instructed to do so! printed

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

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

Do not start the test until instructed to do so!

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

(the bubble footer is automatically inserted into this space)

Binary Search Trees Treesort

MIDTERM EXAM (HONORS SECTION)

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CSE 131 Introduction to Computer Science Fall Exam II

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

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

IX. Binary Trees (Chapter 10)

KEY printed. Do not start the test until instructed to do so! CS 2704 Object-Oriented Software Design and Construction Test 1.

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

Prelim 2, CS2110. SOLUTION

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

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

CS 216 Exam 1 Fall SOLUTION

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

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

Do not start the test until instructed to do so!

Computer Science Foundation Exam

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.

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

Motivation for Templates

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

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

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

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

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, aside from the permitted one-page formula sheet. 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 10 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 either failing to return this test, or discussing 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 Form B 1

1. [6 points] Complete the statement of the theorem below: Theorem Suppose that f and g are functions with domain [1, ) and that: f ( n) lim n g( n) = C Then: (a) if 0 < C < then f(n) is Θ( g(n) ). (b) if C = 0 then f(n) is Ο( g(n) ). (c) if C = then f(n) is Ω( g(n) ). 2. [8 points] Use the theorem you completed above to either prove the statement below is true, or that it is false. (Be sure to state your conclusion.) f 4 ( ) 2 2 ( n) = n + 3n log n is Ω n First we need to find the relevant limit: 3 2 2 2 2 lim n + 3n log n n 3n log n 1 3log n = lim = lim = 0 + lim ln 3 4 + 4 4 + n 2 n n n n n n n n 2 n 2n = 0 So, from the theorem, the statement is false. Form B 2

2. [10 points] A programmer must choose a data structure to store N elements, which will be supplied to the program in random order. Give a big-θ estimate for the number of operations required to create the structure if the programmer uses the following data structure and otherwise making intelligent decisions regarding efficiency: a) a doubly-linked list, inserting the N elements, in the order supplied, as they are supplied. Inserting each element, in the order supplied, is Θ(1), since they can simply be appended. Since there are N elements, the total cost is Θ(N). b) a sorted array, inserting the N elements as they are supplied and then sorting the array. Again, inserting each element would be Θ(1), and so inserting all N of them would be Θ(N). Sorting the array, efficiently, would be Θ(N log N), so the total cost is Θ(N log N). 3. [12 points] Assuming that each assignment, arithmetic operation, comparison, and array index costs one unit of time, analyze the complexity of the body of the following code fragment that computes an approximation of the number π, and give a simplified exact count complexity function T(N): double Pi(unsigned int N) { double Approx, Term; Approx = 1.0; // 1 for (unsigned int It = 1; It <= N; It++) { // 1 before, 2 each pass, // 1 to exit Term = 1.0 /(2.0 * It + 1); // 4 each pass if ( It % 2 == 0 ) // 2* Term = -1.0 * Term; // 2, if done Approx = Approx + Term; // 2 return 4.0 * Approx; // 2 Given the statement analysis above, the total complexity is: N ( 2 + 4 + 2 + 2 + 2) + 1+ 2 = 5 + ( 12) = 5 + 12N T ( N) = 1+ 1+ It= 1 * Consider this: what would you say if the statement was "if ( foo )" where foo was a Boolean variable? You could argue that this should be 3 operations, in which case you should also argue that the for loop test costs 2 operations, not 1. I'd accept that analysis as well. N It= 1 Form B 3

4. [10 points] Referring to the binary tree shown below, write down the values in the order they would be visited if an enumeration was done using: a) a preorder traversal b) an inorder traversal 50 25 10 45 75 60 55 70 90 80 95 10 25 40 59 55 60 70 75 80 90 95 For each of the questions 5 and 6, start with the following BST: 50 25 75 10 45 60 90 5. [8 points] Draw the resulting BST if 40 is inserted. 55 70 80 95 50 25 75 10 45 60 90 40 55 70 80 95 6. [8 points] Draw the resulting BST if 50 is deleted. 55 25 75 10 45 60 90 70 80 95 Form B 4

For questions 7 and 8, assume the following template declarations for an implementation of a doubly-linked list: // DNodeT.h //... template <typename T> class DNodeT { public: T Element; DNodeT<T>* Prev; DNodeT<T>* Next; ;... // DListT.h //... template <typename T> class DListT { private: DNodeT<T>* Head, Tail; // pointers to first and last data nodes, if any DNodeT<T>* Fore, Aft; // pointers to leading and trailing sentinels public: iterator begin(); // return iterator to first data node (or end()) iterator end(); // one-past-end const_iterator begin() const; // return const_iterator objects similarly ;... const_iterator end() const; ~DListT(); // destroy all dynamic content of the list 7. [8 points] Write an implementation of the destructor for the DListT template. You may not call any other member functions of the template in your solution. template <typename T> DListT<T>::~DListT() { DNodeT<T>* Current = Head; while ( Current!= Aft ) { Head = Head->Next; delete Current; Current = Head; delete Fore; delete Aft; Form B 5

8. [10 points] Write an efficient implementation for the following new member function for the DListT template. Note that an efficient implementation will not allocate any new nodes or delete any existing ones.. // mtffind() searches the DListT object for the first occurrence of a data // object that is equal to the parameter Target. If no match is found, the list // is not modified and the iterator value end() is returned. If a matching // element is found, that element is moved to the head of the list (if necessary), // and an iterator to that element is returned. // // For example, if mtffind(17) is called on the list {35, 89, 23, 17, 45, 9, // the resulting list would be {17, 35, 89, 23, 45, 9 and an iterator to 17 // would be returned. // template <typename T> typename DListT<T>::iterator DListT<T>::mtfFind(const T& Target) { if ( Head == NULL ) return end(); DNodeT<T>* Current = Head; while ( Current!= Aft ) { if ( Target == Current->Element ) { if ( Current!= Head ) { Current->Prev->Next = Current->Next; // reset surrounding nodes Current->Next->Prev = Current->Prev; if ( Current == Tail ) Tail = Current->Prev; // reset Tail pointer if necessary Current->Next = Head; // attach node to old head node, Current->Prev = Fore; // and Fore sentinel Fore->Next = Current; // attach Fore sentinel to node Head->Prev = Current; // attach old head node to node Head = Current; // attach Head pointer to node return iterator( Current ); Current = Current->Next; return end(); Form B 6

For question 9, assume the following template declarations for an implementation of a binary tree: template <typename T> class BinNodeT { public: T Element; BinNodeT<T>* Left; BinNodeT<T>* Right; ; template <typename T> class BinTreeT { protected: BinNodeT<T>* Root; public: ; 9. [10 points] Write an implementation for the new BinTreeT member function below. Your implementation should not need to call any other template member functions, except for any helper member functions you may wish to write. // PostOrderRepMax makes a postorder traversal of the binary tree. When it // "visits" an internal node, it replaces the value stored there with the larger // of the of the values stored in the children of that node. Leaf nodes are // unchanged. template <typename T> void BinTreeT<T>::PostOrderRepMax(std::ostream& Out) const { RepMaxHelper( Root ); template <typename T> void BinTreeT<T>::RepMaxHelper(BinNodeT<T>* sroot) const { if ( sroot == NULL ) return; if ( sroot->left == NULL && sroot->right == NULL ) return; RepMaxHelper( sroot->left ); RepMaxHelper( sroot->right ); if ( sroot->left == NULL ) sroot->element = sroot->right->element; else if ( sroot->right == NULL ) sroot->element = sroot->left->element; else { if ( sroot->left->element >= sroot->right->element ) sroot->element = sroot->right->element; else sroot->element = sroot->left->element; Form B 7

10. [10 points] Let T be a binary tree, and number the levels of the tree starting at zero, so we would say the root node is in level 0. Prove: for all n 0, the maximum number of nodes T can have in level n is equal to 2 n. proof: Let T be a binary tree. Level 0 contains, at most, the root node, so the maximum number of nodes that can occur in level 0 would indeed be 1 (which is 2 0 ). Assume that for some integer k 0, the maximum number of nodes in level k is 2 k. Consider level k+1 of T. Each node in level k+1 must be the child of a node in level k. Each node in a binary tree can have no more than 2 children. Therefore, the maximum number of nodes in level k+1 is exactly twice the number of nodes in level k. Since the latter is 2 k, the maximum number of nodes T can have in level k+1 would be 2 2 k = 2 k+1. Form B 8