-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.py
More file actions
61 lines (50 loc) · 1.7 KB
/
trie.py
File metadata and controls
61 lines (50 loc) · 1.7 KB
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
# create Trie data structure
from trie_node import TrieNode
from collections import deque
class Trie:
def __init__(self):
self.root = TrieNode(None)
self.count = 0
def dfs_traversal_print(self):
if self.root == None:
print("None")
return False
print (self.root.value)
for child in self.root.children:
# print(child)
self.dfs_helper(self.root.children[child])
def dfs_helper(self, node_to_search_through):
# print(parent.children[node_to_search_through])
if len(node_to_search_through.children) == 0:
print (node_to_search_through.value)
return
else:
print (node_to_search_through.value)
for child in node_to_search_through.children:
self.dfs_helper(node_to_search_through.children[child])
def bfs_traversal(self):
if self.root == None:
return False
print (self.root.value)
queue = deque()
for index in self.root.children:
queue.append(self.root.children[index])
while len(queue) > 0:
val = queue.pop()
print(val.value)
for child in val.children:
queue.append(val.children[child])
return True
if __name__ == "__main__":
var = Trie()
# var.root = TrieNode(1)
# print(var.root.value)
val = TrieNode(1)
val.add_child(2)
val.children[2].add_child(6)
val.children[2].children[6].add_child(7)
val.add_child(3)
var.root = val
var.dfs_traversal_print()
print("_____")
var.bfs_traversal()