Computer Science 112b
Spring 1999
Ileana Streinu

CS 112
Final Examination


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.

  1. (3 points) Write the postfix form for the following arithmetic expression: (((a-b)+(c*d))+((a+c)-b))

  2. (3 points) What is the value of the following arithmetic expression written in postfix form (assumming that we only use one-digit decimal integers):


  3. (9 points)

  4. (5 points) Describe the structure of the Makefile for a C++ object-oriented program designed to do the following

  5. (10 points) Given the following code:

    struct ANode{
    int info;
    ANodePtr parent;
    typedef ANode* AnodePtr;

    ANode a1, a2, a3, a4;
    ANodePtr b;
    b = &a1; = 1; = 2; = 3; = 4;
    a1.parent = new ANode;
    a2.parent = &a1;
    a3.parent = &a2;
    a4.parent = &a1;
    b->parent->parent = NULL;
    b->parent->info = 0;

  6. (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.

    1. n1 -> a
    2. n1.a
    3. n2.a
    4. n2->a
    5. n1.b.b
    6. n1.b -> b
    7. n2.b -> b
    8. n2->b ->b
    9. n2-> b . b
    10. n3[2].c
    11. n3[2].c ->a
    12. n3[2].c.a
    13. n3[2]->c->a
    14. n4[5].b->a
    15. n4[5]->b->a
    16. n4[5]->b->c->a
    17. n4[2]->c->b->b.a

  7. (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. (8 points) Given the graph:

  9. (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. (10 points) Sort the list of numbers {5, 7, 2, 6, 8, 4, 1, 3, 9} using binary search trees.

    Ileana Streinu