Click or drag to resize
BinarySearchSTKey, 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 sorted array. It requires that the key type implements the IComparable interface and calls the CompareTo() method to compare two keys. It does not call either Equals() or GetHashCode(). The Put and Remove operations each take linear time in the worst case; the Contains, Ceiling, Floor, and Rank operations take logarithmic time; the Count, IsEmpty, Minimum, Maximum, and indexer operations take constant time. Construction takes constant time.

Inheritance Hierarchy
SystemObject
  Algs4NetBinarySearchSTKey, Value

Namespace: Algs4Net
Assembly: Algs4Net (in Algs4Net.dll) Version: 1.0.0.0 (1.0.0.0)
Syntax
C#
public class BinarySearchST<Key, Value>
where Key : Object, IComparable<Key>

Type Parameters

Key
Value

The BinarySearchSTKey, Value type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyCount
Returns the number of key-value pairs in this symbol table.
Public propertyIsEmpty
Returns true if this symbol table is empty.
Public propertyItem
Indexer wrapping Get and Put for convenient syntax
Public propertyMax
Returns the largest key in this symbol table.
Public propertyMin
Returns the smallest key in this symbol table.
Top
Methods
  NameDescription
Public methodCeiling
Returns the smallest key in this symbol table greater than or equal to key.
Public methodContains
Does this symbol table contain the given key?
Public methodDelete
Removes the specified key and associated value from this symbol table (if the key is in the symbol table).
Public methodDeleteMax
Removes the largest key and associated value from this symbol table.
Public methodDeleteMin
Removes the smallest key and associated value from this symbol table.
Public methodFloor
Returns the largest key in this symbol table less than or equal to key.
Public methodGet
Returns the value associated with the given key in this symbol table.
Public methodKeys
Returns all keys in this 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()).
Public methodKeys(Key, Key)
Returns all keys in this symbol table in the given range, as an IEnumerable.
Public methodStatic memberMainTest
Demo test the BinarySearchST data type.
Public methodPut
Removes the specified key and its associated value from this symbol table (if the key is in this symbol table).
Public methodRank
Returns the number of keys in this symbol table strictly less than key.
Public methodSelect
Return the kth smallest key in this symbol table.
Public methodSize
Returns the number of keys in this symbol table in the specified range.
Top
Remarks

For additional documentation, see Section 3.1 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

For other implementations, see SequentialSearchSTKey, Value. This class is a C# port from the original Java class BinarySearchST implementation by Robert Sedgewick and Kevin Wayne.

See Also