Section 1: True / False (2 points each, 30 pts total)

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

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

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

Solution to CSE 250 Final Exam

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

Largest Online Community of VU Students

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

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

CSE 373 Spring Midterm. Friday April 21st

CS 216 Exam 1 Fall SOLUTION

MIDTERM EXAMINATION Douglas Wilhelm Harder EIT 4018 x T09:30:00P1H20M Rooms: RCH-103 and RCH-302

Data Structure and Algorithm Midterm Reference Solution TA

Computer Science 302 Spring 2007 Practice Final Examination: Part I

CMSC 341 Lecture 10 Binary Search Trees

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

Second Examination Solution

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

CSE373 Fall 2013, Midterm Examination October 18, 2013

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

CS 315 Data Structures mid-term 2

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

UNIVERSITY REGULATIONS

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

Computer Science E-119 Fall Problem Set 4. Due prior to lecture on Wednesday, November 28

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

CSE 100: C++ TEMPLATES AND ITERATORS

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

Chapter 20: Binary Trees

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

INF2220: algorithms and data structures Series 1

CSE332 Summer 2010: Midterm Exam Sample Solutions

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

Section 05: Solutions

CSCE 2014 Final Exam Spring Version A

Binary Search Trees Part Two

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION B /13:30-14:50 MC-4021/RCH-211

Final Exam Solutions PIC 10B, Spring 2016

MIDTERM EXAM (HONORS SECTION)

CS102 Binary Search Trees

Largest Online Community of VU Students

Section 05: Solutions

CS 106X Sample Final Exam #2

Programming II (CS300)

Red-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood

CMSC 341. Binary Search Trees CMSC 341 BST

MLR Institute of Technology

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

Section 05: Midterm Review

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

Chapter 5. Binary Trees

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

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

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

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

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

Data Structure (CS301)

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

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

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

CSE030 Fall 2012 Final Exam Friday, December 14, PM

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

Dynamic Data Structures

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

Binary Search Trees Treesort

Data Structure and Algorithm, Spring 2013 Midterm Examination 120 points Time: 2:20pm-5:20pm (180 minutes), Tuesday, April 16, 2013

Student number: Datenstrukturen & Algorithmen page 1

CSE wi: Practice Midterm

Practice Midterm Exam Solutions

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

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

Computing Science 115 Final Examination April 23, 2002 Section: B2 BASU. Please put student id on last page. Instructions:

CS126 Final Exam Review

Final Exam. Name: Student ID: Section: Signature:

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

Computer Science Foundation Exam

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

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

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

Computer Science 302 Spring 2017 (Practice for) Final Examination, May 10, 2017

Prelim One Solution. CS211 Fall Name. NetID

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

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

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

TREES Lecture 10 CS2110 Spring2014

Questions: ECE551 PRACTICE Final

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

Chapter 13. Recursion. Copyright 2016 Pearson, Inc. All rights reserved.

COMP 250 Midterm #2 March 11 th 2013

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

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

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?

COSC 2011 Section N. Trees: Terminology and Basic Properties

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

CSE373 Winter 2014, Midterm Examination January 29, 2014

Largest Online Community of VU Students

CS/ENGRD2110: Final Exam

Help File on C++ and Programming at CSUSM by Rika Yoshii

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

Computer Science 50: Introduction to Computer Science I Harvard College Fall Quiz 0 Solutions. Answers other than the below may be possible.

Transcription:

Section 1: True / False (2 points each, 30 pts total) Circle the word TRUE or the word FALSE. If neither is circled, both are circled, or it impossible to tell which is circled, your answer will be considered wrong. 1. TRUE or FALSE Computer scientists always draw tree structures with the root at the top and leaves at the bottom because they don t get out enough to remember how trees grow in nature. 2. TRUE or FALSE In a makefile, the default target is the last one in the file. 3. TRUE or FALSE Local variables declared in a function are stored on the stack. 4. TRUE or FALSE The function n log n 2 is O(n 2 ) 5. TRUE or FALSE The function n log n 2 is O(n log n) 6. TRUE or FALSE The function n log n 2 is Ω(n log n) 7. TRUE or FALSE The function n log n 2 is Ω(n 2 ) 8. TRUE or FALSE The function 4n 2 + n 2 log 2n is O(n 2 ) 9. TRUE or FALSE The function 4n 2 + n 2 log 2n is O(n 3 ) 10. TRUE or FALSE If an algorithm consists of a program segment that is O(n 2 ), sequentially followed by a segment that is O(n), the entire algorithm is O(n 3 ) 11. TRUE or FALSE A function can be both O(n 2 ) and Ω(n 3 ) 12. TRUE or FALSE An in-order traversal of a binary search tree will process the smallest value first. 13. TRUE or FALSE In the function declaration foo(int arg1, int * & arg2), a pointer to arg2 is being passed by reference. 14. TRUE or FALSE Both individual functions and entire classes can be templated. 15. TRUE or FALSE For container classes that support iterators, the function end() return an iterator that points to the last element in the container.

