CPS 100 FALL 1996: Assignment #5

Due: Friday, Nov. 15, by 8am

Last Date to Turn in: Tuesday, Nov. 19 by 8am

40 points

This assignment will use an animation tool called the Xtango animator that runs on X windows. This program will run on any of the Sparc 5's in the clusters.

In your cps100 directory, create a directory called assign5 using the mkdir command. Change into the assign5 directory.

In order to do this assignment you will need to link to the Xtango animator. In your assign5 directory, type

   ln -s ~rodger/bin/animator  anim

You'll also need to copy the following setup file. Type:

   cp   ~rodger/bin/setup  setup

Each time you login you will need to type "setup" in your assign5 directory to setup the X environment so you can run the Xtango animator.

For the programming problem that follows, you should use the style rules discussed in class, which includes meaningful variable names, indentation, and comments (pre and postconditions) at the top of the file and for each function. Also include your name, date, course, and purpose in a comment at the top of the program.


Problem: Tree Animation

You are to process and animate the following tree commands.

  1. insert x

    (where x is an integer). Insert the number x into a binary search tree. If x is to be inserted at a level number greater than 5 or x is a duplicate, then do not insert x. In this case, print an informative message as to why x was not inserted.

  2. find x y

    (where x and y are integers). Find the lowest common ancestor of x and y in the tree. Any two nodes in a tree have one or more common ancestors. The lowest common ancestor is the one that is closest to the nodes. If one of x and y is not in the tree, print an error message.

  3. mintree

    At this point, reorganize the data values into a min tree. Each node in a min tree is smaller than the nodes in both its subtrees. You may assume there will be no more insert or find commands entered after mintree is entered.

    The reorganized tree must have the same structure as the binary search tree. Reorganizing means you can swap data values that are not in the correct order, but DO NOT tear the tree down and rebuild it from scratch.

  4. Extra Credit (4pts): deletemin

    Delete the minimum node in the tree. You may assume this command only appears after the mintree command. After the deletion, the tree should still be a mintree.

Examples:

Sample input and output files are in ~rodger/cps100/assign5. The sample input file does not test the find command, you'll have to create your own data file for this.

Two output files have been created, tree.out and tree2.out (from tree.in and tree2.in). To see what they look like on the animator, type

    anim < tree.out

You should slow down the animation by moving the scroll bar on the far right down to the bottom. Then click on the run animation button.

Requirements

  1. The tree should be implemented using nodes and pointers. For example, a tree Node might be represented as:

      struct Node
      {
         int data;
         Node * left;
         Node * right;
      };
    

  2. You should write a class for this assignment. One possibility is that you have a private variable that is a pointer to your tree, and member functions apply the operations above to the tree.

  3. The name of your executable should be called tree

  4. You will need to create your own Makefile. See Makefiles for previous assignments to get started (copy and modify). Your Makefile should be set up so that typing make tree compiles all your .cc files corresponding to this program and creates the executable called tree

  5. Your program should use standard input and output. If you have a data file called data1 and you want the new sorted file to be called out1, then you should run your program by typing:

           tree <  data1  >  out1
    

    Thus, cin will automatically read in from the file data1, and cout will automatically write to the file out1.

    See above for running the animator on your output file.

  6. You should have a file called README that contains your name, date, an estimate of how long you worked on the program, and a list of people you received help from in doing this assignment. Please REREAD the COLLABORATION policy on the syllabus.

  7. Your program should follow the style rules mentioned above and should be modular, that is, containing many functions, each with a different purpose.

Input

See the sample input files, tree.in and tree2.in in ~rodger/cps100/assign5

NOTE: These sample files don't show an example of the find command.

Output

Output should be an animation of the tree operations. Output from your program should be suitable for the Xtango animator. See the handout entitled Animator Commands for information on the format of your output.

Your animations do not have to look exactly like mine. Feel free to jazz them up, add more delays, etc. The examples are in color, but you don't have to use color. Using the color black should work on black and white screens.

Submitting Program

When your programs compile and produce the correct output, create a "README" file (please use all capital letters). Include your name, the date, and an estimate of how long you worked on the assignment in the "README" file. You must also include a list of names of all those people (students, prof, tas, tutor) with whom you consulted on the assignment. See the rules for collaboration in the CPS 100 syllabus.

To submit your programs electronically type (where file1 file2 ... are all the .cc and .h files needed by your program):

   submit100 assign5 README Makefile file1 file2 ...

You should receive a message telling you that the program was submitted correctly. If it doesn't work try typing ~rodger/bin/submit100 in place of submit100 above.