PruningRadixTree Class

Represents a case-sensitive radix-compressed trie optimized for ranked prefix autocomplete.

public sealed class PruningRadixTree : IEnumerable<KeyValuePair<string, long>>, IEnumerable

Remarks

This collection stores terms with a frequency score and can efficiently retrieve the top-k completions for a prefix. It augments each node with the maximum frequency in its subtree so low-ranking branches can be pruned during lookup.

Typical use cases include search-box suggestions, query completion, and dictionary-backed command palettes.

The structure is case-sensitive. If you need case-insensitive behavior, normalize input before adding or querying.

Example:

var tree = new PruningRadixTree();
tree.Add("car", 10);
tree.Add("cart", 5);
tree.Add("cat", 7);

var suggestions = tree.GetTopTermsByPrefix("ca", topK: 2, out _);
Assert.Equal([KeyValuePair.Create("car", 10L), KeyValuePair.Create("cat", 7L)], suggestions);
Implements

Constructors

NameDescription
PruningRadixTree()Initializes a new instance of PruningRadixTree.

PruningRadixTree()

PruningRadixTree.cs

Initializes a new instance of PruningRadixTree.

public PruningRadixTree()

Properties

NameDescription
CountGets the number of stored terms.

Gets the number of stored terms.

public int Count { get; }

Property value

int

Methods

NameDescription
Add(string, long)Adds a term and its frequency to the tree.
AddRange(IEnumerable<KeyValuePair<string, long>>)Adds a sequence of terms and frequencies to the tree.
ContainsKey(ReadOnlySpan<char>)Determines whether the tree contains a specific term.
GetEnumerator()Returns an enumerator that iterates through all terms in the tree.
GetTopTermsByPrefix(ReadOnlySpan<char>, int, long, bool)Gets the top terms for a given prefix, sorted by frequency descending.
Remove(ReadOnlySpan<char>)Removes a term from the tree.
System.Collections.IEnumerable.GetEnumerator()
TryGetValue(ReadOnlySpan<char>, long)Gets the frequency associated with a term.

Add(string, long)

PruningRadixTree.cs

Adds a term and its frequency to the tree.

public void Add(string term, long frequency)

Parameters

term string
The term to add.
frequency long
The positive frequency value to add to the term.

Exceptions

ArgumentNullException
term is null.
ArgumentOutOfRangeException
frequency is less than or equal to 0.

Remarks

If the term already exists, frequency is added to the existing value.

AddRange(IEnumerable<KeyValuePair<string, long>>)

PruningRadixTree.cs

Adds a sequence of terms and frequencies to the tree.

public void AddRange(IEnumerable<KeyValuePair<string, long>> terms)

Parameters

terms IEnumerable<KeyValuePair<string, long>>
The terms to add.

Exceptions

ArgumentNullException
terms is null.
ArgumentNullException
One of the term keys is null.
ArgumentOutOfRangeException
One of the frequencies is less than or equal to 0.

Remarks

This method is optimized for bulk loading by aggregating duplicate keys and building the radix tree from sorted entries. When the tree already contains more terms than the sequence, the terms are added one by one instead of rebuilding the tree.

ContainsKey(ReadOnlySpan<char>)

PruningRadixTree.cs

Determines whether the tree contains a specific term.

public bool ContainsKey(ReadOnlySpan<char> term)

Parameters

term ReadOnlySpan<char>
The term to locate.

Returns

bool true if the term exists; otherwise, false.

GetEnumerator()

PruningRadixTree.cs

Returns an enumerator that iterates through all terms in the tree.

public IEnumerator<KeyValuePair<string, long>> GetEnumerator()

Returns

IEnumerator<KeyValuePair<string, long>> An enumerator for all stored terms and frequencies.

GetTopTermsByPrefix(ReadOnlySpan<char>, int, long, bool)

PruningRadixTree.cs

Gets the top terms for a given prefix, sorted by frequency descending.

public IReadOnlyList<KeyValuePair<string, long>> GetTopTermsByPrefix(ReadOnlySpan<char> prefix, int topK, out long prefixFrequency, bool pruning = true)

Parameters

prefix ReadOnlySpan<char>
The prefix to search.
topK int
The maximum number of suggestions to return.
prefixFrequency long
When this method returns, contains the exact frequency of prefix if it is a stored term; otherwise, 0.
pruning bool
true to prune non-promising branches using subtree metadata; otherwise, false.

Returns

IReadOnlyList<KeyValuePair<string, long>> A ranked list of matching terms.

Exceptions

ArgumentOutOfRangeException
topK is negative.

Remove(ReadOnlySpan<char>)

PruningRadixTree.cs

Removes a term from the tree.

public bool Remove(ReadOnlySpan<char> term)

Parameters

term ReadOnlySpan<char>
The term to remove.

Returns

bool true when the term existed and was removed; otherwise, false.

System.Collections.IEnumerable.GetEnumerator()

PruningRadixTree.cs
IEnumerator IEnumerable.GetEnumerator()

No documentation is available.

TryGetValue(ReadOnlySpan<char>, long)

PruningRadixTree.cs

Gets the frequency associated with a term.

public bool TryGetValue(ReadOnlySpan<char> term, out long frequency)

Parameters

term ReadOnlySpan<char>
The term to find.
frequency long
When this method returns, contains the associated frequency if found; otherwise, 0.

Returns

bool true if the term exists; otherwise, false.