ANSWER: // As long as the 2nd iterator is in the list while (it2!= lst.end()) { if (*it2 < *it1) // not in the sorted order

Similar documents
list<t>::const_iterator it1 = lst.begin(); // points to first element list<t>::const_iterator it2 = lst.begin(); // points to second element it2++;

Question Paper Code : 97044

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

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

ECE 250 Algorithms and Data Structures

Data Structures and Algorithm Analysis in C++

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

Bachelor Level/ First Year/ Second Semester/ Science Full Marks: 60 Computer Science and Information Technology (CSc. 154) Pass Marks: 24

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Chapter 1 Introduction

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

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?

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid

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

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid. Fall 2018

Course Review for Finals. Cpt S 223 Fall 2008

ECE 250 Algorithms and Data Structures

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

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Lab 8: Greedy Algorithms Due: Start of your next lab session

PROGRAMMING IN C++ (Regulation 2008) Answer ALL questions PART A (10 2 = 20 Marks) PART B (5 16 = 80 Marks) function? (8)

Priority Queues. 04/10/03 Lecture 22 1

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

Final Examination. Algorithms & Data Structures II ( )

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

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

Exam Data structures DIT960/DAT036

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

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

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

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

Total No. of Questions :09] [Total No. of Pages : 02. II/IV B.Tech. DEGREE EXAMINATIONS, NOV/DEC First Semester CSE/IT DATA STRUCTURES USING C

CSE373 Spring 2015: Final

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

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

Tribhuvan University Institute of Science and Technology Computer Science and Information Technology (CSC. 154) Section A Attempt any Two questions:

Solutions to Exam Data structures (X and NV)

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

Homework Assignment #3 Graph

CSE373 Fall 2013, Midterm Examination October 18, 2013

CS 216 Fall 2007 Final Exam Page 1 of 10 Name: ID:

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

9. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally likely.)

Name: 1 stack<int> s; 2 s.push(9) 3 s.push(5) 4 s.push(10) 5 s.push(1) 6 s.pop() 7 s.pop() 8 s.push(0) 9 s.pop() 10 s.pop() 11 s.

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

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

Priority Queues Heaps Heapsort

) $ f ( n) " %( g( n)

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists

Questions: ECE551 PRACTICE Final

Table of Contents. Chapter 1: Introduction to Data Structures... 1

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

COS 226 Algorithms and Data Structures Fall Midterm

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

Test points UTA Student ID #

CS302 Data Structures using C++

CSE 373: Practice Final

CS 2150 Final Exam, Spring 2018 Page 1 of 10 UVa userid:

( ) 1 B. 1. Suppose f x

Final Examination CSE 100 UCSD (Practice)

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

Minimum Spanning Trees

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

CS 112 Final May 8, 2008 (Lightly edited for 2012 Practice) Name: BU ID: Instructions

OBJECT ORIENTED DATA STRUCTURE & ALGORITHMS

( ) n 3. n 2 ( ) D. Ο

CS171 Final Practice Exam

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

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

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

Recitation 9. Prelim Review

Priority Queues (Heaps)

CSCE 2014 Final Exam Spring Version A

Total Score /15 /20 /30 /10 /5 /20 Grader

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

INSTITUTE OF AERONAUTICAL ENGINEERING

Sorting and Searching

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο

Exam Data structures DAT036/DAT037/DIT960

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

Minimum Spanning Trees My T. UF

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

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

n 2 ( ) ( ) + n is in Θ n logn

Sorting and Searching

Exam Datastrukturer. DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE

n 2 C. Θ n ( ) Ο f ( n) B. n 2 Ω( n logn)

CS171 Final Practice Exam

CS61B, Fall 2002 Discussion #15 Amir Kamil UC Berkeley 12/5/02

Practice Midterm Exam Solutions

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Exam Data structures DAT036/DAT037/DIT960

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

Data Structures Brett Bernstein

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

