Skip to content

Instantly share code, notes, and snippets.

@shashfrankenstien
Last active October 18, 2018 04:14
Show Gist options
  • Save shashfrankenstien/1b9c79169c506344f30a48049582fb64 to your computer and use it in GitHub Desktop.
Save shashfrankenstien/1b9c79169c506344f30a48049582fb64 to your computer and use it in GitHub Desktop.
Trying trie data structures
class TrieNode(object):
def __init__(self, c):
self.c = c
self.children = {}
self.suffix = 0
def add(self, child):
if not child:
self.suffix +=1
return
if child[0] not in self.children:
self.children[child[0]] = TrieNode(child[0])
self.children[child[0]].add(child[1:])
def __repr__(self):
return str((self.c, self.suffix, self.children.values()))
def build_trie(words):
base = TrieNode(None)
for w in words:
print('adding', w)
base.add(w)
return base
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment