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).


  1. (3 points) Write the arithmetic expression tree for the following arithmetic expression: (((2+3)-(2*5))/(3-(4+5)))
























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


















  3. (9 points)
























  4. (15 points) Given the following code:

  5. 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;
    }
     


  6. (34 points) Given the following structure definition:

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


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



































  9. (10 points) Given the graph:



     
     
     
     
     
     


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


















































  11. (6 points) Sort the list of numbers {5, 7, 2, 6, 8, 4, 1, 3, 9} using binary search trees.
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    Enjoy the winter break :-) Ileana