You are on page 1of 3

UPR-Mayaguez

ICOM 4035 – Ejercicio de Laboratorio1


Trees: General and Binary Trees

1. Introduction You have received a Java project which contains implementations for
different generic collections of objects of type Position. An object of this type is used to
hold one of the elements in the collection, hence, the collection holds one object of type
Position for each one of its element. These collections include: PositionList (see
page 235 in textbook), Tree (see page 270), and BinaryTree (see page 284). The package
called positionInterfaces contains Java interfaces specifying these collections. The
package positionListLLDirect contains an implementation of a PositonList
based on a linked list. The package positionTree contains the classes: GTree and
LinkedBinaryTree. The first class is an abstract class. The second class is a subclass of
the first one. The implementations given are slightly different from the ones discussed in the
textbook. In addition, there are other auxiliary classes with clear purposes.

In this lab session, you will work with these implementations to implement some new classes
for particular types of trees.

2. Exercises Class GTree has two abstract methods: root() and size(). All the
other methods specified for the Tree ADT are implemented in GTree. This class has no
instance field. Observe that the class LinkedBinaryTree has two instance fields: root and
size. It implements the two abstract methods in GTree as well as the methods specified in
BinaryTree interface.

As you can see, there is a class called LinkedBinaryTreePOI inside the package
positionTree. This class has just one method: fillIterator, whieh overrides the
corresponding method in class GTree. Its purpose is to “fill” the iterator for objects of type
LinkedBinaryTreePostOI following the “post-order” mode (post-order iterator). Note
that the corresponding method in class GTree performs a similar action, but in “pre-order
mode”.

The next tree is used in the exercises:

1
Prof. Pedro I. Rivera Vega, ECE Department, UPR-Mayaguez, p.rivera@upr.edu

1
Exercise 1: In package treeTesters there is a partial implementation of the class
LinkedBinaryTreeTester. The purpose of the method inside (main method) is to generate
binary trees and to show the elements in the order that they are accessed through the iterator
that applies to the particular instance (see method showTreeElements). Based on the
operations in classes GTree and LinkedBinaryTree, add the necessary instructions to
construct, as the object that t1 references to, the tree shown in the figure. Once the tree is
correctly built, the output produced by the tester class should be:

The tree has 13 elements. These are:


4 9 7 10 20 15 12 17 19 21 40 30 45
The tree has 0 elements. These are:

You cannot make changes to any of the other classes.

Exercise 2: Now, remove the comment markers surrounding the following block of
statements:

/**

LinkedBinaryTreePOI<Integer> t2 = new LinkedBinaryTreePOI<Integer>();


// ADD NECESSARY INSTRUCTIONS TO CONSTRUCT A NEW COPY OF THE SPECIFIED TREE...

showTreeElements(t2);

**/

Then add the necessary instructions to build a new object, this time of type
LinkedBinaryTreePostOI, and assign it to t2. Then, run the program again. The
output shall be:

The tree has 13 elements. These are:


4 9 7 10 20 15 12 17 19 21 40 30 45
The tree has 0 elements. These are:

The tree has 13 elements. These are:


7 10 9 12 19 17 15 30 45 40 21 20 4

Exercise 3: Implement a class named: LinkedBinaryTreeInOI, which constructs the


iterator in “in-order” mode. Then, modify the tester to add a new tree, t3, of this type,
construct a copy of the same tree used above and show the content. This time, the output shall
be:

The tree has 13 elements. These are:


4 9 7 10 20 15 12 17 19 21 40 30 45
The tree has 0 elements. These are:

The tree has 13 elements. These are:


7 10 9 12 19 17 15 30 45 40 21 20 4

The tree has 13 elements. These are:


7 9 10 4 12 15 19 17 20 21 30 40 45

2
Exercise 4: For this exercise, you need access to an implementation of the Queue ADT.
You should have one from the previous lab session. Implement a class named:
LinkedBinaryTreeBFS, which constructs the iterator in “breadth-first-search” mode.
Then, modify the tester to add a new tree, t4, of this type, construct a copy of the same tree
used above and show the content. This time, the output shall be:

The tree has 13 elements. These are:


4 9 7 10 20 15 12 17 19 21 40 30 45
The tree has 0 elements. These are:

The tree has 13 elements. These are:


7 10 9 12 19 17 15 30 45 40 21 20 4

The tree has 13 elements. These are:


7 9 10 4 12 15 19 17 20 21 30 40 45

The tree has 13 elements. These are:


4 9 20 7 10 15 21 12 17 40 19 30 45

Exercise 5: Create a new class as in the previous exercise, but this time, use a stack
implementation to fill the iterator, instead of a queue….

3. FIN …

You might also like