public class BinarySearchTree<K,V> extends LinkedBinaryTree<Entry<K,V>> implements Dictionary<K,V>
Modifier and Type | Class and Description |
---|---|
protected static class |
BinarySearchTree.BSTEntry<K,V>
Nested class for location-aware binary search tree entries
|
Modifier and Type | Field and Description |
---|---|
protected Position<Entry<K,V>> |
actionPos |
protected java.util.Comparator<K> |
C |
protected int |
numEntries |
root, size
Constructor and Description |
---|
BinarySearchTree()
Creates a BinarySearchTree with a default comparator.
|
BinarySearchTree(java.util.Comparator<K> c)
Creates a BinarySearchTree with the given comparator.
|
Modifier and Type | Method and Description |
---|---|
protected void |
addAll(PositionList<Entry<K,V>> L,
Position<Entry<K,V>> v,
K k)
Adds to L all entries in the subtree rooted at v having keys
equal to k.
|
protected void |
checkEntry(Entry<K,V> ent)
Checks whether a given entry is valid.
|
protected void |
checkKey(K key)
Checks whether a given key is valid.
|
java.lang.Iterable<Entry<K,V>> |
entries()
Returns an iterator containing all entries in the tree.
|
protected Entry<K,V> |
entry(Position<Entry<K,V>> position)
Extracts the entry at a given node of the tree.
|
Entry<K,V> |
find(K key)
Returns an entry containing the given key.
|
java.lang.Iterable<Entry<K,V>> |
findAll(K key)
Returns an iterable collection of all the entries containing the
given key.
|
Entry<K,V> |
insert(K k,
V x)
Inserts an entry into the tree and returns the newly created entry.
|
protected Entry<K,V> |
insertAtExternal(Position<Entry<K,V>> v,
Entry<K,V> e)
Auxiliary method for inserting an entry at an external node
|
boolean |
isEmpty()
Returns whether the tree is empty.
|
protected K |
key(Position<Entry<K,V>> position)
Extracts the key of the entry at a given node of the tree.
|
Entry<K,V> |
remove(Entry<K,V> ent)
Removes and returns a given entry.
|
protected void |
removeExternal(Position<Entry<K,V>> v)
Auxiliary method for removing an external node and its parent
|
protected void |
replaceEntry(Position<Entry<K,V>> pos,
Entry<K,V> ent)
Replaces an entry with a new entry (and reset the entry's location)
|
protected Position<Entry<K,V>> |
restructure(Position<Entry<K,V>> x)
Performs a tri-node restructuring.
|
int |
size()
Returns the number of entries in the tree.
|
protected Position<Entry<K,V>> |
treeSearch(K key,
Position<Entry<K,V>> pos)
Auxiliary method used by find, insert, and remove.
|
protected V |
value(Position<Entry<K,V>> position)
Extracts the value of the entry at a given node of the tree.
|
addRoot, attach, checkPosition, children, createNode, expandExternal, hasLeft, hasRight, inorderPositions, insertLeft, insertRight, isExternal, isInternal, isRoot, iterator, left, parent, positions, preorderPositions, remove, removeAboveExternal, replace, right, root, sibling, swapElements
protected java.util.Comparator<K> C
protected int numEntries
public BinarySearchTree()
public BinarySearchTree(java.util.Comparator<K> c)
protected K key(Position<Entry<K,V>> position)
protected V value(Position<Entry<K,V>> position)
protected Entry<K,V> entry(Position<Entry<K,V>> position)
protected void replaceEntry(Position<Entry<K,V>> pos, Entry<K,V> ent)
protected void checkKey(K key) throws InvalidKeyException
InvalidKeyException
protected void checkEntry(Entry<K,V> ent) throws InvalidEntryException
InvalidEntryException
protected Entry<K,V> insertAtExternal(Position<Entry<K,V>> v, Entry<K,V> e)
protected void removeExternal(Position<Entry<K,V>> v)
protected Position<Entry<K,V>> treeSearch(K key, Position<Entry<K,V>> pos)
protected void addAll(PositionList<Entry<K,V>> L, Position<Entry<K,V>> v, K k)
public int size()
public boolean isEmpty()
public Entry<K,V> find(K key) throws InvalidKeyException
find
in interface Dictionary<K,V>
InvalidKeyException
public java.lang.Iterable<Entry<K,V>> findAll(K key) throws InvalidKeyException
findAll
in interface Dictionary<K,V>
InvalidKeyException
public Entry<K,V> insert(K k, V x) throws InvalidKeyException
insert
in interface Dictionary<K,V>
InvalidKeyException
public Entry<K,V> remove(Entry<K,V> ent) throws InvalidEntryException
remove
in interface Dictionary<K,V>
InvalidEntryException
public java.lang.Iterable<Entry<K,V>> entries()
entries
in interface Dictionary<K,V>
protected Position<Entry<K,V>> restructure(Position<Entry<K,V>> x)
z=c z=c z=a z=a / \ / \ / \ / \ y=b t4 y=a t4 t1 y=c t1 y=b / \ / \ / \ / \ x=a t3 t1 x=b x=b t4 t2 x=c / \ / \ / \ / \ t1 t2 t2 t3 t2 t3 t3 t4