Exam 3 Practice Problems

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

Transcription:

============================================= 200809 Spring CSC 393 Review for Final Sample Solutions ============================================= #1. Write a template function islistsorted. It returns true if the elements in the parameter STL list 'lst' are in the ascending order (determined by <). Write the function using a loop. template<typename T> bool issorted(const list<t> & lst) if (lst.size() < 2) return true; list<t>::const_iterator it1 = lst.begin(); points to first element list<t>::const_iterator it2 = lst.begin(); points to second element it2++; As long as the 2nd iterator is in the list while (it2!= lst.end()) if (*it2 < *it1) not in the sorted order return false; early exit advance both iterators it1++; it2++; All adjacent elements are in the sorted order. Return true as the final result. return true;

#2. Write another version of the function from the previous question using recursion. template<typename T> bool islistsorted(const list<t> & lst) Take care of the case when the list of empty or has only one element first. if (lst.size() < 2) return true; list<t>::const_iterator it1 = lst.begin(); list<t>::const_iterator it2 = lst.begin(); it2++; Call the auxilirary recursive function. Whatever it returns, that's the answer from this top-level funciton. return islistsortedaux(it1, it2, lst.end()); The auxilirary, recursive function. Note that some compilers have difficulty instantiating templetized iterators, so here the element type int is instantiated (and the function is NOT a template function). bool islistsortedaux(list<int>::const_iterator it1, list<int>::const_iterator it2, list<int>::const_iterator end) test for base case first -- recursion terminates when the 2nd ierator is off the list. if (it2 == end) return true; if (*it1 > *it2) return false; return islistsortedaux(++it1, ++it2, end);

#3. For the doubly-linked List class you wrote for HW#4, add the following function (which minics that in the STL list): void merge(list& x); This function is rather involved (more than splice() you wrote for HW#4), and YOU DON'T HAVE TO WORRY ABOUT IT. However, I recommend you read my solution below and figure out how I'm doing this task. The logic, in particular the traversal over two sorted Lists for merge, while keeping the existing iterators in-tact, may require some thinking, because it has to be different from the 'standard' merge procedure for arrays or vectors. void merge(list& x) if (this!= &x) iterator p1 = begin(); iterator p2 = x.begin(); iterator p3; Strategy is to advance p1 until it points to an element which is larger than the element p2 currently points to. Then we insert the node pointed by p2 into *this just before p1. while (p1!= end()) Advance p1 while (p1!= end() && *p1 < *p2) p1++; If the loop above terminated because it went off the list, if (p1 == end()) break; break out of the loop (to go to (***)) while (*p2 < *p1) save p2's next in p3 p3 = p2.current->next; p2.current->prev->next = p2.current->next; (1) p2.current->next->prev = p2.current->prev; (2) p1.current->prev->next = p2.current; (3) p2.current->prev = p1.current->prev; (4) p2.current->next = p1.current; (5) p1.current->prev = p2.current; (6) thesize++; x.thesize--; p2 = p3; essentially to advance p2 (***) If any element is still left in x, we splice all of them (in one operation) intoat the end of *this. if (p2!= x.end()) Being lazy here -- calling splice(). Note at this point, p1 is pointing at this->end(), and x contains the remaining elements (only). splice(p1, x);

#4. T(n) and Big-Oh complexity. a) int x = 0; for (int i = 1; i <= 2n; i++) for (int j = 1; j <= i; j++) x = x + 1; count this line T(n) = 2n^2 + n Complexity is O(n^2). ---------------------------- b) YOU CAN IGNORE THIS ONE, but the answer is (as worked out in the class), int x = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= i; j++) for (int k = 1; k <= j; k++) x = x + 1; count this line T(n) = 16 * n^3 + 3n^2 + 2n Complexity is O(n^3). ---------------------------- c) int i; i = n; while (i >= 1) x = x + 1; count this line i = i 2; i = n; while (i >= 1) x = x + 1; count this line too i = i 2; T(n) = 2*lg(n) + 2 Complexity is O(lg(n)). ---------------------------- d) int x = 0; int i = 1, j = n; while (i < j) x = x + 1; count this line i++; j--; T(n) = 12*n Complexity is O(n). ---------------------------- e) int x = 0; for (int i = 1; i <= n; i++) x = x + 1; count this line return x; T(n) = 1 Complexity is O(1).

