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

Similar documents
CS126 Final Exam Review

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

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

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

CSE 332, Spring 2010, Midterm Examination 30 April 2010

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

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

CSE373 Fall 2013, Midterm Examination October 18, 2013

Midterm solutions. n f 3 (n) = 3

ECE368 Exam 2 Spring 2016

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

(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

CS301 - Data Structures Glossary By

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

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

CSE373 Winter 2014, Midterm Examination January 29, 2014

COMP Data Structures

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

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING ADVANCED DATA STRUCTURES AND ALGORITHMS EXAM EXAMINATION JUNE 2014

ECE250: Algorithms and Data Structures Midterm Review

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

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

Binary Search Trees Treesort

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

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

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

Introduction to Algorithms October 12, 2005 Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson Quiz 1.

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

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

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

CS171 Midterm Exam. October 29, Name:

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

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

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

Data Structures and Algorithms Week 4

Data Structures and Algorithms Chapter 4

Programming II (CS300)

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

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

COS 226 Algorithms and Data Structures Fall Midterm

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, January 29/Tuesday, January 30

Data Structures and Algorithms. Roberto Sebastiani

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

Programming II (CS300)

CSE 373 Spring Midterm. Friday April 21st

CS 171: Introduction to Computer Science II. Quicksort

Faster Sorting Methods

Computer Science E-22 Practice Final Exam

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE. Sample Final Exam

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

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

Sorting and Selection

CS211 Spring 2005 Prelim 2 April 19, Instructions

Tree Structures. A hierarchical data structure whose point of entry is the root node

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

INF2220: algorithms and data structures Series 1

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

ECE242 Data Structures and Algorithms Fall 2008

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

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

CPSC 320 Midterm 2 Thursday March 13th, 2014

CLO Assessment CLO1 Q1(10) CLO2 Q2 (10) CLO3 Q4 (10) CLO4 Q3a (4)

CPSC 311: Analysis of Algorithms (Honors) Exam 1 October 11, 2002

Sorting. Task Description. Selection Sort. Should we worry about speed?

Computer Science 302 Spring 2007 Practice Final Examination: Part I

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

c) A perfect ternary tree of height h? (In a ternary tree, each node may have up to three children.)

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION /08:30-9:50 RCH 105, RCH 110

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

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

ECE 250 Algorithms and Data Structures

Data Structures and Algorithms

COMP 250 Midterm #2 March 11 th 2013

CS314 Exam 2 - Spring Suggested Solution and Criteria 1

Cornell University Computer Science 211 Second Preliminary Examination 18 April 2006

TREES. Trees - Introduction

ITEC2620 Introduction to Data Structures

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;

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

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

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

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

Binary Trees, Binary Search Trees

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

COS 226 Midterm Exam, Spring 2009

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

APCS :: Winter Quarter Exam Review Packet

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

Final Examination Semester 1 / Year 2011

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

(Refer Slide Time: 01.26)

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

Lecture 5: Sorting Part A

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

CS210 (161) with Dr. Basit Qureshi Final Exam Weight 40%

Chapter 10. Sorting and Searching Algorithms. Fall 2017 CISC2200 Yanjun Li 1. Sorting. Given a set (container) of n elements

Transcription:

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Please check your tutorial (TUT) section from the list below: TUT 101: F 11:30, MC 4042 TUT 102: M 10:30, MC 4042 TUT 103: M 11:30, MC 4058 TUT 104: F 10:30, MC 4042 TUT 105: M 12:30, STJ 2009 TUT 106: Th 4:30, MC 4042 TUT 107: M 8:30, MC 4042 Uncertain Cheating is an academic offence. Your signature on this exam indicates that you understand and agree to the University s policies regarding cheating on exams. Signature: 1. The examination starts at 7:00 pm and ends at 9:00 pm. You may not leave for the first half hour or in the last ten minutes. 2. You should have 11 pages in this exam (including the cover page), plus a separate 2- sided reference sheet. 3. No additional materials are permitted. 4. Complete all answers in the spaces provided. 5. Write neatly so that you do not lose marks unnecessarily. 6. Proctors will only confirm or deny the existence of errors on the exam. In the case of perceived ambiguity, state a clear assumption and proceed to answer the question. Your assumption must not trivialize the question. Question # Out Of Marks Marker 1 10 2 10 3 12 4 12 5 5 6 5 7 6 Total 60

