CSC 311

Winter 2002

Sample final exam

  1. Write a templatized, recursive version of binary search.  
  2. You are to define and implement a class hierarchy of railroad cars.  First define the base class Freightcar.  It should maintain a freight car's road number (a string) and its year of construction (unsigned integer).  Provide a constructor with two parameters, one for each member variable and each with a default value.  Also provide functions for computing and returning the volume of a freight car and a function for printing the road number of the car.
  3. Next define each of the derived classes: Flatcar, Boxcar, and Tanker.  In addition to the information in the Freightcar class, each of these has the following.

    Also, for each, provide a constructor with parameters for each member variable, a function for computing and returning the volume, and a function for printing.  When printing a Boxcar, print the road number and the volume surrounded by square brackets; when printing a Flatcar, print the road number and the volume surrounded by underscores; and when printing a Tankcar, print the road number and the volume surrounded by parentheses.  

    Be careful to use virtual functions!

  4. Implement the member functions of these classes.
  5. Write a function for printing the data in a binary search tree (implemented with the binary_tree_node class) in ascending order. 
  6. Write a function for printing the data in a binary tree in order by levels, that is, it prints the the root node first, the nodes on the second level next, the nodes on the third level next, and so on.  Hint: Use a queue.  As you print the data in each node, push its children on the queue.
  7. Write a function that takes as parameters a pointer to the root of binary search tree and a value and returns true if that value is in the tree and false otherwise.
  8. Write a function that takes as a parameter a pointer to the roof of a binary tree and an array.  It fills the array with data in the tree by placing the data in the root in the first entry of the array, the data in the root's left child in the second array entry, the data in the root's right child in the third array entry, and so forth.