-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathpriorityqueues.py
216 lines (179 loc) · 5.93 KB
/
priorityqueues.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""
Implement the function RemoveMin for the min priority queue class.
For a minimum priority queue, write the function for removing the minimum element present.
Remove and return the minimum element.
Note : main function is given for your reference which we are using internally to test the code.
"""
Solution :
class PriorityQueueNode:
def __init__(self, value, priority):
self.value = value
self.priority = priority
class PriorityQueue:
def __init__(self):
self.pq = []
def isEmpty(self):
return self.getSize() == 0
def getSize(self):
return len(self.pq)
def getMin(self):
if self.isEmpty():
return None
return self.pq[0].value
def __percolateUp(self):
childIndex = self.getSize() - 1
while childIndex > 0:
parentIndex = (childIndex - 1) // 2
if self.pq[parentIndex].priority > self.pq[childIndex].priority:
self.pq[parentIndex], self.pq[childIndex] = self.pq[childIndex], self.pq[parentIndex]
childIndex = parentIndex
else:
break
def insert(self, ele, priority):
pqNode = PriorityQueueNode(ele, priority)
self.pq.append(pqNode)
self.__percolateUp()
def __percolatedown(self):
parenti = 0
lc = 2 * (parenti) + 1
rc = 2 * (parenti) + 2
while lc < self.getSize():
mi = parenti
if self.pq[mi].priority > self.pq[lc].priority:
mi = lc
if self.getSize() > rc and self.pq[mi].priority > self.pq[rc].priority:
mi = rc
if mi == parenti:
break
self.pq[parenti], self.pq[mi] = self.pq[mi], self.pq[parenti]
parenti = mi
lc = 2 * (parenti) + 1
rc = 2 * (parenti) + 2
def removeMin(self):
if self.isEmpty():
return None
rel = self.pq[0].value
self.pq[0] = self.pq[self.getSize() - 1]
self.pq.pop()
self.__percolatedown()
return rel
myPq = PriorityQueue()
curr_input = [int(ele) for ele in input().split()]
choice = curr_input[0]
i = 1
while choice != -1:
if choice == 1:
element = curr_input[i]
i += 1
myPq.insert(element, element)
elif choice == 2:
print(myPq.getMin())
elif choice == 3:
print(myPq.removeMin())
elif choice == 4:
print(myPq.getSize())
elif choice == 5:
if myPq.isEmpty():
print('true')
else:
print('false')
break
else:
pass
choice = curr_input[i]
i += 1
#Code : Max Priority Queue
"""
Implement the class for Max Priority Queue which includes following functions -
1. getSize -
Return the size of priority queue i.e. number of elements present in the priority queue.
2. isEmpty -
Check if priority queue is empty or not. Return true or false accordingly.
3. insert -
Given an element, insert that element in the priority queue at the correct position.
4. getMax -
Return the maximum element present in the priority queue without deleting.
Return -Infinity if priority queue is empty.
5. removeMax -
Delete and return the maximum element present in the priority queue.
Return -Infinity if priority queue is empty.
Note : main function is given for your reference which we are using internally to test the class.
"""
Solution :
class Node:
def __init__(self, value, priority):
self.value = value
self.priority = priority
class PriorityQueue:
def __init__(self):
self.pq = []
def isEmpty(self):
return self.getSize() == 0
def getSize(self):
return len(self.pq)
def getMax(self):
if self.isEmpty() is True:
return None
return self.pq[0].value
def __upHeapify(self):
child_index = self.getSize() - 1
while child_index > 0:
parent_index = (child_index - 1) // 2
if self.pq[parent_index].priority < self.pq[child_index].priority:
self.pq[parent_index], self.pq[child_index] = self.pq[child_index], self.pq[parent_index]
child_index = parent_index
else:
break
def insert(self, ele, priority):
pqNode = Node(ele, priority)
self.pq.append(pqNode)
self.__upHeapify()
def __downHeapify(self):
parent_index = 0
child_left_index = 2 * parent_index + 1
child_right_index = 2 * parent_index + 2
while child_left_index < self.getSize():
max_index = parent_index
if self.pq[max_index].priority < self.pq[child_left_index].priority:
max_index = child_left_index
if child_right_index < self.getSize() and self.pq[max_index].priority < self.pq[child_right_index].priority:
max_index = child_right_index
if max_index == parent_index:
break
self.pq[parent_index], self.pq[max_index] = self.pq[max_index], self.pq[parent_index]
parent_index = max_index
child_left_index = 2 * parent_index + 1
child_right_index = 2 * parent_index + 2
def removeMax(self):
if self.isEmpty() is True:
return None
value = self.pq[0].value
self.pq[0] = self.pq[self.getSize() - 1]
self.pq.pop()
self.__downHeapify()
return value
myPq = PriorityQueue()
curr_input = [int(ele) for ele in input().split()]
choice = curr_input[0]
i = 1
while choice != -1:
if choice == 1:
element = curr_input[i]
i += 1
myPq.insert(element, element)
elif choice == 2:
print(myPq.getMax())
elif choice == 3:
print(myPq.removeMax())
elif choice == 4:
print(myPq.getSize())
elif choice == 5:
if myPq.isEmpty():
print('true')
else:
print('false')
break
else:
pass
choice = curr_input[i]
i += 1