Page 2 of 15 Student id: Question I Multiple Choice, True/False, Short Answer [21 Marks - 1 mark per question] There were 21 Multiple Choice/True-False/Short Answer questions on this exam worth a total of 21 marks. Question II Traversals [6 marks] a) [3 marks] Suppose we have the following results from performing an inorder and levelorder traversal on a binary tree: inorder: A, W, L, E, T, R level-order: W, A, T, E, R, L Construct all trees that are consistent with both traversals or show no such tree exists. b) [3 marks] Suppose we have the following results from performing a preorder traversal on a binary search tree containing numeric keys: preorder: 45, 32, 11, 29, 64, 50 Construct all trees that are consistent with the above traversal or show no such tree exists.

Question III Binary Search Trees [10 Marks] Page 3 of 15 Student id: a) [2 marks] Beginning with an empty binary search tree, what binary search tree is formed when you insert the following values in the order given: 1, 4, 3, 7, 5, 2, 6. You are only required to draw the final tree for all or nothing credit. If you draw the intermediate trees, you may receive partial credit. b) [2 marks] Show the two possible trees that result after the string kangaroo is removed from the binary search tree below using the binary search tree deletion algorithm.

Page 4 of 15 Student id: A perfect binary tree is tree where each node is either a leaf or has two children, and all leaves have the same depth. c) [2 marks] Give a perfect binary search tree with nodes labeled 1,2,3,4,5,6,7. d) [2 marks] You are given a binary search tree T where all nodes are uniquely labeled with integers between 1 and 99 inclusive (not all integers in that range are necessarily used). The root node is 4 and its left and right subtrees have the same height. Draw the tree that has the maximum possible sum over all the nodes? e) [2 marks] What is the worst case performance of searching for a key in a perfect binary search tree containing n keys. Give your answer in order notation in terms of n. Compare this to searching for a key in a possibly non-perfect binary search tree. Perfect: Non-Perfect:

