- Tear off this sheet and use it to keep your answers covered at all
times. If you need more scratch paper, I have some.
- Write your name on the back of the test by the staple to facilitate
returning the exams. Do not write your name at any other place on the exam.
- If you don't understand a question, come and ask me about it!
- Do NOT use ink. Programming often requires one to change his/her
mind.
- No question is meant to have a syntax error. If it does,
fix it or ask me how I want the error resolved.
- You are graded not only on the correct answer, but also the best answer.
- NO calculators are allowed on the test. They will not be
needed.
class EdgeNode
{ public:
int ToNode; // Node number
int FromNode; // Node number
bool Selected; // True if edge is selected as being part of the match
EdgeNode *Next; // Next edge in linked list
EdgeNode( int F = -1, int T=-1, EdgeNode *N = NULL): ToNode( T ),
FromNode(F), Next( N ){}
};
class GraphNode
{ public:
char Element; // Node code
EdgeNode *Adj; // Adjacency list
bool visited;
};
class Graph
{ GraphNode G[MAX];
}
1
Final Exam CS 2200
Fall 2000
100 Points
- For the code below, which picture represents the complexity?
void doit(int n)
{ int i;
if (n <=2) return;
if (n%2==0)
doit(n/2);
else doit(n/2);
}
- Left most child, next right sibling method of storage involves the following
- more space is required for faster access
- less space is required for slower access
- code is less complicated for more space
- by level traversals are facilitated
- What best describes what the following code does?
int mystery(node * root)
{ if (root== NULL) return -999;
if (root->left ==NULL || root->right ==NULL) return root->val;
return( max(mystery(root->left), mystery(root->right)));
}
- It finds the value of the biggest leaf node.
- It finds the biggest value of the leaves or semi-leaf nodes it looks at.
- It checks to make sure the tree has the heap property.
- It finds the value of the biggest of all the nodes (internal and leaf).
- You have two solutions for the same problem. One is O(
) and the other is
O(n log n). When you compare the times for various problems, the O(n log n) algorithm is
sometimes faster and sometimes slower. How do you explain this?
- The complexity is worse case. For some data sets, you can do significantly better.
- Your problem sizes are too large to see the advantages of the O(n log n) algorithm.
- You are running on a very fast machine.
- The O(
) algorithm has a higher value for the constant.
- For the timing information below, what is the complexity?

(a) O(1) (b) O(log n) (c) O(n) (d) O(n log n) (e) O(
)
- From our theorem we know:
T(n)= a T(n/b) + O(
)
if
T(n) is O(
)
if
T(n) is O(
)
if
T(n) is O(
)
Consider the following algorithm:
int doit(int A[], int n){
if (n <=1) return 1;
int t;
for (int i =0; i < n; i++)
t++;
t+= 4*doit(A,n/3)
return t;
}
What is the complexity?
(a) O(1) (b) O(log n) (c) O(n) (d) O(n log n) (e) O(
)
- A breadth first traversal of a graph is commonly done using
(a) a stack
(b) a queue
(c) a heap
(d) a topological ordering
(e) none of the above
- A collision resolution in which each key has an individualized increment is termed
- linear
- quadratic
- double hashing
- lazy
- Rehashing is done for the following reason
- The hash table is becoming too full
- The hash table has too many lazily deleted items
- The hash table is too large
- All of the above
- A merge sort would be an example of which type of algorithm?
- divide and conquer
- greedy
- exhaustive
- dynamic programming
- A leftist tree was designed to
- allow priority queues to be merged
- promote balance
- save storage
- facilitate bin packing
- none of the above
- In sorting, when a large array has just a few elements out of order,
which algorithm is the best for
restoring the order?
- mergesort
- insertion sort
- selection sort
- heap sort
- What does the following code accomplish?
for (int i = n-1; i >= 1; i--)
{
H.DeleteMax(x);
a[i+1]=x;
}
- Priority queue
- heapsort
- union find initialization
- tournament tree
- A friend says, ``Keeping a tree balanced at all times
is overkill. Most trees are reasonably balanced. Why not just
use a regular binary search tree and rebuild it when it gets too
off balance?'' An appropriate response would be
- That would be reasonable.
- This is not a good idea as it is difficult to define ``too off balanced''.
- This is a bad idea.
Why pay the cost of an occassional O(n) operation when you can keep the tree balanced without
increasing the O(log n) complexity?
- This is a bad idea as rebalancing is O(
).
- (13 points) A binary tree is strictly binary if every node has
exactly two children or none. Write the code to check if a tree
is strictly binary. The code should return a true if the tree is
strictly binary and a false otherwise.
- (15 points) Use the code from the cover page to store an undirected graph using an
adjacency list representation.
Assume that a graph has been stored and someone has found a matching of the graph
such that the edges of the match
are marked using the boolean ``Selected'' in the EdgeNode class.
Write a routine to verify that the matching is a valid matching. We will consider a valid
matching to mean that (a) no node is paired with more than one other node, and (b) there is
no edge which connects two unmatched nodes.
- (15 points) The code below contains the dynamic programming solution
for finding the length of the shortest path between any two nodes. The actual path has not
been produced.
Modify the code so that the actual
shortest path between any two points will be stored as an array of nodes. For example,
if the shortest path from node 5 to node 8 consists of the path
the ActPath array would contain {5,4,1,8}.
class Path
{ int FromNode;
int ToNode;
int len; // Length of path
int ActPath[MAX]; // list of nodes in actual shortest path from FromNode to ToNode.
int ActPathCt; // number of nodes in ActPath
}
Path Short[MAX][MAX];
for (k = 0; k<nodeCt;k++)
for (i = 0; i<nodeCt;i++)
if (Short[i][k].len <INFINITY){
for (j = 0; j<nodeCt;j++)
if (Short[i][k].len + Short[k][j].len < Short[i][j].len)
Short[i][j].len=Short[i][k].len + Short[k][j].len;
}
- (15 points)
The following code attempts to delete from a binary search tree. Correct any errors.
Assume deleteSmall removes the smallest value in the tree passed to it and returns the value
deleted.
void deleteNode(Node * n, int delVal)
{
if (n == NULL) return;
if (n->val < delVal) deleteNode(n->right,delVal);
else if (n->val < delVal) deleteNode(n->left,delVal);
else{
int small = deleteSmall(n->right);
n->val = small;
}
}
Vicki Allan
Thu Apr 26 16:41:01 MDT 2001