#5. Show the result of inserting 8, 3, 6, 16, 2, 11, 15 into an initially empty binary search tree. Then show the result of deleting the root. After inserting 8, 3, 6, 16, 2, 11, 15 8 3 16 2 6 11 15 After deleting the root 11 3 16 2 6 15 #6. Show the result of inserting 2,1,6,3,4,5,7,9,8 into an empty AVL tree. Draw the tree after each insertion. AVL after inserting 2,1,6,3,4 2 2 1 6 case 2 1 4 at 6 3 ======> 3 6 4 insert 5 2 4 1 4 case 4 2 6 3 6 at 2 ======> 1 3 5 5 insert 7, 9 4 2 6 1 3 5 7 9 insert 8 4 4 2 6 case 3 2 6 1 3 5 7 at 7 ======> 1 3 5 8 9 8 7 9

#7. Show the result of inserting 10, 22, 31, 4, 15, 28, 17, 88, 59 into an empty Top-down Red-Black tree. Draw the tree after each insertion. (1) insert 10 (2) insert 22 (3) insert 31 10 10 10 22 single 22R 22R rotation 31R ==> 31R (4) insert 4 22 color flip 22 insert 22 31R at 22 ==> 10 31 4R ==> 10 31 4R (5) insert 15 (6) insert 28 22 22 10 31 10 31 4R 15R 4R 15R 28R (7) insert 17 22 22 22 color insert 10 31 flip at 31 17R ==> 31 10 ==> 4R 15R 28R 4 15 28R 4 15 28R 17R (8) insert 88 22 31 4 15 28R 88R 17R (9) insert 59 22 22 22 color insert 31 flip at 31 ==> 31R 59R ==> 31R 4 15 28R 88R 4 15 28 88 4 15 28 88 17R 17R 17R 59R

#8. Write a method void remove(const T& element) for the class Heap shown in the class (defined in "heap.h"). Remove takes one argument, an item (not necessarily the one with minimum priority) to be removed from the queue if it exists. template<class T> void Heap<T>::remove(const T& element) First find the element int index = 0; while (index < array.size()) if (array[index] == element) break; index++; if (index!= array.size()) found overwrite the element with the last element in the array array[index] = array[array.size()-1]; then physically remove the last element array.pop_back(); size--; now reconstruct the heap percolate down from index percolatedown(index, size); #9. Suppose we used the following formula for producing probes: p(i) = (H + i^2 + i) % M, where M is a prime number and the table size. Show the result of inserting 2, 11, 17, 0 in the table where M = 11. This question is left for your exercise. Enjoy. #10 & #11. These questions are left for your exercise as well. Enjoy. #12. Given the following graph, show how Dijkstra's algorithm finds the shortest path from A to F. 2 3 1 A --- B --- C --- G 1 2 2 2 D --- E --- F 2 5 See the next two pages. #13. Find the minimum spanning tree for the graph in the text, Figure 9.82 on p. 399, using both Prim's and Kruskal's algorithms. This question is left for your exercise too.

Dijksha's Algorithm s=? q o=1.a,9'(, t2,7, (IJ B s= t a 1 "=ZD,0,c,eT,G S= <nt ' Q= I a i-," <'.)) ct _l F 61 s= LA, D, V ZE.c,r,G

s= la 'I c, ab e j r'q1 s=?a, D. 9, e,c I a=zg, TJ s= t,4,0, Vt,C,C, GJ Q= ) r-? I r ) s= I o= J