RedBlackBSTKey, Value Class |
The BST class represents an ordered symbol table of generic key-value pairs. It supports the usual Put, Get, Indexer, Contains, Delete, Count, and IsEmpty methods. It also provides ordered methods for finding the Minimum, Maximum, Floor, and Ceiling. It also provides a Keys method for iterating over all of the keys. A symbol table implements the Associative array abstraction: when associating a value with a key that is already in the symbol table, the convention is to replace the old value with the new value. Unlike DictionaryTKey, TValue, this class uses the convention that values cannot be null. Setting the value associated with a key to null is equivalent to deleting the key from the symbol table.
This implementation uses a left-leaning red-black BST. It requires that the key type implements the Comparable interface and calls the compareTo() and method to compare two keys. It does not call either equals() or GetHashCode(). The Put, Contains, Remove, Minimum, Maximum, Ceiling, and Floor operations each take logarithmic time in the worst case, if the tree becomes unbalanced. The Count, and IsEmpty operations take constant time. Construction takes constant time.
Namespace: Algs4Net
public class RedBlackBST<Key, Value> where Key : Object, IComparable<Key>
The RedBlackBSTKey, Value type exposes the following members.
Name | Description | |
---|---|---|
![]() | RedBlackBSTKey, Value |
Initializes an empty symbol table. |
Name | Description | |
---|---|---|
![]() | Count |
Returns the number of key-value pairs in this symbol table. |
![]() | Height |
Returns the height of the BST (for debugging). |
![]() | IsEmpty |
Is this symbol table empty? |
![]() | Item |
Indexer wrapping Get and Put for convenient syntax
|
![]() | Max |
Returns the largest key in the symbol table. |
![]() | Min |
Returns the smallest key in the symbol table. |
Name | Description | |
---|---|---|
![]() | Ceiling |
Returns the smallest key in the symbol table greater than or equal to key. |
![]() | Contains |
Does this symbol table contain the given key? |
![]() | Delete |
Removes the specified key and its associated value from this symbol table
(if the key is in this symbol table). |
![]() | DeleteMax |
Removes the largest key and associated value from the symbol table. |
![]() | DeleteMin |
Removes the smallest key and associated value from the symbol table. |
![]() | Floor |
Returns the largest key in the symbol table less than or equal to key. |
![]() | Get |
Returns the value associated with the given key. |
![]() | Keys |
Returns all keys in the symbol table as an IEnumerable.
To iterate over all of the keys in the symbol table named st,
use the foreach notation: foreach (Key key in st.Keys()). |
![]() | Keys(Key, Key) |
Returns all keys in the symbol table in the given range,
as an IEnumerable. |
![]() ![]() | MainTest |
Demo test the RedBlackBST data type. |
![]() | Put |
Inserts the specified key-value pair into the symbol table, overwriting the old
value with the new value if the symbol table already contains the specified key.
Deletes the specified key (and its associated value) from this symbol table
if the specified value is null. |
![]() | Rank |
Return the number of keys in the symbol table strictly less than key. |
![]() | Select |
Return the kth smallest key in the symbol table. |
![]() | Size |
Returns the number of keys in the symbol table in the given range. |
For additional documentation, see Section 3.3 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
For other implementations, see BSTKey, Value. This class is a C# port from the original Java class BST implementation by Robert Sedgewick and Kevin Wayne.