-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArbolNArio.py
331 lines (257 loc) · 6.69 KB
/
ArbolNArio.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
from Nodo import Nodo
class ArbolNArio:
raiz= None
ramas=[]
preOrd=[]
inOr=[]
postOr=[]
hojas=[]
def __init__(self,dato):
''''dato is the id of root Node.constructor of class ArboNario'''
self.raiz = Nodo (dato)
def agregar(self,n,padre,dato):
'''n is the root -padre is the father -dato is id of new Node.its can to add a new Node to the tree'''
if not n is None:
#print("raiz", n.dato, " padre",padre," dato",dato)
if n.dato== padre:
#print("encontrado")
if len(n.hijos)<3:
n.agregarHijo(dato)
#print("padre :",n.dato," hijo",dato)
else:
print("NO se pude agregar ya tiene 3 hijos")
else:
for h in n.hijos:
self.agregar(h,padre,dato)
def mostrar (self,n):
'''its show one by one the Node and their childrens'''
if not n is None:
print("Nodo: ",n.dato,"\n")
for h in n.hijos:
print("Hijos de ",n.dato,":",h.dato,end="\n")
for hh in n.hijos:
self.mostrar(hh)
def profundidad(self,r,d,n):
if r is not None:
if r.dato==d:
print(n)
else:
for h in r.hijos:
self.profundidad(h,d,n+1)
n-=1
def anchura(self,n):
'''n- is the root. its show the breadth path'''
if n is not None:
cola=[]
temp=None
cola.append(n)
while len(cola)>0:
temp=cola.pop(0)
print(temp.dato,end=" ")
for h in temp.hijos:
cola.append(h)
def altura(self,n):
'''its show largest tree size'''
if n is None:
return 0
else:
return 1+max((self.altura(h)for h in n.hijos),default=0)
def obtenerRamas(self,n,lst):
''' n is the root -lst is temporal list ways . it can to obtain all ways. Please include the root tree in the lst'''
if n is not None:
if n.hijos==[]:
lt=[]
for l in lst:
lt.append(l)
self.ramas.append(lt)
return lst
for h in n.hijos:
lst.append(h)
self.obtenerRamas(h,lst)
lst.pop()
def nivelNodo(self,r,d,l):
'''r is the root -d is data. it can to obtain the level of data'''
if r is not None:
if r.dato == d:
return l
for h in r.Hijos:
self.nivelNodo(h,d,l+1)
def preOrder(self,r,n):
'''r is root -n is flag. it can show the inOrder path'''
if r is not None:
preOrd.append(r)
for h in r.hijos:
self.preOrder(h,n+1)
n-=1
def postOrder(self,r):
if r is not None:
if r.hijos==[]:
postOr.append(r)
return
for x in r.hijos:
self.postOrder(x)
postOr.append(r)
def inOrder(self,r):
if r is not None:
if r.hijos==[]:
self.inOr.append(r)
return
for h in r.hijos:
self.inOrder(h)
if r not in self.inOr:
self.inOr.append(r)
def EncuentraHojas(self,r):
if r is not None:
if r.hijos==[]:
hojas.append(r)
for x in r.hijos:
self.inOrder(x)
def nodosNivel(self,r):
'''r is the root of tree. it can to obtain all node by level'''
if r is not None:
colaNodos=[]
colaNodos.append(r)
contadorNodos=0
listaNNodos=[]
contadorNodostemp=0
listaNNodos.append(1)
temp=colaNodos.pop(0)
for h in temp.hijos:
colaNodos.append(h)
contadorNodos+=1
listaNNodos.append(contadorNodos)
while colaNodos:
if contadorNodos!=0:
temp=colaNodos.pop(0)
for h in temp.hijos:
colaNodos.append(h)
contadorNodostemp+=1
contadorNodos-=1
else:
listaNNodos.append(contadorNodostemp)
contadorNodos=contadorNodostemp
contadorNodostemp=0
return listaNNodos
def nodoById(self,r,d):
'''r is the tree root -d is the id of search Node. it make a search in the tree by Id'''
'''este metodo no funciona mejor usar buscarById'''
if r is not None:
if r.dato==d:
return r
for h in r.hijos:
self.nodoById(h,d)
def listaAnchura(self,r):
'''r is the tree root . it make list in breadth '''
if r is not None:
list=[]
cola=[]
temp=None
cola.append(r)
while cola:
temp=cola.pop(0)
list.append(temp)
for h in temp.hijos:
cola.append(h)
return list
def eliminar(self,r,f,d):
'''it delete a Node'''
l=[]
if r is not None:
if r.dato==f:
i=0
for h in r.hijos:
if h.dato==d:
if h.hijos==[]:
print("dato",h.dato)
r.hijos.remove(self.buscarById(self.raiz,d))
else:
l=self.nodosNivel(self.raiz)
if l[i]<=(3**i):
hh=h.hijos
r.hijos.remove(h)
for j in hh:
print(j.dato)
print("valor r",r.dato)
self.agregarAdoptado(r,self.buscarById(self.raiz,r.dato),j.dato)
i+=0
for child in r.hijos:
self.eliminar(child,f,d)
def agregarAdoptado(self,r,p,d):
'''it add child to anyone'''
if r is not None:
if r.dato==p.dato:
print("encontrado...")
if r.hijos==[]:
self.agregar(r,r.dato,d)
return
for hp in p.hijos:
if hp.tieneEspacio():
self.agregar(self.raiz,hp.dato,d)
print("se agrego ",d)
return
else:
for hph in hp.hijos:
self.agregarAdoptado(self.raiz,hph,d)
return
for h in r.hijos:
print("bscando...",h.dato)
self.agregarAdoptado(h,p,d)
''''
if p.tieneEspacio():
print("agregando ",d, " a",p.dato)
self.agregar(self.raiz,r,d,"blue")
else:
for ad in p.hijos:
self.agregarAdoptado(r,ad,d)
'''
def buscarById(self,r,id):
'''r is the tree root -id is the data of Node. it can to obtain a Node By id'''
if r is not None:
l= self.listaAnchura(self.raiz)
for child in l:
if child.dato==id:
return child
def cambioId(self,idI,idF):
''' idI: it will change -idF it well be new id. it can to change id's'''
nodo=self.buscarById(self.raiz,idI)
nodo.cambioId(idF)
def esCompleto(self,r):
'''the tree is complete '''
if r is not None:
l=self.nodosNivel(r)
r=True
for k in range(0,len(l)):
if 3**k!=l[k]:
r=False
break
return r
def esFull(self,r):
if r is not None:
l=self.nodosNivel(r)
r=True
for m in range(0,len(l)-1):
if 3**m !=l[m]:
r=False
return r
def ramaLarga(self,r):
''''the way largest'''
if r is not None :
self.obtenerRamas(r,[r])
r=self.ramas
lg=r[0]
for l in r:
if len(l)>len(lg):
lg=l
return lg
def numNodos(self,r):
if r is not None:
l=self.nodosNivel(r)
nnodos=0
for i in l:
nnodos+=i
return nnodos
def textrama(self,l):
v =""
for x in l:
v+=str(x.dato)+" "
return v