1 """
2 Implementación del algoritmo de colas de prioridad con listas o "arreglos".
3 Es propiedad de Diego Andrés Sanabria y es liberado bajo la licencia GPL v2.0
4 """
5 def mayor(a,b):
6 if a>=b:
7 return a
8 else:
9 return b
10
11 class MonticuloArreglo:
12 arreglo=[]
13 def __init__(self):
14 self.arreglo.append(None)
15 def insertar(self,dato):
16 try:
17 print "El dato ya esta en la posición "+str(self.arreglo.index(dato))
18 except:
19 self.arreglo.append(dato)
20 self.acomodar(self.arreglo.index(dato))
21 def acomodar(self, pos):
22 if pos/2==0:
23 pass
24 else:
25 if self.arreglo[pos]>self.arreglo[pos/2]:
26 tmp=self.arreglo[pos]
27 self.arreglo[pos]=self.arreglo[pos/2]
28 self.arreglo[pos/2]=tmp
29 self.acomodar(pos/2)
30 else:
31 pass
32 def atender(self, pos=1):
33 if pos*2<len(self.arreglo) and (pos*2+1) <len(self.arreglo):
34 may=mayor(self.arreglo[pos*2],self.arreglo[pos*2+1])
35 ind=self.arreglo.index(may)
36 self.arreglo[pos]=self.arreglo[ind]
37 self.atender(ind)
38 elif pos*2<len(self.arreglo) and not ((pos*2+1)<len(self.arreglo)):
39 self.arreglo[pos]=self.arreglo[pos*2]
40 self.atender(pos*2)
41 else:
42 if pos==len(self.arreglo)-1:
43 self.arreglo.pop()
44 elif pos==1 and len(self.arreglo)==1:
45 pass
46 else:
47 self.arreglo[pos]=self.arreglo.pop()
48 def __repr__(self):
49 return str(self.arreglo[1:])