Question: I have this to do in java and no matter how much i read this I am not understanding trees. I have the code but
I have this to do in java and no matter how much i read this I am not understanding trees. I have the code but it keeps telling me that I am asking more than one question. How do I get the code to you? Please help and explain as much as possible for me to hopefully understand this. THANK YOU FOR ANY AND ALL HELP!!!
Modify the BST class to use a Comparator for comparing objects. Name your new class ComparatorTree.
We will change the books instructions slightly: Leave ComparatorTrees generic type parameter as extending Comparable. So dont change
public class ComparatorTree
Test your solution with either ComparatorTreeTester class with a main method, or a JUnit test class. Your tests should include at least the following types of tests:
1. use the natural ordering of String elements
2. use a Comparator to order String elements by their reversed value (hint: use a StringBuilder)
3. use a Comparator to order Integer elements by their toString values.
4. use a Comparator order Integers by their negated values. So if you wanted to order 5 and 6, you would actually compare -5 to -6 to determine the order
Objectives
By the end of this assignment the student will have
Instrumented an implementation of a binary tree with a Comparator
Declared and used a strategy design pattern (Comparator)
Enabled client control of a binary trees ordering mechanism
Tested several different ordering algorithms and observed their effects
Declaring and Initializing the Comparator Instance Variable
You need to add and initialize an instance variable of type Comparator
if (e.compareTo(current.element) < 0)
would be replaced by
if (comparison.compare(e, current.element) < 0)
The following sections show three options in declaring and initializing this instance variable. Other possibilities exist. These three examples are functionally equivalent, and differ only in syntax.
Using a Named Inner Class
// using named inner class
private Comparator
private class MyComparator
@Override
public int compare(E o1, E o2) {
return o1.compareTo(o2);
}
}
Using an Anonymous Inner Class
// using anonymous inner class
private Comparator
@Override
public int compare(E o1, E o2) {
return o1.compareTo(o2);
}
};
Using a Lambda Expression
// using a lambda expression
private Comparator
Grading Elements
The new class is named ComparatorTree
ComparatorTree uses its elements natural ordering if no Comparator is supplied
If the user supplies a Comparator object, ComparatorTree uses it for all comparisons and orderings
ComparatorTree supports any element type implementing Comparable
ComparatorTree is tested with multiple generic types, multiple Comparators, and a natural ordering
ComparatorTree tester is in a separate class containing a main method or JUnit tests
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