Question IV Order Notation [10 Marks] Page 5 of 15 Student id: Use order notation to express the worst-case running time of each of the following code fragments in terms of n. Give as tight an asymptotic upper bound as possible. A println statement is considered to be an O(1) operation. a) b) c) d) e) int x = n; System.out.println( x ); for (int i = 1; i <= 10000; i++) { bigarray1[i] = bigarray2[i] + x; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { System.out.println( We want tight bounds! ); for( int i = 1; i <= n; i++ ) { x++; for(int i = 1; i <= n*n*n; i = i + n) { System.out.println( I love CS ); int i = 1; while (i < n) { i = i * 2; System.out.println( Enjoy CS 241 or CS 230! ); for( int i = 1; i <= n; i++ ) { if ( i == (n / 2) ) { for( int j = 0; j <= n* n ) { System.out.println( Halfway there! ); else { System.out.println( Lots to do, yet );

Page 6 of 15 Student id: 2 f) [3 marks] Prove by applying the definition of big-oh that f n) 5n 4n O n ( = is ( ) 2 g) [2 marks] Give two reasons that we might prefer an algorithm with an ( n ) over an algorithm with an O( n) running time. O running time

Question V Recursive Program [7 Marks] Page 7 of 15 Student id: A heap is a special binary tree which is either empty, or one where the value stored in all parent nodes in the tree is greater than all its children (if they exist). The following tree is a heap: Write the following two methods which could be added to any implementation of the ADT BinaryTreeInterface. You must use recursion whenever you need to traverse the tree. Specifically, you are not allowed to use for, while or do loops in any methods: public int maxvalue() { // pre: this is a heap of positive Integer objects // post: returns the maximum number contained in the heap, // or returns -1 if the tree is empty

Page 8 of 15 Student id: public boolean isheap() { // pre: all items in the tree are positive Integer objects // post: returns true if this is a heap

Question VI Inductive Proof [6 Marks] Page 9 of 15 Student id: Prove by induction on h that the number of leaves, L, in a tree of height h satisfy: L 2 h 1 For example, the following tree has two leaves and height 3 which satisfies the above relation: Use the space on the next page if you run out of room.

Question VI Inductive Proof Continued Page 10 of 15 Student id:

Question VII Sorting [12 Marks] Page 11 of 15 Student id: Carefully explain under what circumstances each of the following sorting algorithms would be the best sorting algorithm to use in an application. a) [1 mark] Mergesort b) [1 mark] Quicksort c) [1 mark] Selection sort d) [1 mark] Insertion sort e) [1 mark] Assume the array 20, 10, 80, 40, 15, 75 is sent as input to Selection Sort. Show the contents of the array immediately after the second swap f) [1 mark] Give an example of the best case input of size 6 for Insertion Sort. g) [1 mark] Assume the array 20, 10, 80, 40, 15, 5 is sent as input to Merge Sort. Show the contents of the array immediately before the final merge takes place. h) [1 mark] Assume the array 20, 10, 80, 40, 15, 75 is sent as input to Quick Sort. Show the contents of the array immediately after the first partition operation takes place. Use the first element as the pivot.

Page 12 of 15 Student id: i) [4 marks] Professor Dumbledore is developing a course on the Magic of Sorting. He proposes a new sorting algorithm called HogwartsSort based on the ideas of merge and selection sort. Using divide and conquer, we divide a full array (i.e. it is not partially filled) into two equal sized regions as we would for merge sort, until a region has less than n items. At that point each region is sorted using selection sort. Then the regions are merged as normal for merge sort. Complete the body of the HogwartsSort method as described above. Assume the existence of the merge and selectionsort methods described in the Sorting class of the Reference booklet. public static void HogwartsSort(Comparable[] ta, int first, int last){ // pre: 0 <= first <= last < ta.length and // ta and ta[first..last] are non-null // post: ta[first..last] is in ascending order i) [2 bonus marks] Analyze the running time of HogwartsSort. Do not attempt this until you have finished the rest of the exam. Show your work on the back of the previous page, but put your final answer here.

Page 13 of 15 Student id: Question VIII Searching Ordered Arrays [9 Marks] [6 marks] Recall that binary search splits the array in half. Suppose we wrote a variation of binary search which narrowed the search range to either the first, middle or last third of the array. Write the recursive body of this trisearch method assuming that it would appear inside of an ordered array implementation of the table ADT. Note the return type of the method. public class OrderedArray implements TableInterface { private KeyedItem[] items; // instance variable holding the sorted array private int size; // number of items in the table private KeyedItem trisearch( int lo, int hi, Comparable key ) { // pre: 0 <= lo < size; 0 <= hi < size; key!= null // post: if key is in items[lo..hi], return the corresponding KeyedItem // otherwise return null

Page 14 of 15 Student id: [2 marks] Suppose the initial number of items is N, where N is a power of 3. How many times is the trisearch method called in the worst case (including the original call)? Give the exact answer, and justify your answer. [1 mark] Is this version of searching asymptotically more efficient than binary search? Justify in one sentence.

Question IX Iterators [4 Marks] Page 15 of 15 Student id: Write a method which creates a duplicate copy of an existing TableInterface, T. This copy will contain exactly the same set of KeyedItems as the original. You should not alter the contents of T You can assume that the TableInterface defines a method tabletraverse which returns an Iterator for the given table (see the reference sheet) public static TableInterface duplicate ( TableInterface T ) { // pre: T is non-null // post: returns a new TableInterface containing the same set of // KeyedItems as T