Docsity

Final Exam Practice Problems with Solution Data Structure | CSCI 1200, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
CSCI-1200 Computer Science II — Spring 2006
Final Exam Practice Problem Solutions
1. Write a function to create a new singly-linked list that is a copy of a sublist of an existing list. The
prototype is:
Node<T>* Sublist(Node<T>* head, int low, int high)
The Node class is:
template <class T>
class Node {
public:
T value;
Node* next;
};
The new list will contain high-low+1 nodes, which are copies of the values in the nodes occupying
positions low up through and including high of the list pointed to by head. The function should
return the pointer to the first node in the new list. For example, in the following drawing the original
list is shown on top and the new list created by the function when low==2 and high==4 is shown
below.
New list
3.1 2.4 8.7 9.4head
2.4 8.7 9.4
nhead
14.2 0.9
Original list
A pointer to the first node of this new list should be returned. (In the drawing this would be the
value of nhead.) You may assume the original list contains at least low nodes. If it contains fewer
than high nodes, then stop copying at the end of the original list.
Solution:
Node<T>* Sublist(Node<T>* head, int low, int high) {
// Skip over the first low-1 nodes in the existing list
Node<T>*p = head;
int i;
for (i=1; i<low; ++i) p = p-> next;
// Make the new head node and make a pointer to the last node in list
Node<T>* new_head = new Node<T>;
new_head->value = p->value;
Node<T> * last = new_head;
// Copy the remaining nodes, one at a time
for (++i, p = p->next; i<=high && p; ++i, p = p->next) {
last->next = new Node<T>;
last->next->value = p->value;
last = last->next;
}
last->next = 0;
}
1
pf3
pf4
pf5

Partial preview of the text

Download Final Exam Practice Problems with Solution Data Structure | CSCI 1200 and more Exams Data Structures and Algorithms in PDF only on Docsity!

CSCI-1200 Computer Science II — Spring 2006

Final Exam Practice Problem Solutions

1. Write a function to create a new singly-linked list that is a copy of a sublist of an existing list. The

prototype is:

Node* Sublist(Node* head, int low, int high)

The Node class is:

template class Node { public: T value; Node* next; };

The new list will contain high-low+1 nodes, which are copies of the values in the nodes occupying

positions low up through and including high of the list pointed to by head. The function should

return the pointer to the first node in the new list. For example, in the following drawing the original

list is shown on top and the new list created by the function when low==2 and high==4 is shown

below.

New list

head 3.1 2.4 8.7 9.

nhead 2.4 8.7 9.

Original list

A pointer to the first node of this new list should be returned. (In the drawing this would be the

value of nhead.) You may assume the original list contains at least low nodes. If it contains fewer

than high nodes, then stop copying at the end of the original list.

Solution:

Node* Sublist(Node* head, int low, int high) { // Skip over the first low-1 nodes in the existing list Nodep = head; int i; for (i=1; i<low; ++i) p = p-> next; // Make the new head node and make a pointer to the last node in list Node new_head = new Node; new_head->value = p->value; Node * last = new_head; // Copy the remaining nodes, one at a time for (++i, p = p->next; i<=high && p; ++i, p = p->next) { last->next = new Node; last->next->value = p->value; last = last->next; } last->next = 0; }

2. Suppose that a monster is holding you captive on a computational desert island, and has a large file

containing double precision numbers that he needs to have sorted. If you write correct code to sort

his numbers he will release you and when you return home will be allowed to move on to DSA. If you

don’t write correct code, he will eventually release you, but only under the condition that you retake

CS 1. The stakes indeed are high, but you are quietly confident — you know about the standard

library sort function. (Remember, you are supposed to have forgotten all about bubble sort.) The

monster startles you by reminding you that this is a computational desert island and because of this

the only data structure you have to work with is a queue.

After panicking a bit (or a lot), you calm down and think about the problem. You realize that if you

maintain the values in the queue in increasing order, and insert each value into the queue one at a

time, then you can solve the rest of the problem easily. Therefore, you must write a function that

takes a new double, stored in x, and stores it in the queue. Before the function is called, the values

in the queue are in increasing order. After the function ends, the values in the queue must also be

in increasing order, but the new value must also be among them.

Here is the function prototype:

void insert_in_order(double x, queue& q)

You may only use the public queue interface (member functions) as specified in lab. You may use a

second queue as local variable scratch space or you may try to do it in a single queue (which is a bit

harder). Give an “O” estimate of the number of operations required by this function.

Solution: Here is 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.

4. Write a cs2list member function called splice that takes an iterator and a second cs2list

object and splices the entire contents of the second list between the node pointed to by the iterator

and its successor node. The second list must be completely empty afterwards. The function prototype

is:

template void cs2list::splice(iterator itr, cs2list& second);

No new nodes should be created by this function AND it should work in O(1) time (i.e. it should be

independent of the size of either list).

Solution:

template void cs2list::splice(iterator itr, cs2list& second) { if (second.empty()) return; second.head_->prev_ = itr.ptr_; second.tail_->next_ = itr.ptr_->next_; if (itr.ptr_->next_) { itr.ptr_->next_->prev_ = second.tail_; } else { // itr.ptr_ is the tail, so it must be reset this->tail_ = second.tail_; } itr.ptr_->next_ = second.head_; this->size_ += second.size_; second .size_ = 0; second.head_ = second.tail_ = 0; }

5. 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.

Recursive Solution:

int FindLargest(TreeNode* root) { if (! root->right) return root->value; else return FindLargest(root->right) }

Non-recursive Solution:

int FindLargest(TreeNode* root) { while (root->right) root = root->right; return root->value; }

6. Write a recursive function to count the number of nodes stored in the binary tree pointed to by

TreeNode* root.

Solution:

int Count(TreeNode* root) { if (! root) return 0; else return 1 + Count(root->left) + Count(root->right); }

7. Write a new member function of the cs2set class called to vector that copies all values from the

binary search tree implementation of the set into a vector. The resulting vector should be increasing

order. You may assume the vector is empty at the start. The function prototype should be:

template void cs2set::to_vector(vector& vec);

Solution:

template void cs2set::to_vector(vector& vec) { to_vector(this->root_, vec); }

template void cs2set::to_vector(TreeNode* p, vector& vec) { if (p) { to_vector(p->left, vec); vec.push_back(p->value); to_vector(p->right, vec); } }

8. 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); } } }