-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriorityQueue.py
46 lines (37 loc) · 1.38 KB
/
PriorityQueue.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
import queue
class PriotityQueue:
def __init__(self):
self.__queue = []
def estaVacia(self):
return len(self.__queue) == 0
def insertar(self, nodoInsertar):
costoNodo = nodoInsertar.getCostoCamino()
indice = 0
for nodo in self.__queue:
if nodo.getCostoCamino() <= costoNodo:
indice+=1
else:
break
if indice == -1:
self.__queue.insert(0,nodoInsertar)
else:
self.__queue.insert(indice,nodoInsertar)
def pop(self):
retVal = self.__queue[0]
del self.__queue[0]
return retVal
#función que busca si un estado ya se encuentra dentro de la priority queue y retorna el indice
def seEncuentra(self, nodoBuscar):
for i, nodo in enumerate(self.__queue):
if nodoBuscar.getEstado() == nodo.getEstado():
return i
return -1
#función que intercambia el nodo, si el nodo recibido tiene un menor costo
def intercambiarMejorEstado(self, nodoComparar, indice):
nodoActual = self.__queue[indice]
if nodoActual.getCostoCamino() > nodoComparar.getCostoCamino():
del self.__queue[indice]
self.insertar(nodoComparar)
#función que retorna el size de la priority queue
def size(self):
return len(self.__queue)