public class BinarySearchTree
extends java.lang.Object
TreeSet
s, which can only tell if a value is in the tree but
cannot find the nearest value.
This is useful when dealing with user input that may not be exact, as it effectively clamps the input value to the nearest value in the tree.
Note: This tree structure does not currently support inserting or removing values. The tree's contents are strictly tied to a list of input values supplied to the constructor.
Constructor and Description |
---|
BinarySearchTree(java.util.List<java.lang.Double> contents)
The default constructor.
|
Modifier and Type | Method and Description |
---|---|
int |
findNearestIndex(double d)
Finds the index of the value in the tree closest to the specified value.
|
double |
findNearestValue(double d)
Finds the value in the tree closest to the specified value.
|
double |
get(int i)
Gets the item at the specified index.
|
int |
size()
Gets the size of the tree.
|
public BinarySearchTree(java.util.List<java.lang.Double> contents) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
contents
- The list of numbers from which to create a binary tree.java.lang.NullPointerException
- If the specified list is null
.java.lang.IllegalArgumentException
- If the specified list contains zero non-null values.public int findNearestIndex(double d)
d
- The value to search for in the tree.public double findNearestValue(double d)
d
- The value to search for in the tree.public double get(int i) throws java.lang.IndexOutOfBoundsException
i
- The index of the requested element in the tree.java.lang.IndexOutOfBoundsException
- If the specified index is invalid.public int size()