-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheapbetterimplementation.py
More file actions
31 lines (26 loc) · 944 Bytes
/
heapbetterimplementation.py
File metadata and controls
31 lines (26 loc) · 944 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
class pqele:
def __init__(self, value):
self.val=value
def __repr__(self):
return self.val
class pqelemax(pqele):
def __lt__(self,other):
return self.val>other.val
class pqelemin(pqele):
def __lt__(self,other):
return self.val<other.val
# Usage-
pq = PriorityQueue()
pq.put(PqElement(3)) # Add Item - O(Log(n))
pq.put(PqElement(5)) # Add Item - O(Log(n))
pq.put(PqElement(6)) # Add Item - O(Log(n))
pq.put(PqElement(32)) # Add Item - O(Log(n))
pq.put(PqElement(2)) # Add Item - O(Log(n))
pq.put(PqElement(53)) # Add Item - O(Log(n))
pq.put(PqElement(6)) # Add Item - O(Log(n))
pq.put(PqElement(23)) # Add Item - O(Log(n))
pq.put(PqElement(233)) # Add Item - O(Log(n))
pq.put(PqElement(56)) # Add Item - O(Log(n))
# topValue = pq.get() #Pop top item - O(1)
# topValue = pq.queue[0].val #Get top value - O(1)
print(pq.get())