CS365

Practice Homework Set 1

  1. The first three questions in this homework give you some practice with generic objects in C++, Java, and C. The last question gives you some additional practice with Java's layout managers.

  2. Answers will be posted after Spring Break.

  3. The homework is optional and you will not hand it in.


  1. Suppose you are given the following public parts of a C++ class declaration for a binary tree that stores keys in alphabetical order:
         template<class Key, class Value>
         class BinaryTree {
           public:
             void insert(Key key, Value val);
    	 // returns true if the key is in the tree and false otherwise.
    	 // Positions the cursor to point to the tree node that contains the
    	 // key if such a node exists and otherwise to the first tree node
    	 // with a value greater than key
    	 boolean find(Key key);  
    
    	 // returns the value of the tree node pointed to by the cursor
    	 Value get();
    	 void delete(Key key);
         }
         

    1. Write a line of code that declares a variable, x, to be a BinaryTree with an integer key and a string (char *) value. The variable should be a value object (i.e., stack allocated).
    2. Write a line of code that declares a variable, y, to be a BinaryTree with a string (char *) key and a Payroll pointer value (assume Payroll is a class). The variable should be a pointer.
    3. Write a line of code that allocates a BinaryTree object off the heap and assigns it to y.
    4. Write a function that takes as inputs an integer key and a reference to a BinaryTree object that has an integer key and a standard template library string value (i.e., the value should be a string object from C++'s standard template library). The function should look up the integer key in the BinaryTree object and return the associated string if the integer key is found, and 0 otherwise.
    5. Write a line of code that calls your function and assigns the result to a standard template library string variable.

  2. Find the generic Java class that implements a Red Black tree and then write the following code fragments:

    1. Write a line of code that declares a variable, x, to be a Red Black tree with an integer key and a string value.
    2. Write a line of code that declares a variable, y, to be a Red Black tree with a string key and a Payroll value (assume Payroll is a class).
    3. Write a line of code that allocates a Red Black tree and assigns it to y.
    4. Write a function that takes as inputs an integer key and a reference to a Red Black tree object that has an integer key and a string value. The function should look up the integer key in the Red Black tree object and return the associated string if the integer key is found, and 0 otherwise.
    5. Write a line of code that calls your function and assigns the result to a string variable.

  3. Suppose you wanted to design a generic binary tree data structure written in C.

    1. Write a struct statement for a binary tree node. What restrictions, if any, do you need to put on the types of objects that can be used as key and value objects (hint: unless you are more clever than me there will be a restriction)?
    2. Write the function prototype for an insert function. Remember that because your tree nodes are generic, your insert function may require more information than an insert function that knows the types of its key and value objects. You do not need to write the function.

  4. Write a snippet of Java code that creates and adds two JButtons to a JPanel and then lays them out so that:

    1. The two buttons are separated by 10 pixels
    2. The two buttons are centered in the JPanel
    3. The two buttons are surrounded by a 10 pixel border on all sides.