The exam is open book: you may consult your notes, books, even computer
programs that you have written before. But all the work should be yours.
Show all your work: you may get partial credit. Add extra sheets if
necessary.
-
(10 points) Given the following code:
struct Node{
int info;
Node* parent;
Node* child;
};
main()
{
Node a1, a2;
Node* b, b3, b4;
b = &a1;
a1.info = 5; a2.info = 4;
b3=new Node;
b4=new Node;
b3->info = 10; b4->info = 3;
a1.parent = new Node;
a2.parent = &a1;
b3->parent = &a2;
b4->parent = &a1;
b->parent->parent = NULL;
b->parent->info = 20;
b->child = &a2;
}
-
(2 points) Draw a "box and arrow" structure corresponding to the set of
Node's created by this program and fill in all the information and
pointers as done by the program.
-
(2 points) Write a C++ statement to set to 7 the value of the info field
of the parent of a1.
-
(2 points) Write a C++ statement to change the parent of the node
pointed to by b3 to be the node pointed to by b4. You should
program this according to the specifications, i.e. do not make
use of the fact that you happen to know what is the parent of the node
pointed to by b4.
-
(4 points) Write C++ statements to set the children of all the
nodes accessible through the pointer b to be a new node
(which you will allocate dynamically).
-
(34 points) Given the following structure definition:
struct BNode{
int x;
BNode* y;
BNode* z;
};
and the following definitions of variables:
BNode a1, *a2, a3[5];
BNode* b4[10];
Which of the following is a valid C++ expression? (2 points each) Cross
the invalid expressions.
-
a2-> y . z
-
a3[2].x ->x
-
a2->z
-
a4[2]->z->y->y.z
-
a1.y
-
a2.z
-
a3[2].x
-
a1.y.z
-
a1.y -> z
-
a1 -> z
-
a2.y -> z
-
a2->y ->y
-
a3[2].x.z
-
b4[5].y->z
-
b4[5]->y->z->z
-
b4[5]->y->z->z->x
-
a3[2]->z->y
-
(12 points) Write a short recursive C++ function (NOT a whole
program!!!) to compute the minimum element in a binary search tree.
The program should use the representation of a binary (search) tree as a pointer
to the root node, where a tree node is a C++ structure defined as:
struct TreeNode{
int data;
TreeNode* left;
TreeNode* right;
}
Name your function minTree. It should take as argument
the root and return an integer (the minimum value). Design the
function as a separate entity, not as part of some binary tree class.
If the recursive definition seems elusive but you have other ideas for solving this
problem, I also accept other solutions. Warning: an iterative solution may not be easy
to produce.
- (10 points) Trace the stack behavior for the following recursive function
call. What is the value printed in the main function?
main()
{
cout << func(2,2);
}
int func(int a, int b)
{
if (a==0 || b== 0) return 1;
return func(a, b-1)*func(a-1, b);
}
-
(18 points) Given the graph:
-
(2 points)Write the adjacency list representation for the graph, with each
adjacency list sorted increasingly by the vertex number.
- (10 points)
Write code to define and allocate in C++ the adjacency list representation for the
previous digraph. You may allocate the list elements either statically or dynamically.
Write C++ statements to link the list nodes into the corresponding
adjacency lists, and into the array of vertices representing the whole graph.
-
(3 points) Starting from vertex 1, list the vertices of the graph in the
order in which they are visited by a depth-first search algorithm.
-
(3 points) Starting at vertex 1, list the vertices in the order in which
they are visited by a breadth-first search algorithm.
-
(6 points) Given the following binary tree:
Write down the vertices of the tree in prefix order (2
points), infix order
(2 points) and postfix order (2 points).
-
(10 points) Sort the list of numbers {15, 3, 12, 5, 1, 14, 2, 9, 7} using
binary search trees.
-
Step 1:construct the binary search tree.
-
Step 2: traverse the tree in the appropriate order (and specify which one).
Enjoy your summer :-)
Ileana