Package generic.stl
Class RedBlackTree<K,V>
java.lang.Object
generic.stl.RedBlackTree<K,V>
A RedBlack Tree implementation with K type keys and place to store V type values.
-
Field Summary
-
Constructor Summary
ConstructorDescriptionRedBlackTree
(RedBlackTree<K, V> tree) Creates a copy of an existing RedBlackTreeRedBlackTree
(Comparator<K> comparator, boolean allowDuplicateKeys) Creates a new RedBlackTree -
Method Summary
Modifier and TypeMethodDescriptionboolean
containsKey
(K key) Returns true if the key is in the set.void
deleteEntry
(RedBlackNode<K, V> p) Delete node p, and then rebalance the tree.findFirstNode
(K key) findLastNode
(K key) getFirst()
Returns the first entry in this set.getLast()
Returns the last entry in this set.boolean
isEmpty()
Test if the set is empty.lowerBound
(K key) Finds the node with the lowest key that is >= to the given key.Adds the given key,value to the map.Removes the given key (first if duplicates are allowed) from the set.void
Removes all entrys from the set.int
size()
Returns the number keys in this set.toString()
upperBound
(K key) Finds the node with the lowest key that is > the given key.
-
Field Details
-
EOL
-
-
Constructor Details
-
RedBlackTree
Creates a new RedBlackTree- Parameters:
comparator
- the comparator for this treeallowDuplicateKeys
- true to allow duplicate keys
-
RedBlackTree
Creates a copy of an existing RedBlackTree- Parameters:
tree
- the existing tree to copy
-
-
Method Details
-
toString
-
size
public int size()Returns the number keys in this set. -
containsKey
Returns true if the key is in the set.- Parameters:
key
- the key whose presence is to be tested.
-
getFirst
Returns the first entry in this set. -
getLast
Returns the last entry in this set. -
lowerBound
Finds the node with the lowest key that is >= to the given key. Returns null if all nodes in the tree have keys less than the given key.- Parameters:
key
- the key to search for.- Returns:
- the node with the lowest key that is >= to the given key or null if no such key exists.
-
upperBound
Finds the node with the lowest key that is > the given key. Returns null if all nodes in the tree have keys less than or equal to the given key.- Parameters:
key
- the key to search for.- Returns:
- the node with the lowest key that is > to the given key or null if no such key exists.
-
put
Adds the given key,value to the map. If the map does not allow duplicate keys and a key already exists, the old value will be replaced by the new value and the old value will be returned.- Parameters:
key
- the key to add to the set.value
- the key's value.- Returns:
- the old value associated with the key, or null if the key was not previously in the map.
-
findFirstNode
-
findLastNode
-
remove
Removes the given key (first if duplicates are allowed) from the set.- Parameters:
key
- the key to remove from the set.- Returns:
- the value associated with the key removed or null if the key not found.
-
removeAll
public void removeAll()Removes all entrys from the set. -
isEmpty
public boolean isEmpty()Test if the set is empty.- Returns:
- true if the set is empty.
-
deleteEntry
Delete node p, and then rebalance the tree.
-