public class BinarySearchTree<K extends java.lang.Comparable<? super K>,V>
extends java.lang.Object
| Constructor and Description |
|---|
BinarySearchTree()
Default constructor for BinarySearchTree.
|
BinarySearchTree(TreeNode<K,V> root)
This constructor instantiates a BinarySearchTree with the given
root |
| Modifier and Type | Method and Description |
|---|---|
void |
balance()
This method is used to balance the
BinarySearchTree. |
private TreeNode<K,V> |
balanceTree(java.lang.Object[] arr,
int first,
int last)
The private recursive method to balance the BinarySearchTree.
|
void |
delete(K key)
This is the public method used to delete a TreeItem from the
BinarySearchTree based
on the specified key. |
private TreeNode<K,V> |
deleteItem(TreeNode<K,V> node,
K key)
The private recursive method to delete the TreeItem
with the specified key from BinarySearchTree.
|
private TreeNode<K,V> |
deleteLeftmost(TreeNode<K,V> node)
The private recursive method to deleted the left-most child of a subtree
This method is initially called by the
deleteNode method |
private TreeNode<K,V> |
deleteNode(TreeNode<K,V> node)
The private recursive method to delete the specified TreeNode.
|
TreeItem<K,V> |
find(K key)
Method to find and retrieve the TreeItem
with the given key if it is in the BinarySearchTree.
|
private TreeItem<K,V> |
findItem(TreeNode<K,V> node,
K key)
The private recursive method to find and retrieve the TreeItem
with the given key if it is in the BinarySearchTree.
|
private TreeNode<K,V> |
findLeftmost(TreeNode<K,V> node)
The private recursive method to find the left-most child of a subtree
This method is initially called by the
deleteNode method |
TreeNode<K,V> |
getRoot()
Method to get the current root of this BinarySearchTree
|
TreeItem<K,V> |
getRootItem()
Method to get the TreeItem currently in the root of this BinarySearchTree
|
int |
height()
This method is used to obtain the height of the
BinarySearchTree. |
void |
insert(TreeItem<K,V> treeItem)
Method to insert the given TreeItem into the BinarySearchTree
|
private TreeNode<K,V> |
insertItem(TreeNode<K,V> node,
TreeNode<K,V> parent,
TreeItem<K,V> treeItem)
The private recursive method to insert the TreeItem
into the BinarySearchTree.
|
boolean |
isBalanced()
This is the method the user calls to find out if the
BinarySearchTree is balanced. |
private boolean |
isBalancedSubtree(TreeNode<K,V> node)
The private recursive method to determine if the subtree rooted at the given node is
balanced.
|
boolean |
isEmpty()
Method to find out if the BinarySearchTree is empty
|
void |
makeEmpty()
Method to remove all entries from the BinarySearchTree.
|
void |
setRoot(TreeNode<K,V> root)
Method to set set the root of this BinarySearchTree
|
private int |
treeHeight(TreeNode<K,V> node)
The private recursive method to calculate the height of a subtree rooted at the
specified TreeNode.
|
public BinarySearchTree()
public TreeNode<K,V> getRoot()
public void setRoot(TreeNode<K,V> root)
root - The new root of this BinarySearchTreepublic TreeItem<K,V> getRootItem() throws TreeException
TreeException - Throws a TreeException if the root of the tree is nullpublic boolean isEmpty()
public void makeEmpty()
public TreeItem<K,V> find(K key)
key - The key that the user wishes to search forprivate TreeItem<K,V> findItem(TreeNode<K,V> node, K key)
find methodnode - The current TreeNode being searchedkey - The key that the user wishes to search forpublic void insert(TreeItem<K,V> treeItem)
treeItem - The TreeItem to insert into the BinarySearchTreeprivate TreeNode<K,V> insertItem(TreeNode<K,V> node, TreeNode<K,V> parent, TreeItem<K,V> treeItem)
insert methodnode - The current TreeNode being examined for insertionparent - The current parent of the TreeNode being examinedtreeItem - The TreeItem to insert into the BinarySearchTreepublic void delete(K key) throws TreeException
BinarySearchTree based
on the specified key.key - The key of the TreeItem the user wishes to delete from the BinarySearchTree.TreeExceptionprivate TreeNode<K,V> deleteItem(TreeNode<K,V> node, K key)
delete methodnode - The current TreeNode being examined for deletionkey - The key of the TreeItem to be deletedprivate TreeNode<K,V> deleteNode(TreeNode<K,V> node)
deleteItem methodnode - The current TreeNode being examined for deletionprivate TreeNode<K,V> findLeftmost(TreeNode<K,V> node)
deleteNode methodnode - The current TreeNode being examined for being the left-most childprivate TreeNode<K,V> deleteLeftmost(TreeNode<K,V> node)
deleteNode methodnode - The current TreeNode being examined for being the left-most childpublic int height()
BinarySearchTree.BinarySearchTree.private int treeHeight(TreeNode<K,V> node)
height method
and isBalancedSubtreenode - The root node of the BinarySearchTree for which we are measuring
the height.BinarySearchTree.public boolean isBalanced()
BinarySearchTree is balanced.true if the BinarySearchTree is balanced, false otherwise.private boolean isBalancedSubtree(TreeNode<K,V> node)
isBalanced methodnode - The root node of the BinarySearchTree that is being checked for
balance.true if the BinarySearchTree is balanced, false otherwise.public void balance()
BinarySearchTree.private TreeNode<K,V> balanceTree(java.lang.Object[] arr, int first, int last)
balance methodarr - An array containing all the TreeItems for the BinarySearchTree in sorted order.first - The first index of the array.last - The last index of the array.