Java has a generic interface called Comparable. A class that implements the Comparable interface must have a
Question:
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.
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, treeSize is wrong. Specification Constructor for the BTNode public BTNode(E initialData, BTNode initiallLeft, BTNode initialRight) Initialize a node with specified initial data and links to children. Note that a reference to a child may be null, which indicates that there is no child. Parameters: initialData - the initial data of this new node initialleft and initialRight- references to the children of this new node Postcondition: This new node contains the specified data and links to its children. getData-getLeft-getRight public E getData( ) public BTNode getleft( ) public BTNode getRight( ) These are accessor methods to obtain this node's data or a reference to one of the children. Any of these objects may be null. A null reference to a child indicates that the child does not exist. getLeftmostData public E getleftmostData( ) Accessor method to get the data from the leftmost node of the tree below this node. Returns: The data from the deepest node that can be reached from this node following left links. getRightmostData public E getRightmostData( ) Accessor method to get the data from the rightmost node of the tree below this node. Returns: The data from the deepest node that can be reached from this node following right links.
Step by Step Answer:
Heres an implementation of the ComparableTreeBag class as described in the question public class ComparableTreeBag private BTNode root private int size public ComparableTreeBag root null size 0 public ...View the full answer
Students also viewed these Computer science questions
-
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...
-
If you are familiar with Javas Comparable interface (Programming Project 11), then rewrite one of the sorting methods so that it sorts an array of Comparable objects. You may choose selectionsort,...
-
In mathematics, a magma is a type of algebraic structure that has a set of elements, all of the same type, and one binary operation. Such a pairing of a set (ex. Positive integers) with this...
-
Jerome Neeson is a shareholder in Gourmet Chefs Inc., a company that owns and operates a test kitchen in a large metropolitan area. The company is involved in a number of businesses, including...
-
Weights of bears (use 11 classes with a class width of 50 and begin with a lower class boundary of 20.5). a. Construct a histogram. b. Describe the general shape of the distribution, such as...
-
Define a bus.What are buses used for?
-
Use invariants to find the optimum lamina orientation for maximizing the shear stiffness \(\bar{Q}_{66}\), then find the corresponding maximum shear stiffness in terms of invariants.
-
High school seniors with strong academic records apply to the nations most selective colleges in greater numbers each year. Because the number of slots remains relatively stable, some colleges reject...
-
Santa's Castle sells all types of Christmas decorations. On November 1 , 2 0 X 1 , Santa's Castle purchased 5 0 0 ornaments from a supplier for $ 9 0 0 . On November 2 , it spent $ 5 0 transporting...
-
Gulf Real Estate Properties, Inc. is a real estate firm located in southwest Florida. The company, which advertises itself as "expert in the real estate market," monitors condominium sales by...
-
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 strings compareTo method, which is described on page...
-
Using a heap, implement the priority queue ADT from Section 7.4. You can store the heap in arrays, similar to the solution to Self-Test Exercise 1. To have FIFO behavior for elements with equal...
-
How does Yammer capture BI from employees?
-
(Multiple choice) The statement (setting one element equal to the next element) in a client program of the queue class 1. would cause a syntax error at compile time. 2. would cause a run-time error....
-
The stack is implemented as a class containing an array of items, a data member indicating the index of the last item put on the stack (top), and two Boolean data members, underFlow and overFlow. The...
-
17. 1. Change the specifications for the array-based Unsorted List ADT so that PutItem throws an exception if the list is full. 2. Implement the revised specifications in (a). Statements int value;...
-
Using one or more stacks, write a code segment to read a string of characters and determine whether it forms a palindrome. A palindrome is a sequence of characters that reads the same both forward...
-
One queue implementation discussed in this chapter dedicated an unused cell before the front of the queue to distinguish between a full queue and an empty queue. Write another queue implementation...
-
Alice J. and Bruce M. Byrd are married taxpayers who file a joint return. Their Social Security numbers are 123-45-6789 and 111-11-1112, respectively. Alice's birthday is September 21, 1968, and...
-
Revol Industries manufactures plastic bottles for the food industry. On average, Revol pays $76 per ton for its plastics. Revol's waste-disposal company has increased its waste-disposal charge to $57...
-
Calculate (1.666015625 10 0 1.9760 10 4 ) + (1.666015625 10 0 -1.9744 10 4 ) by hand, assuming each of the values are stored in the 16-bit half precision format described in Exercise 3.27 (and...
-
Based on your answers to 3.38 and 3.39, does (1.666015625 10 0 1.9760 10 4 ) + (1.666015625 10 0 -1.9744 10 4 ) = 1.666015625 10 0 (1.9760 10 4 + -1.9744 10 4 )?
-
Using the IEEE 754 floating point format, write down the bit pattern that would represent -1/4. Can you represent -1/4 exactly?
-
Convert the following ERDs to a Database schema. Follow all the steps in order as learned in this class. Identify PKs and FKs for each relation. Explain your work. Press esc to exit full screen Page...
-
How to draw a database in MS SQL Server for an entity with only a multivalued attribute and no primary key?
-
use Adventure Works Database Run each query and save the notebook with the results before submitting it. All the questions below are related to AdventureWorks database. make sure your notebook is...
Study smarter with the SolutionInn App