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.
|
private int |
compareKeys(K k1,
K k2)
This method compares two keys of generic type
K and returns
the result of the comparison |
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> |
deleteNode(TreeNode<K,V> node)
The private recursive method to delete the specified TreeNode.
|
private TreeNode<K,V> |
deleteSuccessorNode(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 |
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> |
findSuccessorNode(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. |
boolean |
isEmpty()
Method to find out if the BinarySearchTree is empty
|
private boolean |
isLeafNode(TreeNode<K,V> node)
This method checks if the specified
node is a leaf node. |
void |
makeEmpty()
Method to remove all entries from the BinarySearchTree.
|
private boolean |
noLeftChild(TreeNode<K,V> node)
This method checks if the specified
node has no left child. |
private boolean |
noRightChild(TreeNode<K,V> node)
This method checks if the specified
node has no right child. |
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.
|
private boolean |
treeIsBalanced(TreeNode<K,V> node)
The private recursive method to determine if the tree rooted at the given node is
balanced.
|
public BinarySearchTree()
public TreeNode<K,V> getRoot()
public void setRoot(TreeNode<K,V> root)
root - The new root of this BinarySearchTreepublic boolean isEmpty()
public void makeEmpty()
public TreeItem<K,V> getRootItem() throws TreeException
TreeException - Throws a TreeException if the root of the tree is nullpublic TreeItem<K,V> find(K key)
key - The key that the user wishes to search forpublic void insert(TreeItem<K,V> treeItem)
treeItem - 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.TreeExceptionpublic int height()
BinarySearchTree.BinarySearchTree.public boolean isBalanced()
BinarySearchTree is balanced.true if the BinarySearchTree is balanced, false otherwise.public void balance()
BinarySearchTree.private int compareKeys(K k1, K k2)
K and returns
the result of the comparisonk1 - The first keyk2 - The second keycompareTo() operation.private 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 forprivate 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 BinarySearchTreeprivate 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> findSuccessorNode(TreeNode<K,V> node)
deleteNode methodnode - The current TreeNode being examined for being the left-most childprivate TreeNode<K,V> deleteSuccessorNode(TreeNode<K,V> node)
deleteNode methodnode - The current TreeNode being examined for being the left-most childprivate int treeHeight(TreeNode<K,V> node)
height method
and treeIsBalancednode - The root node of the BinarySearchTree for which we are measuring
the height.BinarySearchTree.private boolean treeIsBalanced(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.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.private boolean isLeafNode(TreeNode<K,V> node)
node is a leaf node.node - The node being checkedtrue if the node is a leaf node, false
otherwise.private boolean noLeftChild(TreeNode<K,V> node)
node has no left child.node - The node being checkedtrue if the node does not have a left child,
false otherwise.