Computer Science 112a
Fall 2004
Ileana Streinu

CS 112
Final Examination
(Self-scheduled)


Name:

112a-ax account:

The exam is open book: you may consult your notes, books. But all the work should be yours.

Show all your work: you may get partial credit. Add extra sheets if necessary.


  1. (10 points) Allocation of memory for variables. Given the following Java program, specify where each variable will be allocated, and when. Write the answers next to each variable in the program.

    class Test{

    static int x,y,z;

    public static void main()

    int a,b;
    a=func(x,y,z,b);
    }
    int func(int a,b,c,d)
    {
    int x,y;
    ...
    }
    }

  2. (5 points) Stacks and Queues. Give a convincing but short argument for or against using stacks to store:

  3. (20 points) Definition, allocation and access of variables in Java.
  4. (37 points) Java and C++ pointers.
    Given the following C++ class definition:

    class BNode{
    int x;
    BNode* y;
    BNode* z;
    };

    and the following definitions of variables:

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


  5. (8 points) Recursion. Write a short recursive Java function (NOT a whole program!!!) to compute the minimum element in a binary search tree.

  6. The program should use the representation of a binary (search) tree as a pointer to the root node, where a tree node is a Java structure defined as:

    class TreeNode{
    int data;
    TreeNode left;
    TreeNode right;
    }

    Name your function minTree. It should take as argument the root and return an integer (the minimum value). Design the function as a separate entity, not as part of some binary tree class.

    If the recursive definition seems elusive but you have other ideas for solving this problem, I also accept other solutions. Warning: an iterative solution may not be easy to produce.
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     


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

    main()
    {
    int a = func(2,2);
    System.out.println(a);
    }


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

     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     


  8. (6 points) Graphs. Given the graph:

  9.  
     
     
     
     
     

  10. (3 points) Tree Traversal. Given the following binary tree:
  11. Write down the vertices of the tree in prefix, infix and postfix order.
     
     
     
     
     
     
     
     
     
     


  12. (6 points) Binary Search Trees and Sorting. Sort the list of numbers {15, 3, 12, 5, 1, 14, 2, 9, 7} using binary search trees.

  13.  
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    Enjoy your winter break :-) Ileana