-
Notifications
You must be signed in to change notification settings - Fork 0
/
priority queue.py
71 lines (62 loc) · 2.13 KB
/
priority queue.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Node:
def __init__(self,v,p):
self.value=v
self.Priority=p
class PriorityQueue:
def __init__(self,val,pr):
newNode=Node(val,pr)
self.values=[newNode]
def getParent(self,pr):
return (pr-1)//2
def getRight(self,pr):
return 2*pr + 2
def getLeft(self,pr):
return 2*pr + 1
def add(self,v,pr):
newNode=Node(v,pr)
self.values.append(newNode)
p=newNode.Priority
idx=len(self.values) - 1
while self.values[self.getParent(idx)].Priority > self.values[idx].Priority:
temp=self.values[self.getParent(idx)]
self.values[self.getParent(idx)]=self.values[idx]
self.values[idx]=temp
idx=self.getParent(idx)
if idx <=0: #this is important
break
def toswapwith(self,idx):
left=self.getLeft(idx)
right=self.getRight(idx)
if self.getLeft(idx) > len(self.values) - 1:
return self.getRight(idx)
if self.getRight(idx) > len(self.values) - 1:
return self.getLeft(idx)
return self.getRight(idx) if self.values[self.getRight(idx)] > self.values[self.getLeft(idx)] else self.getLeft(idx)
def remove(self):
temp=self.values[0]
self.values[0]=self.values[-1]
self.values[-1]=temp
m=self.values.pop()
idx=0
im=self.toswapwith(idx)
if im > len(self.values) - 1:
return 0
while self.values[self.toswapwith(idx)].Priority < self.values[idx].Priority:
temp=self.values[self.toswapwith(idx)]
self.values[self.toswapwith(idx)]=self.values[idx]
self.values[idx]=temp
idx=self.toswapwith(idx)
if idx > len(self.values) - 1 or self.toswapwith(idx) > len(self.values) - 1: #important
break
return m
def prints(obj):
for i in obj.values:
print(i.value,end=" ")
print(i.Priority,end=" ")
print("\n")
test = PriorityQueue(10,0)
test.add(20,100)
test.add(30,300)
prints(test)
test.remove()
prints(test)