-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhybrid_structure.py
245 lines (197 loc) · 7.53 KB
/
hybrid_structure.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
end_of_word = "\0"
def build_nodes(word):
if len(word) == 0:
return Node(end_of_word, None, None, None)
else:
return Node(word[0], None, build_nodes(word[1:]), None)
def insert(node,into):
if node.value == into.value :
if into.eq is None :
into.eq = node.eq
elif node.eq is not None :
insert(node.eq, into.eq)
if node.value > into.value :
if into.sup is None :
into.sup = node
else :
insert(node,into.sup)
if node.value < into.value :
if into.inf is None :
into.inf = node
else :
insert(node,into.inf)
def clone(node) :
if node is None :
return None
return Node(node.value, clone(node.inf), clone(node.eq), clone(node.sup))
class Node():
def __init__(self, value, inf, eq, sup):
self.value = value
self.inf = inf
self.eq = eq
self.sup = sup
def has_word(self, word):
if len(word) == 0:
if self.value == end_of_word :
return True
elif self.inf is not None:
return self.inf.has_word(word)
else:
return False
elif self.value == word[0]:
if self.eq is None:
return False
else:
return self.eq.has_word(word[1:])
elif self.value > word[0]:
if self.inf is None:
return False
else:
return self.inf.has_word(word)
elif self.value < word[0]:
if self.sup is None:
return False
else :
return self.sup.has_word(word)
def add_word(self,word):
#print word
if len(word) == 0:
if self.value == end_of_word:
return
# On ne veut jamais ajouter dans self.eq
# Heuresement il n'est jamais nul si value != EOW
#elif self.eq is None :
# self.eq = Node(end_of_word, None, None, None)
elif self.inf is None:
self.inf = Node(end_of_word, None, None, None)
else:
self.inf.add_word(word)
elif word[0] == self.value:
if self.eq is not None:
self.eq.add_word(word[1:])
else:
self.eq.build_nodes(word[1:])
elif word[0] < self.value:
if self.inf is None :
self.inf = build_nodes(word)
else :
self.inf.add_word(word)
elif word[0] > self.value:
if self.sup is None:
self.sup = build_nodes(word)
else:
self.sup.add_word(word)
def del_word(self,word):
if len(word) == 0 :
if self.value == end_of_word :
return {'found': True, 'deleted': False}
elif self.inf is not None :
state = self.inf.del_word(word)
if state['found'] == True and state['deleted'] == False :
if self.inf.sup is None and self.inf.inf is None :
self.inf = None
state['deleted'] = True
else :
inf = self.inf
self.inf = inf.sup
if self.inf is None :
self.inf = inf.inf
elif inf.inf is not None :
insert(inf.inf,self.inf)
state['deleted'] = True
else :
return {'found': False, 'deleted': False}
elif self.value == word[0]:
if self.eq is not None:
state = self.eq.del_word(word[1:])
if state['found'] == True and state['deleted'] == False :
if self.eq.sup is None and self.eq.inf is None :
self.eq = None
else :
eq = self.eq
self.eq = eq.sup
if self.eq is None :
self.eq = eq.inf
elif eq.inf is not None :
insert(eq.inf,self.eq)
if self.eq is not None :
state['deleted'] = True
else:
return {'found': False, 'deleted': False}
elif self.value > word[0]:
if self.inf is not None :
state = self.inf.del_word(word)
if state['found'] == True and state['deleted'] == False :
if self.inf.sup is None and self.inf.inf is None :
self.inf = None
state['deleted'] = True
else :
inf = self.inf
self.inf = inf.sup
if self.inf is None :
self.inf = inf.inf
elif inf.inf is not None :
insert(inf.inf,self.inf)
state['deleted'] = True
else:
return {'found': False, 'deleted': False}
elif self.value < word[0]:
if self.sup is not None:
state = self.sup.del_word(word)
if state['found'] == True and state['deleted'] == False :
if self.sup.sup is None and self.sup.inf is None :
self.sup = None
state['deleted'] = True
else :
sup = self.sup
self.sup = sup.sup
if self.sup is None :
self.sup = sup.inf
elif sup.inf is not None :
insert(sup.inf,self.sup)
state['deleted'] = True
else :
return {'found': False, 'deleted': False}
return state
def prefixe(self, word, total=0):
if len(word) == 0:
total += self.compte_mots(0)
else:
if word[0] == self.value:
if self.eq is not None :
total += self.eq.prefixe(word[1:],total)
else:
return total
elif word[0] < self.value:
if self.inf is not None :
total += self.inf.prefixe(word,total)
else:
return total
elif word[0] > self.value:
if self.sup is not None:
total += self.sup.prefixe(word,total)
else:
return total
return total
def print_hybrid_trie(self, depth=0):
ret = ""
if self.inf is not None:
ret += ""+self.inf.print_hybrid_trie(depth + 1)
if self.eq is not None:
ret += ("*"*(depth-1)) + str(self.value) + self.eq.print_hybrid_trie(depth + 1)
else:
ret += "\n"+ (""*depth) +str(self.value)
if self.sup is not None:
ret += ""+self.sup.print_hybrid_trie(depth + 1)
return ret
def compte_mots(self, n):
cpt = 0
if self.value == end_of_word:
cpt+=1
if self.eq is not None :
cpt += self.eq.compte_mots(cpt)
if self.inf is not None:
cpt += self.inf.compte_mots(cpt)
if self.sup is not None:
cpt += self.sup.compte_mots(cpt)
return cpt