-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbriandais_struct.py
201 lines (144 loc) · 5.03 KB
/
briandais_struct.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
end_of_word = "\0"
def build_nodes(word):
if len(word) == 0 :
return Node(end_of_word, None, None)
else:
return Node(word[0], None, build_nodes(word[1:]))
def clone(node) :
if node is None :
return None
return Node(node.value, clone(node.sibling), clone(node.child))
def fuse_trees(node, into):
if node is None and into is None :
return None
elif node is None :
return clone(into)
elif into is None :
return clone(node)
value = into.value
child = None
sibling = None
if node.value == into.value :
child = fuse_trees(node.child, into.child)
sibling = fuse_trees(node.sibling,into.sibling)
else :
child = clone(into.child)
sibling = fuse_trees(node,into.sibling)
return Node(value,sibling,child)
class Node():
def __init__(self, value, sibling, child):
self.value = value
self.sibling = sibling
self.child = child
def add_word(self,word):
if len(word) == 0 :
if self.value == end_of_word :
return
elif self.sibling is None :
self.sibling = Node(end_of_word, self.sibling, None)
else :
self.sibling.add_word(word)
elif word[0] == self.value :
if self.child is None :
self.child = build_nodes(word)
else :
self.child.add_word(word[1:])
elif self.sibling is None :
self.sibling = Node(word[0], None, build_nodes(word[1:]))
else :
self.sibling.add_word(word)
def has_word(self, word):
if len(word) == 0 :
if self.value == end_of_word :
return True
elif word[0] == self.value :
if self.child is not None :
return self.child.has_word(word[1:])
return False
if self.sibling is not None :
return self.sibling.has_word(word)
return False
def list_words(self, prefix = ""):
list = []
if self.value == end_of_word :
list.append(prefix)
if self.child is not None :
list += self.child.list_words(prefix + self.value)
if self.sibling is not None :
list += self.sibling.list_words(prefix)
return list
def cpt_words(self):
cpt = 0
if self.value == end_of_word :
cpt += 1
if self.child is not None :
cpt += self.child.cpt_words()
if self.sibling is not None :
cpt += self.sibling.cpt_words()
return cpt
def height(self):
child_height = 0
if self.child is not None :
child_height = 1 + self.child.height()
sibling_height = 0
if self.sibling is not None :
sibling_height = self.sibling.height()
return max(child_height,sibling_height)
def avg_depth(self, depth = 0):
sum = depth
cpt = 1
if self.child is not None :
data = self.child.avg_depth(depth + 1)
sum += data['sum']
cpt += data['cpt']
if self.sibling is not None :
data = self.sibling.avg_depth(depth)
sum += data['sum']
cpt += data['cpt']
return {'sum': sum, 'cpt': cpt}
def cpt_prefix(self,prefix):
cpt = 0
if self.value == end_of_word and len(prefix) == 0:
cpt += 1
elif len(prefix) == 0 or prefix[0] == self.value :
if self.child is not None :
cpt += self.child.cpt_prefix(prefix[1:])
if self.sibling is not None :
cpt += self.sibling.cpt_prefix(prefix)
return cpt
def cpt_nils(self):
cpt = 0
if self.child is not None :
cpt += self.child.cpt_nils()
else :
cpt += 1
if self.sibling is not None :
cpt += self.sibling.cpt_nils()
else :
cpt += 1
return cpt
def accept(self,visitor):
visitor.visit(self)
def delete_word(self,word) :
if len(word) == 0 :
if self.value == end_of_word :
return {'found': True, 'deleted': False}
elif self.sibling is None :
return {'found': False, 'deleted': False}
elif word[0:1] == self.value :
if self.child is None :
return {'found': False, 'deleted': False}
else :
state = self.child.delete_word(word[1:])
if state['found'] is True and state['deleted'] is False :
self.child = self.child.sibling
if self.child is not None :
state['deleted'] = True
return state
elif self.sibling is None :
return {'found': False, 'deleted': False}
state = self.sibling.delete_word(word)
if state['found'] is True and state['deleted'] is False :
self.sibling = self.sibling.sibling
state['deleted'] = True
return state