diff --git a/content/posts/2024-06-30-trie/index.md b/content/posts/2024-06-30-trie/index.md index 32b6fb8..584e856 100644 --- a/content/posts/2024-06-30-trie/index.md +++ b/content/posts/2024-06-30-trie/index.md @@ -34,3 +34,32 @@ spell correction, as it can easily be used to fuzzy match words under a given edit distance (think [Levenshtein distance]) [Levenshtein distance]: https://en.wikipedia.org/wiki/Levenshtein_distance + +## Implementation + +This implementation will be in Python for exposition purposes, even though +it already has a built-in `dict`. + +### Representation + +Creating a new `Trie` is easy: the root node starts off empty and without any +mapped values. + +```python +class Trie[T]: + _children: dict[str, Trie[T]] + _value: T | None + + def __init__(self): + # Each letter is mapped to a Trie + self._children = defaultdict(Trie) + # If we match a full string, we store the mapped value + self._value = None +``` + +We're using a `defaultdict` for the children for ease of implementation in this +post. In reality, I would encourage you exit early when you can't match a given +character. + +The string key will be implicit by the position of a node in the tree: the empty +string at the root, one-character strings as its direct children, etc...