E
- the type of the data stored in the nodes of the binary search treepublic class BinarySearchTree<E extends Comparable<? super E>> extends Object
A binary search tree is a binary tree where values are stored in the tree according to three rules:
Modifier and Type | Class and Description |
---|---|
static class |
BinarySearchTree.Node<E>
A class that represents nodes in the binary search tree.
|
Modifier and Type | Field and Description |
---|---|
private BinarySearchTree.Node<E> |
root
The root node of the binary search tree.
|
Constructor and Description |
---|
BinarySearchTree()
Create an empty binary search tree.
|
Modifier and Type | Method and Description |
---|---|
void |
add(E element)
Add an element to the tree.
|
private static <E extends Comparable<? super E>> |
add(E element,
BinarySearchTree.Node<E> subtreeRoot)
Add an element to the subtree rooted at
subtreeRoot . |
boolean |
contains(E element)
Returns
true if the tree contains the given element,
false otherwise. |
private static <E extends Comparable<? super E>> |
contains(E element,
BinarySearchTree.Node<E> subtreeRoot)
Returns
true if the subtree rooted at subtreeRoot
contains the given element, false otherwise. |
BinarySearchTree.Node<E> |
getRoot()
Get the root node of the tree.
|
static <E> BinarySearchTree.Node<E> |
maximumInSubtree(BinarySearchTree.Node<E> subtreeRoot)
Find the node in a subtree that has the largest data element.
|
static <E> BinarySearchTree.Node<E> |
minimumInSubtree(BinarySearchTree.Node<E> subtreeRoot)
Find the node in a subtree that has the smallest data element.
|
static <E> BinarySearchTree.Node<E> |
predecessorInSubtree(BinarySearchTree.Node<E> subtreeRoot)
Find the node in a subtree that is the predecessor to the root of the
subtree.
|
static <E> BinarySearchTree.Node<E> |
successorInSubtree(BinarySearchTree.Node<E> subtreeRoot)
Find the node in a subtree that is the successor to the root of the
subtree.
|
String |
toString()
Return a string representation of the tree.
|
private static <E> String |
toString(BinarySearchTree.Node<E> subtreeRoot)
Return a string representation of the subtree rooted at
subtreeRoot . |
private BinarySearchTree.Node<E extends Comparable<? super E>> root
public void add(E element)
element
- the element to add to the treeprivate static <E extends Comparable<? super E>> void add(E element, BinarySearchTree.Node<E> subtreeRoot)
subtreeRoot
. The
element is inserted into the tree in a position that preserves the
definition of a binary search tree.element
- the element to add to the subtreesubtreeRoot
- the root of the subtreepublic boolean contains(E element)
true
if the tree contains the given element,
false
otherwise.element
- the element to search fortrue
if the tree contains the given element,
false
otherwiseprivate static <E extends Comparable<? super E>> boolean contains(E element, BinarySearchTree.Node<E> subtreeRoot)
true
if the subtree rooted at subtreeRoot
contains the given element, false
otherwise.element
- the element to search forsubtreeRoot
- the root of the subtreetrue
if the subtree rooted at subtreeRoot
contains the given element, false
otherwisepublic String toString()
The string is made up of the elements stored in the tree separated by commas; the entire list of elements is enclosed in braces. The elements are in ascending sorted order.
toString
in class Object
Object.toString()
private static <E> String toString(BinarySearchTree.Node<E> subtreeRoot)
subtreeRoot
.
The string is made up of the elements stored in the tree separated by commas where the elements are in ascending sorted order.
The string is generated using inorder processing of the subtree:
subtreeRoot.left
is computed
subtreeRoot.data
is computed
subtreeRoot.right
is computed
The returned string is the concatenation of the three strings computed by the inorder processing of the subtree.
subtreeRoot
- the root of the subtreepublic BinarySearchTree.Node<E> getRoot()
public static <E> BinarySearchTree.Node<E> minimumInSubtree(BinarySearchTree.Node<E> subtreeRoot)
subtreeRoot
- the root of the subtreepublic static <E> BinarySearchTree.Node<E> maximumInSubtree(BinarySearchTree.Node<E> subtreeRoot)
subtreeRoot
- the root of the subtreepublic static <E> BinarySearchTree.Node<E> predecessorInSubtree(BinarySearchTree.Node<E> subtreeRoot)
subtreeRoot
.subtreeRoot
- the root of the subtreenull
if the root of the subtree has no predecessorpublic static <E> BinarySearchTree.Node<E> successorInSubtree(BinarySearchTree.Node<E> subtreeRoot)
subtreeRoot
.subtreeRoot
- the root of the subtreenull
if the root of the subtree has no successor