Computer Science 112b
Spring 2002
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 Node{
    int info;
    Node* parent;
    Node* child;
    };

    main()
    {
    Node a1, a2;
    Node* b, b3, b4;
    b = &a1;
    a1.info = 5; a2.info = 4;
    b3=new Node;
    b4=new Node;
    b3->info = 10; b4->info = 3;
    a1.parent = new Node;
    a2.parent = &a1;
    b3->parent = &a2;
    b4->parent = &a1;
    b->parent->parent = NULL;
    b->parent->info = 20;
    b->child = &a2;
    }
     


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

  4. struct BNode{
    int x;
    BNode* y;
    BNode* z;
    };

    and the following definitions of variables:

    BNode a1, *a2, a3[5];
    BNode* b4[10];

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

    1. a2-> y . z
    2. a3[2].x ->x
    3. a2->z
    4. a4[2]->z->y->y.z
    5. a1.y
    6. a2.z
    7. a3[2].x
    8. a1.y.z
    9. a1.y -> z
    10. a1 -> z
    11. a2.y -> z
    12. a2->y ->y
    13. a3[2].x.z
    14. b4[5].y->z
    15. b4[5]->y->z->z
    16. b4[5]->y->z->z->x
    17. a3[2]->z->y


  5. (12 points) Write a short recursive C++ function (NOT a whole program!!!) to compute the length of a list.

  6. The program should use the representation of a list as a pointer to a list node (head), where a list node is a C++ structure defined as:

    struct ListNode{
    int data;
    ListNode* next;
    }

    Name your function length. It should take as argument the head of a list and return an integer. Design the function as a separate entity, not as part of the List class.
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     


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

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


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

     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     


  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 plan to 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 code to allocate dynamically (node by node) a binary tree with 5 nodes. Write C++ statements to link the nodes into a tree of your choice.
    Then write statements to delete the whole tree.

    Clarifications: I am NOT asking you to write a C++ function to delete the tree. Nor should you use any of the functions you know of from one of the homework assignments. You just write C++ instructions to do these tasks, in this particular case.
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     


  10. (8 points) Given the graph:

  11.  

     
     
     
     
     
     
     
     
     
     
     
     
     
     
     


     
     
     
     
     
     


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

  13.  

     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     

    Write down the vertices of the tree in prefix order (2 points), infix order (2 points) and postfix order (2 points).
     
     
     
     
     
     
     
     
     
     


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

  15.  
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     


    Enjoy your summer :-) Ileana