Last active
March 17, 2024 15:54
-
-
Save crlane/eefb7b6a4baae458c45ea2adf61ed2a6 to your computer and use it in GitHub Desktop.
An implementation of a trie in python using defaultdict and recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
def node(): | |
return defaultdict(node) | |
def word_exists(word, node): | |
if not word: | |
return None in node | |
return word_exists(word[1:], node[word[0]]) | |
def add_word(word, node): | |
if not word: | |
# terminal letter of the word | |
node[None] | |
return | |
add_word(word[1:], node[word[0]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fixed