-
Notifications
You must be signed in to change notification settings - Fork 0
/
PriorityQueue.py
41 lines (37 loc) · 1.16 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
""" Custom PriorityQueue class """
class PQ():
def __init__(self):
self.queue = []
# for checking if the queue is empty
def notEmpty(self):
return len(self.queue) > 0
# insert element into queue, handles special cases where elements have same priority or same node with diff priorities
def insert(self, new_priority, new_node):
if(len(self.queue) > 0):
for i in range(len(self.queue)):
old_priority, old_node = self.queue[i]
if (new_node.state == old_node.state):
if(new_priority >= old_priority):
return
else:
del self.queue[i]
self.queue.append((new_priority, new_node))
return
self.queue.append((new_priority, new_node))
else:
self.queue.append((new_priority, new_node))
# remove element with highest priority and return it
def pop(self):
try:
min = 0
for i in range(len(self.queue)):
new = self.queue[i][0]
old = self.queue[min][0]
if new < old:
min = i
item = self.queue[min]
del self.queue[min]
return item
except IndexError:
print()
exit()