CS2604 : Data Structures and File Processing
Summer Session II 1999 : Midterm 1
Question One [30]


Because we need enough of the smallest edge weights until we cover all vertices – we don’t need the whole set
Min= 2^(h-1) Max=2^h-1
120, 42, 43, 7, 2, 32, 37, 24, 40



void BST::Sort ( Node *rt )
{
if (rt==NULL) return;
Sort (rt->Left);
rt->Output ();
Sort (rt->Right);
}
Assume the following definitions :
class Node {
public:
int aValue;
Node *Left, *Right;
void Output ();
};
class BST {
public:
Node *root;
void BST::Sort ( Node *root );
.
.
};
Assume that the function is called externally using the statement Sort (root);
Question Two [15]
To output the contents of a node *p, use the statement p->Output ();
(0, 1) (5, 6) (0, 6) (2, 3) (3, 4) (0, 2) (6, 7)



Question Three [30]
Mark all nodes unvisited.
While nodes are unvisited, perform DFS on them.
Function DFS (node aNode)
...Visit (aNode)
...Set aNode to be visited
...For each neighbor N of aNode that is unvisited, DFS(N)
Dijkstra: shortest distance from one vertex to all others
Floyd: shortest distance between every pair of vertices
Dense: E is approximately V^2, hence Prim is better
Sparse: E is less than V, hence Kruskal is better
Use a priority queue for edge weights
Base case: n=0: 0=n(n-1)/2 edges
n=1: 0=n(n-1)/2 edges
Thus the formula holds for the base case
Suppose the formula holds for k vertices.
A new vertex will introduce k new edges.
Therefore, edges(k+1) = edges(k) + k = k(k-1)2+k = (k+1)k/2 = (k+1)((k+1)-1)/2
Hence, by induction, the result holds.
Question Four [25]
Selection: Swap is always order n.
Insertion: Best case for comparisons is order n.
Best: O(n Log n)
Worst: O(n^2)
22, 31, 42, 11, 12, 41, 23, 13, 21, 33
22 31 42 11 12 41 23 13 21 33
(22 31 42 11 12) (41 23 13 21 33)
(22 31 42) (11 12) (41 23 13) (21 33)
(22 31) (42) (11) (12) (41 23) (13) (21) (33)
(22) (31) (42) (11) (12) (41) (23) (13) (21) (33)
(22 31) (42) (11) (12) (23 41) (13) (21) (33)
(22 31 42) (11 12) (13 23 41) (21 33)
(11 12 22 31 42) (13 21 23 33 41)
(11 12 13 21 22 23 31 33 41 42)
22, 31, 42, 11, 12, 41, 23, 13, 21, 33
1 – 31, 11, 41, 21
2 – 22, 42, 12
3 – 23, 13, 33
31, 11, 41, 21, 22, 42, 12, 23, 13, 33
1 – 11, 12, 13
2 – 21, 22, 23
3 – 31, 33
4 – 41, 42
11, 12, 13, 21, 22, 23, 31, 33, 41, 42