Data Structures and Algorithms • C# examples
Tries and Prefix Search
Store strings by shared prefixes to support dictionary lookup and autocomplete.
Concept
A trie follows one edge per symbol, sharing common prefixes among keys. Lookup cost depends on key length rather than the number of stored words, but node-per-character representations can use substantial memory. Compressed radix tries reduce chains of single-child nodes.
How to approach it
Mark terminal nodes separately from child links so that one stored word can also be a prefix of another.
Complexity
Lookup O(L) for key length L; memory depends on alphabet and representation.
C# example
sealed class TrieNode
{
public Dictionary<char, TrieNode> Children { get; } = new();
public bool IsWord { get; set; }
}
Expected result or behavior
Each node stores outgoing character edges and whether a complete key ends there.
Practice exercise
Describe how to enumerate all words with a given prefix without scanning unrelated entries.
Key takeaway: Tries trade extra structure for prefix-oriented operations.