Package examples
Interface BinaryTree<E extends java.lang.Comparable<? super E>>
- Type Parameters:
E
- type of data stored by this tree
- All Known Subinterfaces:
BalancedBTree<E>
public interface BinaryTree<E extends java.lang.Comparable<? super E>>
A collection implemented as a binary search tree. Stored values must
implement the
Comparable
interface, and will be stored
in the tree according to their natural ordering.-
Method Summary
Modifier and Type Method Description boolean
add(E element)
Add a value to the tree.boolean
has(E element)
Check whether or not the tree contains the given element.int
height()
Measure the height of the tree.E
max()
Find the largest value stored in the tree.E
min()
Find the smallest value stored in the tree.boolean
remove(E element)
Remove a value from the tree.TreeNode<E,?>
rootNode()
Get the node at the base of this tree.int
size()
Count the number of distinct values stored in the tree.
-
Method Details
-
rootNode
Get the node at the base of this tree.- Returns:
- the root node, or null if the collection is currently empty
-
has
Check whether or not the tree contains the given element.- Parameters:
element
- the value to search for- Returns:
- true if element is in this collection; false otherwise
-
min
Find the smallest value stored in the tree.- Returns:
- the minimum value in this collection
- Throws:
java.util.NoSuchElementException
- if the tree is empty
-
max
Find the largest value stored in the tree.- Returns:
- the maximum value in this collection
- Throws:
java.util.NoSuchElementException
- if the tree is empty
-
add
Add a value to the tree. Duplicates are ignored.- Parameters:
element
- new value to add- Returns:
- false if the collection already contained the given element; true otherwise
-
remove
Remove a value from the tree.- Parameters:
element
- value to remove- Returns:
- true if the collection contained the given element; false otherwise
-
size
int size()Count the number of distinct values stored in the tree.- Returns:
- number of elements stored in the collection
-
height
int height()Measure the height of the tree. A tree with asize()
of 0 will have a height of 0, and a tree with asize()
of 1 will have a height of 1. Otherwise, the height will be an implementation defined value between 1 andsize()
.- Returns:
- the number of steps from the root to the furthest leaf node
-