============================================= 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