Fundamentals of Computer Science II (CSC-152 99S)


Laboratory: Binary Trees

This laboratory is under development.

In this laboratory session, you will investigate a node-based implementation of trees. You will begin by considering the basics of the implementation (there are many code files to look at). You will then attempt to correct a few errors. Finally, you will consider how you might print out the elements of the tree in a variety of orders.

Required files:

Interfaces

Node-Based Binary Trees

For Testing

Miscellaneous


1. Getting Started

  1. Copy all the above files.
  2. Compile them while you're doing the following steps. (% jc *Tree* should do the trick.)
  3. Scan the two interfaces. How is the design of these binary trees similar to that we discussed in class? How is it different?
  4. How might you use these methods to create the following tree?
        A
       / \
      B   C
     /
    D
    
  5. Scan the node-based cursor implementation (NodeBasedBinaryTreeCursor.java). What happens when you add a child? What happens when you delete a node?
  6. Read through BinaryTreePrinter.java to familiarize yourself with the output format.
  7. How might this program print the tree you created in an earlier step?
  8. What does it mean when the printer prints a question-mark?
  9. Read through the tester and summarize the expected output after each step.

2. Initial Experiments

  1. Compile the various files (if you have not done so already).
  2. Run the tester. Does the output you get match the output you expected? If not, do you think it's a problem with your understanding or with the implementation?
  3. Summarize the problems and the methods that you think cause (or fail to address) the problems.

3. Correcting Errors

  1. As you may have noted, there is an error with the update to the depth of the tree. How might you correct that error? Try doing so.
  2. As you may have noted, there is an error with deletion in that the parent does not realize that a child has been deleted. This is a potential drawback with the design of the interface. Nonetheless, we will try to support it. Update the delete method in NodeBasedBinaryTreeCursor to correct this problem.

4. Alternate Printing

  1. As you may recall from our discussion of ``puzzle trees'', we may want to visit a tree in depth-first order (go as far as possible down branches) or in breadth-first order (go across each level before going on to the next level). Write the elements of the following tree in breadth-first and depth-first order.
          X
         / \
        Q   C
       / \   \
      D   A   E
             / \
            N   F
    
  2. Sketch an algorithm one might use for printing out the elements of a binary tree in depth-first order. You should rely on the methods given in the BinaryTree and BinaryTreeCursor classes.
  3. Sketch an algorithm one might use for printing out the elements of a binary tree in breadth-first order. You should rely on the methods given in the BinaryTree and BinaryTreeCursor classes.

History


Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/99S/Labs/binarytrees.html

Source text last modified Wed Apr 28 11:15:40 1999.

This page generated on Wed Apr 28 11:22:00 1999 by SiteWeaver. Validate this page's HTML.

Contact our webmaster at rebelsky@math.grin.edu