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

[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Labs] [Assignments] [Examples] [Bailey Docs] [SamR Docs] [Tutorial] [API]


Lab 14: Implementing and Traversing Trees

Held: Tuesday, April 14, 1998 and Wednesday, April 15, 1998.

Overview: In this lab, you will develop and use a simple binary tree implementation. You will experiment with construction, output, and traversal of your binary trees.

Purpose: The primary goals of this assignment are to strengthen your understanding of implementation and usage issues in trees.

Library Files:

Notes: This is intended to be a two-day lab. On the first day, you should do experiments one and two. On the second day, you should do the experiment three.

Experiments

Name:
Email:

Experiment 1: Preparation

Step 1. In this lab, you'll be developing a simple implementation of binary trees. Rather than creating your own nodes, you will extend an existing BinaryNode class. Read through the documentation for the BinaryNode class. Note which functions you expect to be useful and list any questions you may have.

Step 2. Create a TreeNode class which extends the BinaryNode class. For now, put nothing in your class body. Compile your code to ensure that you have correctly extended the original class.

Write your code here.

Step 3. Write a unary constructor for your class. The constructor should take one parameter, an object, and should create a tree node with the given object as its contents. It is likely that your constructor will call super() with appropriate parameters.

Write your code here.

Step 4. Write a trinary constructor for your class. The constructor should take an object and the left and right subtrees and should create a tree node with the given object and subtrees. It is likely that your constructor will call super() with appropriate parameters.

Write your code here.

Step 5. Using your constructors, create the tree

    A
   / \
  B   C
 / \   \ 
D   E   F

Write the code you used to create the tree here. Note that while you can compile your code, there is no easy way to verify that it worked correctly.

Experiment 2: Converting Trees to Strings

In order to verify that our tree operations work correctly, it is useful to find a way to print out trees. As you know, the basic Java model for printing objects is to convert them to strings and then allow someone else to print these strings.

The easiest way to print trees is to print the current node followed by the left and right children in parentheses. For the tree above, we might write

A(B(D,E),C(F))

Another alternative is to use indentation to indicate levels of the tree. For the tree above, we might write

A
  B
    D
    E
  C
    F

Step 1. Add a toSimpleString() method that creates a string representation of the tree in the simple form mentioned above.

Write your code here.

Step 2. Print out the tree you created earlier. Copy the output here.

Step 3. Build and print out the following trees.

       A      A
      /        \
     B          B
    / \        / \
   C   D      C   D
  /                \
 E                  E

Copy the output here.

Step 4. Do your answers to the previous assignment make you question any part of the design of our string output? If so, what?

Step 5. Write a toIndentedString() method that creates an "indented" string representation of the tree as described above.

Write your code here.

Step 6. Print the trees you created previously. Write the output here.

Step 7. [This exercise is optional.] Write a method that converts a tree to an appropriate two-dimensional representation of the tree, similar to the ones I've used in the examples above.

Write your code here.

Experiment 3: Efficient Representation

Often, tree structures are represented internally by non-tree structures. For example, the tree

     A
   /   \
  B     B
 / \   / \
C   C C   C

might be more efficiently represented as
    A
   ( )
    B
   ( )
    C

Step 1. Develop code that generates the second tree. Write your code here.

Step 2. Print out the tree you've created, using one of the print routines from experiment 3. Write the results here.

Step 3. One disadvantage (or, possibly, advantage) or the "non-tree" strategy is that loops are possible. Create a node which is its own child. Write the code for doing so here.

Step 4. What do you think will happen when you try to print out this node?

Experiment 4: Traversal

As you may know, there are a number of methods of traversing trees (visiting, printing, or returning, each of the nodes in the tree). Three primary criteria govern a traversal strategy:

Consider the following tree,

    A
   / \
  B   C
 / \   \ 
D   E   F

Step 1. Write a method that creates a string corresponding to the depth-first, preorder, left-to-right traversal of the tree. Write your method here.

Step 2. Write down the output of your method on the sample trees from experiment 2

Step 3. Write a method that creates a string corresponding to the depth-first, postorder, left-to-right traversal of the tree. Write your method here.

Step 4. Write down the output of your method on the sample trees from experiment 2

Step 5. Write a method that creates a string corresponding to the depth-first, inorder, left-to-right traversal of the tree. Write your method here.

Step 6. Write down the output of your method on the sample trees from experiment 2

Step 7. Write a method that creates a string corresponding to the breadth-first, preorder, left-to-right traversal of the tree. Write your method here.

Note that this method may be signficantly different than the other methods. It should be possible to write the other methods recursively. Writing this method may require more creativity..

Step 8. Write down the output of your method on the sample trees from experiment 2

Experiment 5: Traversal, Revisited

Revise your methods from experiment 4 to create iterators rather than strings. Time limitations have prevented more detail on this part of the lab.

Finishing Up

Read over your answers and, if you think it's appropriate to do so, click on the button below.


[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Labs] [Assignments] [Examples] [Bailey Docs] [SamR Docs] [Tutorial] [API]

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.

Source text last modified Wed Apr 15 07:54:17 1998.

This page generated on Wed Apr 15 07:59:08 1998 by SiteWeaver.

Contact our webmaster at rebelsky@math.grin.edu