Created
November 1, 2017 01:20
-
-
Save marianocordoba/f2e629a7a79a209f3fd91653091ab4a6 to your computer and use it in GitHub Desktop.
Java implementation of binary search tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tree; | |
/** | |
* Binary search tree implementation | |
*/ | |
public class BST<T extends Comparable<? super T>> implements Tree<T> { | |
/** | |
* A class representing a node of the binary tree. | |
*/ | |
static class Node<T> { | |
T value; | |
Node<T> left; | |
Node<T> right; | |
/** | |
* Constructor. | |
* @param value the value of the node | |
*/ | |
Node(T value) { | |
this.value = value; | |
} | |
} | |
private Node<T> root; | |
/** | |
* Constructor. | |
*/ | |
public BST() { | |
this.root = null; | |
} | |
/** | |
* Empties the tree. | |
*/ | |
@Override | |
public void empty() { | |
this.root = null; | |
} | |
/** | |
* Check if the tree is empty. | |
* @return true if the tree is empty, false otherwise | |
*/ | |
@Override | |
public boolean isEmpty() { | |
return this.root == null; | |
} | |
/** | |
* Returns the value of a node | |
* @param node the node | |
* @return the value of the node or null | |
*/ | |
private T valueOf(Node<T> node) { | |
return node == null ? null : node.value; | |
} | |
/** | |
* Inserts an element in the tree. | |
* @param value the element to be inserted | |
* @throws DuplicateElementException | |
*/ | |
@Override | |
public void insert(T value) throws DuplicateElementException { | |
this.root = insert(value, this.root); | |
} | |
/** | |
* Inserts an element in a subtree. | |
* @param value the element to be inserted | |
* @param node the subtree root | |
* @throws DuplicateElementException | |
*/ | |
private Node<T> insert(T value, Node<T> node) throws DuplicateElementException { | |
if (node == null) | |
node = new Node<>(value); | |
else if (value.compareTo(node.value) < 0) | |
node.left = insert(value, node.left); | |
else if (value.compareTo(node.value) > 0) | |
node.right = insert(value, node.right); | |
else | |
throw new DuplicateElementException(value.toString() + " is already in the tree."); | |
return node; | |
} | |
/** | |
* Removes an element from the tree. | |
* @param value the element to be removed | |
* @throws ElementNotFoundException | |
*/ | |
@Override | |
public void remove(T value) throws ElementNotFoundException { | |
this.root = remove(value, this.root); | |
} | |
/** | |
* Removes an element from a subtree. | |
* @param value the element to be removed | |
* @param node the subtree root | |
* @return the node resulting from removing the element | |
* @throws ElementNotFoundException | |
*/ | |
private Node<T> remove(T value, Node<T> node) throws ElementNotFoundException { | |
if (node == null) | |
throw new ElementNotFoundException(value.toString() + " was not found in the tree."); | |
else if (value.compareTo(node.value) < 0) | |
node.left = remove(value, node.left); | |
else if (value.compareTo(node.value) > 0) | |
node.right = remove(value, node.right); | |
else if (node.left != null && node.right != null) { | |
node.value = findMin(node.right).value; | |
node.right = removeMin(node.right); | |
} else | |
// If either left or right are != null, then the node has just one child and we replace the reference. | |
// Else, if both left and right are null, then the node is a leaf and it is deleted. | |
node = (node.left != null) ? node.left : node.right; | |
return node; | |
} | |
/** | |
* Removes the minimum element from the tree. | |
* @throws ElementNotFoundException | |
*/ | |
public void removeMin() throws ElementNotFoundException { | |
this.root = removeMin(this.root); | |
} | |
/** | |
* Removes the minimum element from a subtree. | |
* @param node the subtree root | |
* @return the node resulting from removing the element | |
* @throws ElementNotFoundException | |
*/ | |
private Node<T> removeMin(Node<T> node) throws ElementNotFoundException { | |
if (node == null) | |
throw new ElementNotFoundException("Minimum element not found."); | |
else if (node.left != null) { | |
node.left = removeMin(node.left); | |
return node; | |
} else | |
return node.right; | |
} | |
/** | |
* Removes the maximum element from the tree. | |
* @throws ElementNotFoundException | |
*/ | |
public void removeMax() throws ElementNotFoundException { | |
this.root = removeMax(this.root); | |
} | |
/** | |
* Removes the maximum element from a subtree. | |
* @param node the subtree root | |
* @return the node resulting from removing the element | |
* @throws ElementNotFoundException | |
*/ | |
private Node<T> removeMax(Node<T> node) throws ElementNotFoundException { | |
if (node == null) | |
throw new ElementNotFoundException("Maximum element not found."); | |
else if (node.right != null) { | |
node.right = removeMax(node.right); | |
return node; | |
} else | |
return node.left; | |
} | |
/** | |
* Finds an element in the tree | |
* @param value the searched element | |
* @return the searched element or null if it doesn't exists | |
*/ | |
@Override | |
public T find(T value) { | |
return valueOf(find(value, this.root)); | |
} | |
/** | |
* Finds an element in a subtree. | |
* @param value the searched element | |
* @param node the subtree root | |
* @return the node containing the searched element or null if it doesn't exists | |
*/ | |
private Node<T> find(T value, Node<T> node) { | |
if (node == null) | |
return null; | |
else | |
if (value.compareTo(node.value) < 0) | |
return find(value, node.left); | |
else if (value.compareTo(node.value) > 0) | |
return find(value, node.right); | |
else | |
return node; | |
} | |
/** | |
* Finds the minimum element in the tree. | |
* @return the minimum element in the tree or null if it doesn't exists | |
*/ | |
public T findMin() { | |
return valueOf(findMin(this.root)); | |
} | |
/** | |
* Find the minimum element in a subtree. | |
* @param node the subtree root | |
* @return the node containing the minimum element in the subtree or null if it doesn't exists | |
*/ | |
private Node<T> findMin(Node<T> node) { | |
if (node == null) | |
return null; | |
else if (node.left == null) | |
return node; | |
else | |
return findMin(node.left); | |
} | |
/** | |
* Finds the maximum element in the tree. | |
* @return the node containing the maximum element in the tree or null if it doesn't exists | |
*/ | |
public T findMax() { | |
return valueOf(findMax(this.root)); | |
} | |
/** | |
* Find the maximum element in a subtree. | |
* @param node the subtree root | |
* @return the node containing the maximum element in the subtree or null if it doesn't exists | |
*/ | |
private Node<T> findMax(Node<T> node) { | |
if (node == null) | |
return null; | |
else if (node.right == null) | |
return node; | |
else | |
return findMax(node.right); | |
} | |
/** | |
* Returns the size of the tree. | |
* @return the size of the tree | |
*/ | |
@Override | |
public int size() { | |
return size(this.root); | |
} | |
/** | |
* Returns the size of a subtree. | |
* @return the size of the subtree | |
*/ | |
private int size(Node<T> node) { | |
if (node == null) | |
return 0; | |
else | |
return 1 + size(node.left) + size(node.right); | |
} | |
/** | |
* Returns the height of the tree. | |
* @return the height of the tree | |
*/ | |
@Override | |
public int height() { | |
return height(this.root); | |
} | |
/** | |
* Returns the height of a subtree. | |
* @return the height of the subtree | |
*/ | |
private int height(Node<T> node) { | |
if (node == null) | |
return 0; | |
else | |
return 1 + Math.max(height(node.left), height(node.right)); | |
} | |
/** | |
* Prints the tree content | |
* @param order the order to traverse the nodes | |
*/ | |
@Override | |
public void print(Order order) { | |
print(order, this.root); | |
} | |
/** | |
* Prints a subtree content | |
* @param order the order to traverse the nodes | |
* @param node the subtree root | |
*/ | |
private void print(Order order, Node<T> node) { | |
if (node == null) return; | |
if (order == Order.PREORDER) | |
System.out.println(node.value); | |
print(order, node.left); | |
if (order == Order.INORDER) | |
System.out.println(node.value); | |
print(order, node.right); | |
if (order == Order.POSTORDER) | |
System.out.println(node.value); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tree; | |
public interface Tree<T extends Comparable<? super T>> { | |
class DuplicateElementException extends RuntimeException { | |
DuplicateElementException(String message) { | |
super(message); | |
} | |
} | |
class ElementNotFoundException extends RuntimeException { | |
ElementNotFoundException(String message) { | |
super(message); | |
} | |
} | |
enum Order { | |
PREORDER, INORDER, POSTORDER | |
} | |
void empty(); | |
boolean isEmpty(); | |
void insert(T value) throws DuplicateElementException; | |
void remove(T value) throws ElementNotFoundException; | |
T find(T value); | |
int size(); | |
int height(); | |
void print(Order order); | |
} |
Hi,
This is an article, I have written while practicing the algorithms. I gathers a lot of information and wrote this articles of a binary tree in java: this is the link-> Click here
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to write the main? Please help
Your main program will use a BST (compiled in Part 1) to maintain the database of Bank Customers (comparable type created in Part 2).
Your program will open and immediately call a “Load Customers” method that will read the text file of customers you previously created (either directly in notepad or by writing a text file) and store the Customers in the BST.
Hard Code the textfile name into the method and include a copy of the text file when you submit your project.
The program will then display a “menu” (just SOP statements, numbered, so you can use switch-case statements to call the appropriate methods) similar to this:
Include a prompt for the menu selection by number (input through keyboard)