- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPriorityQueue.py
More file actions
Latest commit
37 lines (28 loc) · 781 Bytes
/
Copy pathPriorityQueue.py
File metadata and controls
37 lines (28 loc) · 781 Bytes
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
importHeap
# implement priority queue
classNode(object):
def__init__(self,data=None,priority=None):
self.data=data
self.priority=priority
def__repr__(self):
return"%s %d"% (self.data, self.priority)
classPriorityQueue(object):
def__init__(self):
# self._heap = Heap.MinHeap(key=lambda x: -x.priority)
self._heap=Heap.MaxHeap(key=lambdax: x.priority)
defadd(self, data, priority):
node=Node(data=data, priority=priority)
self._heap.insert(node)
defremove(self):
returnself._heap.pop()
defsize(self):
returnself._heap.size()
pq=PriorityQueue()
pq.add("Task 1", 5)
pq.add("Task 2", 10)
pq.add("Task 3", 1)
pq.add("Task 4", 3)
pq.add("Task 5", 2)
pq.add("Task 6", 7)
whilepq.size():
printpq.remove()