-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.py
97 lines (82 loc) · 2.55 KB
/
trie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class Trie:
def __init__(self):
self.children = {}
self.leaf = False
def matches(self, str):
return self._matches(str, "")
def _matches(self, str, prefix):
has_more = len(str) > 0
has_children = len(self.children) > 0
if not has_more and not has_children:
return [prefix]
if has_more and (not has_children or str[0] not in self.children):
return []
if has_more and has_children:
return self.children[str[0]]._matches(str[1:], prefix + str[0])
if not has_more and has_children:
l = []
if self.leaf:
l = [prefix]
for k, child in self.children.items():
l += child._matches("", prefix + k)
return l
def add(self, str):
if str == "":
self.leaf = True
return
char = str[0]
if char not in self.children:
self.children[char] = Trie()
self.children[char].add(str[1:])
def load(self, trie_dict):
for k, v in trie_dict.items():
if k == "$":
self.leaf = True
else:
trie = Trie()
trie.load(v)
self.children[k] = trie
def remove(self, str):
if str == "":
return
def _remove(str, trie, i=0):
if len(str) == i:
# we found the leaf
trie.leaf = False
return len(trie.children) == 0
# we haven't yet found the leaf
if _remove(str, trie.children[str[i]], i+1):
del trie.children[str[i]]
# it's not a leaf and only direct child was removed so this one can be too
return not trie.leaf and len(trie.children) == 0
return False
_remove(str, self)
def dict(self):
children = { k : v.dict() for k,v in self.children.items()}
if self.leaf:
# {$:1} indicates leaf / the end of a valid string
# todo: this prevents using $ in lexicon, should be configurable or use diff data structure
return { "$": 1 } | children
return children
# trie = Trie()
# trie.add("twigs")
# trie.add("twig")
# trie.add("twitter")
# trie.add("twitch")
# trie.add("twilight")
# trie.add("twinky")
# # trie.add("tough")
# # trie.add("thought")
# print("tw", trie.matches("twi"))
# print("twi", trie.matches("twi"))
# print("twit", trie.matches("twit"))
# print("twitt", trie.matches("twitt"))
# # print("th", trie.matches("th"))
# print(trie.dict())
# for i in ["twigs", "twig", "twitter", "twitch", "twilight", "twinky"]:
# trie.remove(i)
# print(f"removed {i}")
# print("tw", trie.matches("twi"))
# print("twi", trie.matches("twi"))
# print("twit", trie.matches("twit"))
# print("twitt", trie.matches("twitt"))