Computer Science 112b
Spring 2001
Ileana Streinu

CS 112
Final Examination
(Self-scheduled)


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.


  1. (10 points) Given the following code:

  2. struct SpecialNode{
    int info;
    SpecialNode* parent;
    SpecialNode* child;
    };

    main()
    {
    SpecialNode a1, a2, a3, a4;
    SpecialNode* b;
    b = &a1;
    a1.info = 1; a2.info = 2; a3.info = 3; a4.info = 4;
    a1.parent = new SpecialNode;
    a2.parent = &a1;
    a3.parent = &a2;
    a4.parent = &a1;
    b->parent->parent = NULL;
    b->parent->info = 0;
    b->child = &a2;
    }
     


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

  4. struct Node{
    int a;
    Node* b;
    Node* c;
    };

    and the following definitions of variables:

    BNode n1, *n2, n3[5];
    BNode* n4[10];

    Which of the following is a valid C++ expression? (2 points each) Cross the invalid expressions.
     

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


  5. (12 points) Write a short recursive C++ function (NOT a whole program!!!) to compute the depth of a node of a binary tree.

  6. The depth of a node is the length of the longest path from the node to a leaf which is a 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;
    TNode* left;
    TNode* right;
    }

    Name your function depth. It should take as argument a tree and return an integer.
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     


  7. (10 points) Trace the stack behavior for the following recursive function call. What is the value printed in the main function?

    main()
    {
    cout << f(2,2);
    }


    int f(int a, int b)
    { if (a==0 || b== 0) return 1;
    return f(a-1,b)+f(a,b-1);
    }

     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     


  8. (5 points) Describe the structure (header file) of a templated C++ class for a binary search tree with a generic type of data in the tree node. In the testdriver, you will test it for two data types (e.g. int and char). Assuming that the file is called Tree.h, the corresponding implementation file is Tree.cpp and the testdriver is testTree.cpp, describe which file is included in which one, and give the structure of the Makefile for compiling this code.
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     


  9. (5 points) Write a short C++ function to delete a node in a binary tree. The node is given as a parameter to the function by a pointer called current. Your function should deallocate all nodes that would otherwise become inaccessible. Describe the complete code, do not simply invoke some function you know of from one of the homework assignments.
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     


  10. (8 points) Given the graph:

  11.  

     
     
     
     
     
     
     
     
     
     
     
     
     
     
     


     
     
     
     
     
     


  12. (6 points) Given the following binary tree:

  13.  

     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     

    Write down the vertices of the tree in preorder (2 points), inorder (2 points) and postorder (2 points).
     
     
     
     
     
     
     
     
     
     


  14. (10 points) Sort the list of numbers {5, 7, 2, 6, 8, 4, 1, 3, 9} using binary search trees.

  15.  
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     


    Enjoy your summer :-) Ileana