MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / extract_max

Method extract_max

data_structures/heap/heap.py:165–194  ·  view source on GitHub ↗

get and remove max from heap >>> h = Heap() >>> h.build_max_heap([20,40,50,20,10]) >>> h.extract_max() 50 >>> h = Heap() >>> h.build_max_heap([514,5,61,57,8,99,105]) >>> h.extract_max() 514 >>> h = Heap() >>>

(self)

Source from the content-addressed store, hash-verified

163 self.max_heapify(i)
164
165 def extract_max(self) -> T:
166 """
167 get and remove max from heap
168
169 >>> h = Heap()
170 >>> h.build_max_heap([20,40,50,20,10])
171 >>> h.extract_max()
172 50
173
174 >>> h = Heap()
175 >>> h.build_max_heap([514,5,61,57,8,99,105])
176 >>> h.extract_max()
177 514
178
179 >>> h = Heap()
180 >>> h.build_max_heap([1,2,3,4,5,6,7,8,9,0])
181 >>> h.extract_max()
182 9
183 """
184 if self.heap_size >= 2:
185 me = self.h[0]
186 self.h[0] = self.h.pop(-1)
187 self.heap_size -= 1
188 self.max_heapify(0)
189 return me
190 elif self.heap_size == 1:
191 self.heap_size -= 1
192 return self.h.pop(-1)
193 else:
194 raise Exception("Empty heap")
195
196 def insert(self, value: T) -> None:
197 """

Callers 2

top_k_frequent_wordsFunction · 0.95
heap.pyFile · 0.80

Calls 2

max_heapifyMethod · 0.95
popMethod · 0.45

Tested by

no test coverage detected