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.
|
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.
|
String |
bfs()
Returns a string representation of the breadth first search traversal of the tree.
|
BinarySearchTree.Node<E> |
getRoot()
Get the root node of the tree.
|
static void |
main(String[] args) |
String |
preOrder()
Returns a string representation of the preorder traversal of the tree.
|
String |
toString()
Return a string representation of the inorder traversal of the tree.
|
public BinarySearchTree.Node<E> getRoot()
public void add(E element)
element
- the element to add to the treepublic 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()
public String preOrder()
The string is made up of the elements stored in the tree separated by commas; the entire list of elements is enclosed in braces.
public String bfs()
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 breadth first search traversal for any node of the tree visits the node, followed by the left child of the node, followed by the right child of the node.
public static void main(String[] args)