Last active
October 18, 2018 04:14
-
-
Save shashfrankenstien/1b9c79169c506344f30a48049582fb64 to your computer and use it in GitHub Desktop.
Trying trie data structures
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
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