Section 2: Short Answer (varying point values. 48 pts total) 16. (6 points) Show that the function 5n 3 + 3n + 4 is O(n 3 ) (your answer should include a specific n0 and c, as well as an algebraic justification that the limit holds for all n > n0; you do not need any inductive proof) For all n 1: 3n 3 3n, and 4n 3 4 5n 3 + 3n 3 + 4n 3 = 12n 3 5n 3 + 3n + 4 So, for c = 12 and n 0 = 1, 5n 3 + 3n + 4 is O(n 3 ) For questions 17 and 18, Give asymptotic worst-case running times for each of the following code fragments. Characterize the running time as closely as possible (e.g., don t say O(n3) if O(n 2 ) also works). Express running times as a function of n. Make sure you give a satisfactory explanation it is worth a significant fraction of the points. 17. (6 points) for (x = 0; x < n; x++) { for (y = x; y < n; y++) { sum += x * y; Running time: O( n 2 ) Justification: The inner loop runs 0 times, then n times, then n-1, n-2... down to 1. So, the number of times the body of the inner loop is executed is sum(1..n), which is O(n 2 ). Another way to think about it is the average number of inner turns is n/2, so total is (n/2)n, again O(n 2 ) 18. (6 points) sum = 0; for (a = 0; a < n; a++) { sum += a; prod = 1; for (a = sum; a > 0; a--) { prod *= a; Running time: O( n 2 ) Justification: The first loop executes n times, and the second loop executes sum(0..n-1)=n 2 times,so the complete code runs in O(n + n 2 ), which by the rule of sums, is is still O(n 2 ) 2

CMSC 341, Fall 2018 Midterm Student Name: 19. (2 points) What is the maximum height of a binary search tree with n nodes? max height = n 1: essentially a linked list. 20. ( 2 points) What is the minimum height of a binary search tree with n nodes? min height = log(n). 21. (3 points) What is the main difference between a plain binary tree and a binary search tree? The binary search tree (BST) is a binary tree with a special additional property: for any given node, all of the nodes in that node s left subtree have keys less than the node, and all of the nodes in the right subtree have keys that are greater Questions 22 thru 25 refer to the following binary tree: 1 4 3 7 2 8 5 22. (2 points) The height of this tree is: 3 23. (2 points) The depth of the node labeled 7 is: 2 24. (2 points) The height of the node labeled 3 is: 2 25. (4 points) Print out the order the nodes would be visited for a postorder traversal: _7, _4, _5, _2, _8, _3, _1, 3

26. (3 points) Differentiate the experimental versus analytical approaches to determining algorithmic complexity. experimental approach: implement the algorithm and run it, measuring time taken; analytical approach: examine the code and mathematically determine how many times each step is executed, at what cost Questions 27 thru 29 refer to the following binary search tree, with nodes in alphabetical order: M D S A L N V F P T H 27. (3 points) First insert into the above tree a node with the value F (you can draw on the diagram above) (See new blue node F above) 28. (3 points) Then insert into the tree a node with the value H (again, you can add to the diagram above) (See new red node H above) 29. (4 points) Draw the resulting tree when you delete the node S from the original tree (draw the complete new tree below): M M P T N V N V 4 T P

CMSC 341, Fall 2018 Midterm Student Name: Section 3: Design and Coding (22 points total) Note: for this section, you are being asked to write more substantial sections of code. Clarity of code structure and efficiency of design count. 30. (6 points) Write a code snippet with a for-loop that uses an iterator to print out all of the values stored in a STL list<int> object called listofints. Be sure to declare your iterator object correctly. list<int> listofints; // Assume there will be code here to fill listofints with numbers // Now, your code here to print out all the values in listofints; // You must use an iterator!!! list<int>::iterator it; for (it = listofints.begin(); it!= listofints.end(); it++) { cout << *it << endl; 5

31. (10 points) You are given the following C++ class definition for a singly-linked list node class (Node): class Node { public: int data; Node *next; Write the code body for the function swap_with_next(), which is called with two arguments: list, a pointer to the first Node of a singly-linked list; and data, a value to search for in the list. When the Node with the given value is found, it should then be swapped with the Node immediately following it. The next field of the last node in the list is NULL. To keep the solution simple, you can assume that the list is not empty, that the value you are searching for is in the list, and that it will not be the first element or last element in the list. void swap_with_next(node *list, int data) { // taking full advantage of assumption that list isn t empty, // and that we do not need to handle swap at head Node *prev, *curr, *next, *nextnext; for (prev = list, curr = prev->next; curr->next!= NULL; prev = curr, curr = curr->next) { if (curr->data == data) { next = curr->next; nextnext = next->next; prev->next = next; next->next = curr; curr->next = nextnext; return; 6

CMSC 341, Fall 2018 Midterm Student Name: 32. (6 points) You are given the following C++ class definition for TNode (short for tree node ), that is used to represent nodes in a binary search tree ( BST ): class TNode { public: int data; TNode *left, *right; Every TNode contains a distinct data value. Write the code body for the recursive function contains(), which is called with two arguments: root: a pointer to the root node of a tree or subtree, and data: the value to search for in the tree. The function returns true if the value was found in the tree/subtree, false otherwise. Note again that this is a proper BST. Also note that either/both the pointers root->left and root->right can be NULL. To keep the solution simple, you can assume that the tree initially passed in is not empty (i.e., main() promises not to call the initial invocation of contains()with NULL). As to whether contains() ever recursively calls itself with NULL, as many of our in-class examples did, is up to you. Don t forget the return statement at the end! bool contains(tnode *root, int data) { if (root == NULL) return false; else if (data < root->data) { return contains(root->left, data); else if (data > root->data) { return contains(root->right, data); else { return true; 7

33. Extra Credit (5 points) 34. Give asymptotic worst-case running times, as a function of n, for the following code. Characterize the running time as closely as possible: int sum = 0; for (i = 0; i < n; i++) { for (int j = i; j < n; j++) { for (int x = i; x < j; x++) { sum += x; Be careful this is trickier than it seems! That s why it s extra credit. Running time: O( n 3 ) Justification: The innermost loop runs j-i times,but since j ranges from i to n, for any given i, it runs 0 times, then 1, then 2.. n-i-1 times. So it runs a total of sum(0..n-i-1). Now, define i = n-i, then our terms become sum(0..i -1)where i ranges from 0..n-1. sum(0..i) is O(i 2 ), so our outer loop makes the whole function sum i:0..n(i 2 ), which is O(n 3 ) 8