-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie
More file actions
34 lines (24 loc) · 628 Bytes
/
Trie
File metadata and controls
34 lines (24 loc) · 628 Bytes
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
from collections import defaultdict
class Trie():
def __init__(self):
self.trie = {}
def append(self, string):
new_pointer = self.trie
for letter in string:
new_pointer = new_pointer.setdefault(letter, {})
T = Trie()
s = "AAAA"
T.append(s)
from collections import defaultdict
class Trie2:
available = 1
def __init__(self):
self.label = str(Trie.available)
self.child_nodes = defaultdict(Trie)
Trie.available += 1
def add_child(self, child):
if not child: return
self.child_nodes[child[0]].add_child(child[1:])
T = Trie2()
s=["AAAA"]
T.add_child(s)