Skip to content

Instantly share code, notes, and snippets.

@ruofeidu
Forked from crlane/trie.py
Last active July 12, 2018 18:58
Show Gist options
  • Save ruofeidu/21af3dfcf6863700a30ceabc0da56221 to your computer and use it in GitHub Desktop.
Save ruofeidu/21af3dfcf6863700a30ceabc0da56221 to your computer and use it in GitHub Desktop.
An implementation of a trie in python using defaultdict and recursion
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