Expand the class from Project 10 or 11 so that there is an extra method that produces

Question:

Expand the class from Project 10 or 11 so that there is an extra method that produces a Java Iterator for the bag.


Data from Project 10

Write a class for a bag of strings, where the strings are stored in a binary search tree. In creating the binary search tree, you should use the string’s compareTo method, which is described on page 499. The tree itself should use the BTNode class from Figure.

FIGURE 9.10 Specification and Implementation of the Generic Binary Tree Node Class Generic Class BTNode * public class BTNode from the package edu.colorado.nodes A BTNode provides a node for a binary tree with a reference to an E object as the data in each node. Limitations: Beyond Int. MAX_VALUE elements,

Data from Project 11

Java has a generic interface called Comparable. A class that implements the Comparable interface must have a method with this specification:

♦ compareTo
public boolean compareTo(E obj)
Compare this object to another object of the same type.

Returns:
The return value is zero if this object is equal to obj; the return value is negative if this object is smaller than obj; the return value is positive if this object is larger than obj.

Throws: ClassCastException
Indicates that obj is the wrong type of object to be compared with this object.

Write a generic class for a bag of Comparable objects, where the objects are stored in a binary search tree. The tree itself should use the BTNode class from Figure 9.10.

The first line of your new bag class should be: 

public class ComparableTreeBag

This tells the Java compiler that the Comparable- TreeBag is a generic class, but that any instantiation of the generic type parameter E must implement the Comparable interface.

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question
Question Posted: