Computer Science 112b
Spring 1999
Ileana Streinu
CS 112
Final Examination
Name:
112b-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. Add extra sheets if necessary.
- (3 points) Write the postfix form for the following arithmetic expression:
(((a-b)+(c*d))+((a+c)-b))
- (3 points) What is the value of the following arithmetic
expression written
in postfix form (assumming that we only use one-digit decimal integers):
8321+*-51+*
- (9 points)
- (3 points) Draw the double linked list structure, using the box notation,
for the following list: {A, C, D, F}, and give the struct
definition for a node of this list.
- (3 points) Show the modification of the links when the new
item E is inserted
in alphabetical order in this list (i.e. after the element
D). 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.
- (3 points)
Show the modification of the links when the item A is removed from
the list, and write the C++ instructions that perform this
deletion.
- (5 points) Describe the structure of the Makefile for a C++
object-oriented program designed to do the following
- initialize a queue of integers and a queue of characters.
- will ask the user to choose whether she wants to work with
integers or characters.
- then will sit in a loop, at each iteration asking the user
what to do next: add, remove or print the queue.
- (10 points) Given the following code:
struct ANode{
int info;
ANodePtr parent;
};
typedef ANode* AnodePtr;
main()
{
ANode a1, a2, a3, a4;
ANodePtr b;
b = &a1;
a1.info = 1; a2.info = 2; a3.info = 3; a4.info = 4;
a1.parent = new ANode;
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 ANodes 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.
- (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
depth of all the nodes of a binary tree.
The depth of a node is the length of the longest path from the
node to a leaf which is descendent of the node.
The program should use the representation of a tree as a pointer to a tree
node, where a tree node is a C++ structure defined as:
struct TNode{
int data;
int depth;
TNodePtr left;
TNodePtr right;
}
typedef TNode* TNodePtr;
Name your function depth.
It should take as argument a tree. It should check
to see whether the tree is empty (i.e. if it is the NULL
pointer).
If not, it should
look at the right and left subtrees, compute the depths of all
their nodes, then
select the largest depth of its two children
and add one more unit to it. Using this outline, write the
C++ code for the function depth.
- (8 points) Given the graph:
- (2 points)Write the adjacency list representation for the graph, with each
adjacency list sorted increasingly by the vertex number.
- (3 points) Starting from vertex 1, list the vertices of the graph
in the postfix 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 preorder (2 points),
inorder
(2 points) and postorder (2 points).
- (10 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).
Ileana Streinu