-
Notifications
You must be signed in to change notification settings - Fork 2
/
trie_oop.py
176 lines (143 loc) · 4.69 KB
/
trie_oop.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
class Trie(object):
""" root node """
# empty constructor
def __init__(self):
self.root = Node('*')
self.nrWords = 0
self.nrLetters = 0
self.size = 0
# 'inherit' the method addWord from Node
def addWord(self, word):
self.nrWords = self.nrWords + 1
self.nrLetters = self.nrLetters + len(word)
self.size = self.size + self.root.addWord(word)
return True
# 'inherit' the method hasWord from Node
def hasWord(self, word):
return self.root.hasWord(word)
# print all information method
def display(self):
# print(self.size)
self.root.display()
# getter words for nrWords
def countWords(self):
return self.nrWords
# getter method for nrLetters
def countLetters(self):
return self.nrLetters
# getter method for size
def getSize(self):
return self.size
# compute compression
def compression(self):
return self.nrLetters / self.size
# create graphviz Digraph
def diagram(self, filename, render):
from graphviz import Digraph
from queue import Queue
diagram = Digraph(comment='The Trie')
i = 0
diagram.attr('node', fontsize='4')
diagram.attr('node', height='0.1')
diagram.attr('node', width='0.1')
diagram.attr('node', fixedsize='true')
diagram.attr('node', shape='circle')
diagram.attr('edge', arrowsize='0.3')
diagram.node(str(i), self.root.getValue())
q = Queue()
q.put((self.root, i))
while not q.empty():
node, parent_index = q.get()
for child in node.getChildren():
#print('current parent: ', node.getValue(), parent_index)
i += 1
#print('current child: ', child.getValue(), i)
if child.getEnding():
diagram.attr('node', shape='diamond')
diagram.node(str(i), child.getValue())
diagram.attr('node', shape='circle')
else:
diagram.node(str(i), child.getValue())
diagram.edge(str(parent_index), str(i))
q.put((child, i))
o = open(filename + '.gv', 'w')
o.write(diagram.source)
# print(diagram.source)
if render:
diagram.render(filename + '.gv', view=False)
o.close()
class Node(object):
""" any node """
# empty constructor
def __init__(self, val):
self.value = val
self.children = []
self.ending = False
def __str__(self):
return str(self.value)
# setter method for value
def setValue(self, val):
self.value = val
# adds a child
def add_child(self, child):
self.children.append(child)
# remove a child
def rem_child(self, child):
if self.contains_child(child):
self.children.remove(child)
# setter method for ending
def setEnding(self, T_or_F):
self.ending = T_or_F
# check if a letter of interest is already a child
def contains_child(self, letter):
if letter in self.children:
return True
return False
# getter method for value
def getValue(self):
return self.value
# getter method for children
def getChildren(self):
return self.children
# getter method for ending
def getEnding(self):
return self.ending
def addWord(self, word):
size_increment = 0
if word == '':
self.setEnding(True)
return size_increment # stops the recursion
result = None
for child in self.children:
if child.value == word[0]:
result = child
return size_increment + result.addWord(word[1:])
if result == None:
new_child = Node(word[0])
self.add_child(new_child)
size_increment = size_increment + 1
return size_increment + new_child.addWord(word[1:])
return size_increment
# checks if word is in the Trie
def hasWord(self, word):
if word == '':
if self.getEnding():
return True
else:
return False
for child in self.children:
# print(child.value)
if child.value == word[0]:
return True and child.hasWord(word[1:])
return False
# prints object
def display(self):
if self.getEnding():
ending = '_'
else:
ending = ' '
print(ending, self.value, ending)
child_nodes = self.getChildren()
for node in child_nodes:
node.display()
return True