You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A small program to visualize a trie data structure in the form of decision tree graph.
3
+
'''
4
+
#imports
5
+
importjson
6
+
importpydot
7
+
8
+
# function to return a new dict template
9
+
defstruct():
10
+
struct= {
11
+
12
+
}
13
+
returnstruct
14
+
15
+
# getting list of words as input from the file
16
+
file_=open('./input.txt', 'r')
17
+
file_text=file_.read()
18
+
file_len=len(file_text)
19
+
file_.close()
20
+
21
+
# trie making stuff happening (hard to explain)
22
+
tmp_s=struct()
23
+
root=tmp_s
24
+
forlineinfile_text.split('\n'):
25
+
forcinline.split('.'):
26
+
ifcnotintmp_s:
27
+
tmp_s[c] =struct()
28
+
tmp_s=tmp_s[c]
29
+
tmp_s=root
30
+
cur_word= []
31
+
32
+
# saving the trie in a json file
33
+
withopen('output.json', 'w') asfp:
34
+
json.dump(root, fp, indent=4)
35
+
36
+
# converting and saving the trie to dot language decision tree graph using pydot
37
+
# // code taken and modified from stackoverflow (https://stackoverflow.com/questions/13688410/dictionary-object-to-decision-tree-in-pydot)
0 commit comments