Computer Science 112a
Fall 2000
Ileana Streinu
CS 112
Final Examination
(Self-scheduled)
Name:
112a-ax account:
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. Write legibly
in the space allocated to each question, or, if necessary, on the
back of the preceeding page. If necessary, you can use extra
sheets. Answer all 9 questions (summing up to 100 points).
-
(3 points) Write the arithmetic expression tree
for the following arithmetic expression:
(((2+3)-(2*5))/(3-(4+5)))
-
(3 points) What type of traversal of an arithmetic expression
tree would you perform to get the following expression:
2244+*-32+* ? Draw the arithmetic expression tree
corresponding to this expression.
-
(9 points)
-
(3 points) Draw the double linked list structure, using the box notation,
for the following list: {a, d, e, g,h}, and give the struct definition
for a node of this list.
-
(3 points) Show the modification of the links when the new item
f is inserted
in alphabetical order in this list (i.e. after the element
e). Assuming
that you have access to the list through a variable
head pointing to the first node, write the C++
instructions that would perform this insertion. I mean: do not
write a class function and do not call a class function from the
list class - just write the few C++
instructions that would insert this particular element in
this particular list, using well known ways to access
fields of a struct (i.e. using dot and/or arrow
notation). Try to use additional temporary variables (besides
head) only if strictly necessary.
-
(3 points) Show the modification of the links when the item e is removed
from the list, and write the C++ instructions that perform this
deletion. Just like in the previous case, do not use additional
variables unless really necessary
and rely only on elementary dot and/or arrow notation.
-
(15 points) Given the following code:
struct SNode{
int info;
SNodePtr parent;
};
typedef SNode* SNodePtr;
main()
{
SNode a1, a2, a3, a4;
SNodePtr b;
b = &a1;
a1.info = 1; a2.info = 2; a3.info = 3; a4.info = 4;
a1.parent = new SNode;
a2.parent = &a1;
a3.parent = &a2;
a4.parent = &a1;
b->parent->parent = NULL;
b->parent->info = 0;
}
-
(5 points) Draw a "box and arrow" structure corresponding to the set of
SNode'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 10 the value of the info field
of the parent of a1.
-
(3 points) Write a C++ statement to change the parent of a3 to be a4.
- (5 points) If you remove the node created with new in
the previous program, what will you create (circle all that apply)?
- a memory leak
- a dangling pointer
-
(34 points) Given the following structure definition:
struct BNode{
int a;
BNodePtr b;
BNodePtr c;
};
typedef BNode* BNodePtr;
and the following definitions of variables:
BNode n1, *n2, n3[5];
BNodePtr n4[10];
Which of the following is a valid C expression? (2 points each) Cross
the invalid expressions.
-
n1 -> a
-
n1.a
-
n2.a
-
n2->a
-
n1.b.b
-
n1.b -> b
-
n2.b -> b
-
n2->b ->b
-
n2-> b . b
-
n3[2].c
-
n3[2].c ->a
-
n3[2].c.a
-
n3[2]->c->a
-
n4[5].b->a
-
n4[5]->b->a
-
n4[5]->b->c->a
-
n4[2]->c->b->b.a
-
(12 points) Write a short recursive C++ function (NOT a whole
program!!!) to compute the length of a list. Call it
Length. The list is given by a pointer to
its first element, i.e. your function has one argument, which you
may head). Do NOT write this function as a class
function in some List class.
-
(10 points) Given the graph:
-
(2 points)Write the adjacency list representation for the
graph. Assume that the nodes are inserted in the vertex array in
alphabetical order, that the information stored with each vertex
is its one-letter "name", and that the adjacency lists are stored
in sorted order (increasing) of the vertex number.
- (2 points) Write the adjacency list representation for the
underlying undirected
graph. Assume that the nodes are inserted in the vertex array in
alphabetical order, that the information stored with each vertex
is its one-letter "name", and that the adjacency lists are stored
in sorted order (increasing) of the vertex number.
-
(3 points) Starting from vertex A, list the vertices of the
original directed graph in the
postfix order in which they are visited by a depth-first search
algorithm. Assume that the visit function lists the data
in each node (not the index of the vertex) as it visits it.
-
(3 points) Starting at vertex A, list the vertices of the
underlying undirected graph in the order in which
they are visited by a breadth-first search algorithm.
Assume that the visit function lists the data
in each node, as it visits it.
-
(8 points) What is the following recursive function computing? You can
answer with a mathematical formula or explain in English (in no more than
two lines) what it is doing.
int funct(int a[], int n)
{ if (n == 1) return(a[0]);
else return ( a[n-1] + funct(a,n-1));
}
Now trace the stack behavior for the following function call:
funct(b, 3) , where the array b contains the numbers
2,5,3 stored in this order.
-
(6 points) Sort the list of numbers {5, 7, 2, 6, 8, 4, 1, 3, 9} 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 the winter break :-)
Ileana