|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectBinarySearchTree
The BinarySearchTree class maps keys to values (duplicated keys are not allowed). This class implements the DictionaryADT interface and the cloneable interface. The clone method does *not* clone the key and value objects stored in the tree. These objects are considered immutable. If their value is changed after they have been inserted into the tree, the results are undefined.
Nested Class Summary | |
protected class |
BinarySearchTree.BSTEntry
An extension of Map.Entry in the java.util package used for exporting a list view, or an iterator of the contents of this tree. |
protected static class |
BinarySearchTree.BSTNode
Contains the data for each node in the BinarySearchTree as well as a set of functions that performs some important calculations on nodes to analyze and manipulate the tree structure. |
Field Summary | |
protected java.util.Comparator |
comparator
The comparator used to order these key/value pairs. |
protected int |
numElements
The number of elements in the BST |
protected BinarySearchTree.BSTNode |
root
The root of the BST. |
Constructor Summary | |
BinarySearchTree(java.util.Comparator c)
Constructs a new empty binary search tree ordered by the the given comparator |
Method Summary | |
boolean |
changeValue(java.lang.Object oldKey,
java.lang.Object newValue)
Associates a new value with an existing key in the tree. |
java.lang.Object |
clone()
Makes a copy of this tree. |
protected BinarySearchTree.BSTNode |
cloneTree(BinarySearchTree.BSTNode otherRoot)
Recursively copy the tree rooted at otherRoot, returning a reference to the newly copied subtree. |
java.lang.Object |
find(java.lang.Object key)
Finds the value associated with the given key in this BST. |
protected BinarySearchTree.BSTNode |
findNode(java.lang.Object key)
Tries to finds the node associated with a given key. |
java.util.Comparator |
getComparator()
Returns the comparator used to order this Dictionary. |
java.util.List |
getEntryList()
Gets a List view of the data in the BST. |
int |
getSize()
Retrieves the number of elements in the tree |
boolean |
insert(java.lang.Object newKey,
java.lang.Object newValue)
Inserts a new key/value pair into the BST. |
void |
print(java.io.PrintWriter writer)
Pretty-print the tree for debugging purposes. |
protected int |
recursiveGetEntryList(java.util.ArrayList entries,
BinarySearchTree.BSTNode cur)
Recursively copies the key/value pairs in the tree into the list entries . |
Methods inherited from class java.lang.Object |
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected BinarySearchTree.BSTNode root
protected int numElements
protected final java.util.Comparator comparator
Constructor Detail |
public BinarySearchTree(java.util.Comparator c)
c
- The comparator used to order the treeMethod Detail |
public java.lang.Object clone()
public java.util.Comparator getComparator()
getComparator
in interface DictionaryADT
public int getSize()
getSize
in interface DictionaryADT
public java.lang.Object find(java.lang.Object key)
find
in interface DictionaryADT
key
- The key to search for.
null
if key is not in the BST.public java.util.List getEntryList()
getEntryList
in interface DictionaryADT
public void print(java.io.PrintWriter writer)
print
in interface DictionaryADT
writer
- The print writer to output the tree onto.public boolean insert(java.lang.Object newKey, java.lang.Object newValue)
insert
in interface DictionaryADT
newKey
- key with which newValue
will be associated.newValue
- the value to be associated with newKeyh
public boolean changeValue(java.lang.Object oldKey, java.lang.Object newValue)
changeValue
in interface DictionaryADT
oldKey
- The key whose value will be changed.newValue
- The new value to associated with oldKey
protected BinarySearchTree.BSTNode findNode(java.lang.Object key)
key
- the key value to search for.
null
if the tree is empty; otherwise it returns
the node associated with the key, or the parent of where 'key' should
if the key is in the tree.protected int recursiveGetEntryList(java.util.ArrayList entries, BinarySearchTree.BSTNode cur)
entries
. The copying is done via an in-order
traversal of the subtrees rooted at cur
(thus, the data
in the arrays will be ordered by key value because of the BST
property). Does nothing if cur == null
.
entries
- The list to populate with the key/value pairscur
- The current node in the recusive traversal.
protected BinarySearchTree.BSTNode cloneTree(BinarySearchTree.BSTNode otherRoot)
otherRoot
- The root of the tree to clone.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |