You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

KTrie

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

KTrie

Trie (a.k.a. prefix tree) is an ordered tree data structure that is used to store an associative array where the keys are usually strings. All the descendants of a node have a common prefix of the string associated with that node, and the root is associated with the empty string.

3.0.1
Source
nugetNuGet
Version published
Maintainers
1
Created
Source

Trie

Trie (a.k.a. prefix tree) is an ordered tree data structure that is used to store an associative array where the keys are usually strings. All the descendants of a node have a common prefix of the string associated with that node, and the root is associated with the empty string.
Reference: Wikipedia

CI Build Nuget

Advantages

  • Looking up keys is faster. Looking up a key of length key takes O(|key|) time
  • Looking up prefixes is faster. Looking up a prefix takes O(|prefix|) time
  • Removing takes O(|key|) time
Trie trie = ["star", "start", "stack", "stop", "stay", "key"];

          {root}
            /\
           s  k
          /    \
         t      e
        / \      \
      a    o     [y]
    / | \    \
  [r][y] c   [p]
  /       \
[t]       [k]

where [char] -- is end of word

The library provides two implementations of the trie data structure:

  • Trie — implements ICollection<string> for storing unique strings.
  • TrieDictionary<TValue> — implements IDictionary<string, TValue> for key-value pairs.

Usage

Initialization

// Initialization
TrieDictionary<int> trie = [];

// or using constructor with comparer
IEqualityComparer<char> comparer = ...; // specify the comparer
TrieDictionary<int> trieWithComparer = new(comparer);

Adding items to the trie

trie.Add("key", 17);
  • Add throws an ArgumentNullException if the key is null, and ArgumentException if the key already exists.
  • You can overwrite existing values using the indexer:
trie["key"] = 42;

(similar to Dictionary<TKey, TValue>)

Prefix Lookup

The main benefit of a Trie is extremely fast prefix lookup.

var result = trie.EnumerateByPrefix("abc");

This returns all key-value pairs where the key starts with the prefix "abc".

There are multiple overloads of the EnumerateByPrefix method.

  • EnumerateByPrefix(string prefix)
  • EnumerateByPrefix(IReadOnlyList<Character> pattern)
  • EnumerateByPrefix(ReadOnlySpan<char> prefix)

Pattern Matching

The EnumerateMatches method supports character pattern-based search:

var result = trie.EnumerateMatches([Character.Any, 'c', Character.Any, Character.Any, 't']);

This matches words like octet, scout, or scoot using the regex-like pattern: ^.c.{2}t$.

API Methods

The API methods support efficient, allocation-free prefix matching with ReadOnlySpan<char>

string? LongestPrefixMatch(string input)

Returns the longest prefix of the specified input that exists as a full key in the trie. This is useful for IP routing, token parsing, or search suggestion scenarios where the longest valid prefix match is needed.

string? matched = trie.LongestPrefixMatch("starting");

For example, if the trie contains "start", "star", and "stack", calling LongestPrefixMatch("starting") will return "start".

bool TryAdd(string key, TValue value)

Attempts to add the specified key and value to the TrieDictionary.
Returns true if the key/value pair was added successfully; false if the key already exists.

bool success = trie.TryAdd("alpha", 1);

bool Remove(string key)

Removes the value with the specified key from the TrieDictionary.
Returns true if the element is successfully removed; false if the key was not found.

bool removed = trie.Remove("key");

bool ContainsKey(string key)

Determines whether the TrieDictionary contains the specified key.

bool containsKey = trie.ContainsKey("key");

bool TryGetValue(string key, out TValue value)

Attempts to get the value associated with the specified key.
Returns true if the key is found; otherwise, false.

bool keyFound = trie.TryGetValue("key", out int value);

Benchmark tests

For performance tests I used 370105 English words (from: https://github.com/dwyl/english-words).

MethodMeanErrorStdDevAllocated
Load_Trie261,817.07 us4,915.639 us5,851.719 us88595.87 KB
Load_DictionaryWithAllPrefixes645,602.56 us12,695.465 us11,875.345 us315235.58 KB
Load_DictionaryGroupedByFirstLetter13,664.71 us217.810 us193.083 us8691 KB
EnumerateByPrefix_Trie8,911.70 us157.403 us131.439 us2843 KB
StartsWith_String106,177.20 us1,081.102 us1,011.264 us2840.66 KB
StartsWith_Span29,212.79 us229.247 us178.981 us2840.66 KB
StartsWith_Linq_GroupedByFirstLetter10,859.52 us95.205 us79.501 us2844.41 KB
StartsWith_DictionaryWithAllPrefixes3,929.85 us77.316 us89.037 us2840.66 KB
Trie_EnumerateMatches13.96 us0.074 us0.066 us17.99 KB
Trie_Pattern_EnumerateByPrefix60.45 us0.879 us0.779 us50.09 KB
String_PatternMatching931.86 us9.207 us7.188 us1.56 KB
String_PrefixPatternMatching981.95 us18.877 us18.540 us33.72 KB
Regex_PatternMatching29,577.54 us210.987 us176.184 us1.56 KB
Regex_PrefixPatternMatching28,648.33 us437.859 us409.574 us33.72 KB

© Kirill Polishchuk

Keywords

trie

FAQs

Package last updated on 17 Jul 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts