Download Data Structures and Algorithms Study Guide: CSci II and more Exams Data Structures and Algorithms in PDF only on Docsity!
Computer Science II — CSci 1200
Test 3 — Overview and Practice
Overview
- Test 3 will be held Friday, April 20, 2007 2:00-3:30pm, Darrin 308. No make-ups will be given except for emergency situations, and even then a written excuse from the Dean of Students office will be required.
- Coverage: Lectures 15-20, Labs 9-11, HW 7-8. Material from earlier in the semester, especially on pointers may also be covered on the test.
- Closed-book and closed-notes. BUT, you may bring a one-page (8.5x11) double-sided crib sheet without anything written or printed on it that you want. One potential use of this crib sheet is to outline the member functions of the list, vector and map container classes.
- Below are relevant sample questions from previous tests. Solutions will be posted on-line.
- Important note: This review does not cover problems on hashing and hash tables. This is new material to CSci II. You can expect to see some coverage of this on the test, however.
- How to study?
- Review lecture notes
- Review and re-do lecture exercises, lab and homework problems.
- Do as many of the practice problems below as you can. Focus on the ones that cause you trouble. Practice writing solutions using pencil (or pen) and paper.
Practice Problems
- Consider the mergesort function discussed in detail in lecture. Suppose the vector passed to mergesort has 7 items in it. Specify the EXACT set of function calls that are made and the exact order that they are made. In specifying this, you do not need to show the contents of the vector, just the values of low and high (and mid, for calls to merge). This is not a question that will appear on the test, but it is instructive about the behavior of merge sort.
Solution:
mergesort: low = 0, high = 6 mergesort: low = 0, high = 3 mergesort: low = 0, high = 1 mergesort: low = 0, high = 0 mergesort: low = 1, high = 1 merge: low = 0, mid = 0, high = 1 mergesort: low = 2, high = 3 mergesort: low = 2, high = 2 mergesort: low = 3, high = 3 merge: low = 2, mid = 2, high = 3 merge: low = 0, mid = 1, high = 3 mergesort: low = 4, high = 6
mergesort: low = 4, high = 5 mergesort: low = 4, high = 4 mergesort: low = 5, high = 5 merge: low = 4, mid = 4, high = 5 mergesort: low = 6, high = 6 merge: low = 4, mid = 5, high = 6 merge: low = 0, mid = 3, high = 6
- Consider the public stack interface.
template class stack { public: stack(); stack( stack const& other ); ~stack(); void push( T const& value ); void pop( ); T const& top( ) const; int size(); bool empty(); };
In Lab 10 you implemented the stack using a std::vector. In this question you are not allowed to use either a std::vector or a std::list. Instead you are to implement the stack using a dynamically-allocated array. To answer the question, show what private member vari- ables are needed and provide the implementation of the default constructor, the destructor, stack::push, and stack::pop. All operations should be as efficient as possible.
Solution:
template class stack { public: stack() : arr(0), size(0), alloc_size(0) { }
stack( stack const& other );
~stack() { delete [] arr; }
void push( T const& value ) { if ( size == alloc_size ) {
(which is a bit harder). Give an “O” estimate of the number of operations required by this function.
Solution: Here’s the version with a scratch queue
void insert_in_order( double x, queue& q ) { if ( q.empty() ) q.push( x ); else { queue temp( q ); // copy q; while ( !q.empty() ) q.pop(); // empty q out
while ( !temp.empty() && x > temp.front() ) { double item = temp.front(); temp.pop( ); q.push( item ); }
q.push( x ); // insert x in its proper position
while ( !temp.empty() ) { double item = temp.front() temp.pop( ); q.push( item ); } } }
This function requires O(n) operations. Copying the queue initially and emptying the queue requires O(n) time each. The second and third while loops, combined, touch each entry in the queue and therefore require O(n) operations. Since none of these loops are nested we add the results and get O(n) time overall.
Here’s a version without a scratch queue:
void insert_in_order( int x, queue& q ) { int n = q.size(); int position = 0;
// Find the position for x in the queue, copying all values // less than x to the back of the queue
while ( position < n && x < q.front() ) {
q.push( q.front() ); // copy the front to the back q.pop(); // remove the front ++ position; }
q.push( x ); // put x in position
// The first n-position entries on the queue haven’t been // touched and are greater than or equal to the value // stored in x. They need to move to the back of the queue
int i = position; while ( i < n ) { q.push( q.front() ); // copy the front to the back q.pop(); // remove the front ++ i; } }
The two while loops, combined touch each entry in the queue once and therefore require O(n) operations. Since none of these loops are nested we add the results and get O(n) time overall.
- For this question and the next few, consider the following tree node class
template class TreeNode { public: TreeNode() : left(0), right(0) {} TreeNode(const T& init) : value(init), left(0), right(0) {} T value; TreeNode* left; TreeNode* right; };
Write a function to find the largest value stored in a binary search tree of integers pointed to by TreeNode* root. Write both recursive and non-recursive versions.
Solution: Recursive version:
int FindLargest( TreeNode* root ) { if (! root -> right ) return root -> value; else return FindLargest( root -> right ) }
- Write a function called Trim that removes all leaf nodes from a tree, but otherwise retains the structure of the tree. Hint: look carefully at the way the pointers are passed in the insert and erase functions.
Solution:
template void Trim( TreeNode *& root ) // Passing by reference is crucial here { if ( root ) // Only do something for non-empty trees { if ( !root->left && !root->right ) // Leaf { delete root; root = 0; // This sets the appropriate pointer in the parent node to 0. } else { Trim( root->left ); Trim( root->right ); } } }
- Write a function that takes a set of strings and returns a pointer to an array containing the strings in the set that have exactly 4 characters. The function must also return the number of such strings so that the size of the newly constructed array is known. You may assume there is at least one such string. Here is the prototype, with arr being the reference to the array pointer and n being the number of strings with 4 characters.
void four_character_strings(const set& values, string* & arr, int& n);
Solution:
void four_character_strings(const set& values, string* & arr, int& n) { set::const_iterator p;
// count the number of four letter strings n = 0; for (p = values.begin(); p != values.end(); p++) if (p->length() == 4) n++;
// allocate space for n strings arr = new string[n];
// go through a 2nd time to store the strings int i = 0; for (p = values.begin(); p != values.end(); p++) { if (p->length() == 4) { arr[i] = *p; i++; } } }
- A word ladder is a sequence of words that connect a source and target word such that each neighboring pair of words in the sequence differs by exactly one character. For example, here is a word ladder between the words “sea” and “oil”:
sea tea tee tie til oil
The characters can only be replaced one character at a time — they cannot be rearranged. Furthermore, each word must appear in the provided dictionary and words cannot be repeated in the ladder.
(a) First, write a function next word that enumerates all possible words that can be adjacent to an input word, current. The function takes a second parameter, dictionary, which is simply the set of all valid words that may appear in a word ladder. The function returns a vector of the possible next words. Here is the prototype for your function: vector next_word(const string ¤t, const set &dictionary);
Solution: vector next_word(const string ¤t, const set &dictionary) { vector answer; for (int i = 0; i < current.size(); i++) { for (int j = 0; j < 26; j++) { string next = current; next[i] = char(’a’+j); if (next != current && dictionary.find(next) != dictionary.end()) answer.push_back(next); } } return answer; }