Professional Documents
Culture Documents
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”.
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:
Exercise 2: Now, remove the comment markers surrounding the following block of
statements:
/**
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:
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:
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 …