package cse1030.datastructures; /** * A binary search tree class. * *
* A binary search tree is a binary tree where values are stored in the tree * according to three rules: * *
subtreeRoot
. The
* element is inserted into the tree in a position that preserves the
* definition of a binary search tree.
*
* @param element
* the element to add to the subtree
* @param subtreeRoot
* the root of the subtree
*/
private static
* 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.
*
* @see java.lang.Object#toString()
*
* @return a string representation of the tree
*/
@Override
public String toString() {
return "{" + toString(this.root) + "}";
}
/**
* Return a string representation of the subtree rooted at
* 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.
*
* @param subtreeRoot
* the root of the subtree
* @return the string representation of the subtree
*/
private static
* The string is made up of the elements stored in the tree separated by
* commas; the entire list of elements is enclosed in braces.
*
* @return a string representation of the preorder traversal of the tree
*/
public String preOrder() {
StringBuilder b = new StringBuilder();
b.append('{');
b.append('}');
return b.toString();
}
/**
* Returns a string representation of the breadth first search traversal of the tree.
*
*
* 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.
*
* @return a string representation of the breadth first search traversal of the tree
*/
public String bfs() {
StringBuilder b = new StringBuilder();
b.append('{');
b.append('}');
return b.toString();
}
public static void main(String[] args) {
BinarySearchTree