Trie Class
Definition
- Namespace
- Bodu.Collections.Generic.Trees
- Assembly
- Bodu.Collections.dll
- Package
- Bodu.Collections 1.0.0
- Source
- Trie.Enumerator.cs
Represents a prefix tree (trie) of string keys and supports efficient prefix queries.
public sealed class Trie : IReadOnlyCollection<string>, IEnumerable<string>, IEnumerable
- Inheritance
-
Trie
- Implements
- Inherited Members
- Extension Methods
Remarks
A Trie stores keys as paths of characters, so membership and prefix operations cost time proportional to the length of the key rather than to the number of stored keys. Character transitions are keyed by the IEqualityComparer<T> supplied at construction, allowing ordinal or case-insensitive matching. The empty string is a valid key.
Enumeration order - through GetEnumerator() or KeysWithPrefix(string) - is unspecified in this version. The trie is not thread-safe for concurrent mutation. For an associative variant that maps keys to values, see Trie<TValue>.
Constructors
Trie()
Initializes a new instance of the Trie class that is empty and uses ordinal character comparison.
public Trie()
Trie(IEnumerable<string>, IEqualityComparer<char>?)
Initializes a new instance of the Trie class containing the specified keys.
public Trie(IEnumerable<string> keys, IEqualityComparer<char>? charComparer = null)
Parameters
keysIEnumerable<string>The keys to add.
charComparerIEqualityComparer<char>The comparer used to match characters, or null to use Default.
Exceptions
- ArgumentNullException
- ArgumentException
A duplicate key is supplied.
Trie(IEqualityComparer<char>?)
Initializes a new instance of the Trie class that is empty and uses the specified character comparer.
public Trie(IEqualityComparer<char>? charComparer)
Parameters
charComparerIEqualityComparer<char>The comparer used to match characters, or null to use Default.
Properties
Comparer
Gets the comparer used to match characters during key lookup.
public IEqualityComparer<char> Comparer { get; }
Property Value
- IEqualityComparer<char>
The character comparer supplied at construction, or the default comparer.
Count
Gets the number of keys stored in the trie.
public int Count { get; }
Property Value
- int
The number of stored keys.
Methods
Add(ReadOnlySpan<char>)
Adds the specified key to the trie.
public bool Add(ReadOnlySpan<char> key)
Parameters
keyReadOnlySpan<char>The key to add.
Returns
Add(string)
Adds the specified key to the trie.
public bool Add(string key)
Parameters
keystringThe key to add.
Returns
Exceptions
- ArgumentNullException
keyis null.
Clear()
Removes all keys from the trie.
public void Clear()
Contains(ReadOnlySpan<char>)
Determines whether the trie contains the specified key.
public bool Contains(ReadOnlySpan<char> key)
Parameters
keyReadOnlySpan<char>The key to locate.
Returns
Contains(string)
Determines whether the trie contains the specified key.
public bool Contains(string key)
Parameters
keystringThe key to locate.
Returns
Exceptions
- ArgumentNullException
keyis null.
GetEnumerator()
Returns an enumerator that iterates over the keys of the trie.
public Trie.Enumerator GetEnumerator()
Returns
- Trie.Enumerator
A struct enumerator that lazily walks the trie's keys and fails fast on modification.
KeysWithPrefix(string)
Returns the keys in the trie that begin with the specified prefix.
public IEnumerable<string> KeysWithPrefix(string prefix)
Parameters
prefixstringThe prefix to match.
Returns
- IEnumerable<string>
A lazily evaluated sequence of matching keys, in unspecified order.
Remarks
The sequence is fail-fast: it is invalidated by any structural modification of the trie, and continuing to iterate after a modification throws InvalidOperationException.
Exceptions
- ArgumentNullException
prefixis null.- InvalidOperationException
The trie was modified after enumeration began.
Remove(ReadOnlySpan<char>)
Removes the specified key from the trie.
public bool Remove(ReadOnlySpan<char> key)
Parameters
keyReadOnlySpan<char>The key to remove.
Returns
Remove(string)
Removes the specified key from the trie.
public bool Remove(string key)
Parameters
keystringThe key to remove.
Returns
Exceptions
- ArgumentNullException
keyis null.
StartsWith(ReadOnlySpan<char>)
Determines whether any key in the trie begins with the specified prefix.
public bool StartsWith(ReadOnlySpan<char> prefix)
Parameters
prefixReadOnlySpan<char>The prefix to test.
Returns
StartsWith(string)
Determines whether any key in the trie begins with the specified prefix.
public bool StartsWith(string prefix)
Parameters
prefixstringThe prefix to test.
Returns
Exceptions
- ArgumentNullException
prefixis null.
Explicit Interface Implementations
IEnumerable<string>.GetEnumerator()
Returns an enumerator that iterates through the collection.
IEnumerator<string> IEnumerable<string>.GetEnumerator()
Returns
- IEnumerator<string>
An enumerator that can be used to iterate through the collection.
IEnumerable.GetEnumerator()
Returns an enumerator that iterates through a collection.
IEnumerator IEnumerable.GetEnumerator()
Returns
- IEnumerator
An IEnumerator object that can be used to iterate through the collection.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |