def _balance(self): while len(self.heap_high) - len(self.heap_low) > 1: self.heap_low.append(heapq.heappop(self.heap_high)) heapq._siftdown_max(self.heap_low, 0, len(self.heap_low) - 1) while len(self.heap_low) - len(self.heap_high) > 1: heapq.heappush(self.heap_high, heapq.heappop(self.heap_low)) heapq._siftup_max(self.heap_low, 0)
def _kth_smallest(nums, k): heap = [] for num in itertools.islice(nums, 0, k): heap.append(num) heapq._siftdown_max(heap, 0, len(heap) - 1) for num in itertools.islice(nums, k, len(nums)): if num <= heap[0]: heapq._heappushpop_max(heap, num) return heap[0]
def HeapDelete(A, i): if (len(A) <= i): raise Exception("Heap does not have ith element.") # Exchange i with last element and trim the list last = A[len(A) - 1] A[i] = A[last] A = A[:(len(A) - 1)] if A[i] < last: # Bubble up the ith element heapq._siftdown_max(A, 0, i) else: # Bubble down the ith element heapq._siftup_max_(A, i)
def rebalance_heaps(self): ''' @pre: two heaps that differ by more than 1 in size @post: two heaps that differ by only one in size, obtained by shuffling elements ''' if self.minL - self.maxL > 1: self.maxH.append(heapq.heappop(self.minH)) self.maxL += 1 self.minL += -1 heapq._siftdown_max(self.maxH, 0, self.maxL - 1) elif self.maxL - self.minL > 1: heapq.heappush(self.minH, heapq._heappop_max(self.maxH)) self.maxL += -1 self.minL += 1 assert abs(self.maxL - self.minL) <= 1, "Not balanced!"
def insert(h_low, h_high, m): """Insert the item m to the heap. """ # Push the m in the heap if m < h_low[0]: h_low.append(m) heapq._siftdown_max(h_low, 0, len(h_low) - 1) else: heapq.heappush(h_high, m) # Balance the two heaps size_diff = len(h_low) - len(h_high) if size_diff > 1: item = heapq._heappop_max(h_low) heapq.heappush(h_high, item) elif size_diff < -1: item = heapq.heappop(h_high) h_low.append(item) heapq._siftdown_max(h_low, 0, len(h_low) - 1)
def heap_push(self, val): ''' @pre: two balanced heaps @post: two heaps with val added in the right spot, balanced :param val: the value to be added to the median heap adds the value to the proper heap ''' # If we're adding to the max heap if self.maxL == 0 or val < self.maxH[0]: self.maxH.append(val) self.maxL += 1 heapq._siftdown_max(self.maxH, 0, self.maxL - 1) # Min heap is simpler else: heapq.heappush(self.minH, val) self.minL += 1 self.rebalance_heaps() print(self.get_median())
def median(arr): max_heap = [] min_heap = [] results = [] heapq.heappush(min_heap, arr[0]) results.append(arr[0]) for i in arr[1:]: max_l = len(max_heap) min_l = len(min_heap) if max_l > min_l: if i < max_heap[0]: temp = max_heap[0] heapq._heapreplace_max(max_heap, i) min_heap.heappush(temp) else: heapq.heappush(min_heap, i) results.append((max_heap[0] + min_heap[0]) / 2) continue elif max_l < min_l: if i > min_heap[0]: temp = min_heap[0] heapq.heapreplace(min_heap, i) max_heap.append(temp) heapq._siftdown_max(max_heap, 0, max_l) else: max_heap.append(i) heapq._siftdown_max(max_heap, 0, max_l) results.append((max_heap[0] + min_heap[0]) / 2) continue else: if i > min_heap[0]: heapq.heappush(min_heap, i) results.append(min_heap[0]) else: max_heap.append(i) heapq._siftdown_max(max_heap, 0, max_l) results.append(max_heap[0]) return results
def heappush_max(heap, item): heap.append(item) heapq._siftdown_max( heap, 0, len(heap) - 1) #use the heapq sifting function to push the item to the heap
def maxheappush(heap, item): """Push item onto heap, maintaining the heap invariant.""" heap.append(item) heapq._siftdown_max(heap, 0, len(heap) - 1)
def heappush(heap, item): heap.append(item) minq._siftdown_max(heap, 0, len(heap) - 1)
def update_event(self, inp=-1): self.set_output_val(0, heapq._siftdown_max(self.input(0), self.input(1), self.input(2)))
def _push_to_heap_low(self, num): self.heap_low.append(num) heapq._siftdown_max(self.heap_low, 0, len(self.heap_low) - 1)
def maxheappush(heap, item): """Push item onto heap, maintaining the heap invariant.""" heap.append(item) heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappush_max(self, heap, item): """Maxheap version of a heappush.""" heap.append(item) heapq._siftdown_max(heap, 0, len(heap) - 1)
import heapq lst = [3, 8, 5, 4, 6, 9, 7] heapq._heapify_max(lst) # heapify for max heap print(*lst) heapq._heappop_max(lst) # max element popped and lst is re-heapified print(*lst) heapq._heapreplace_max(lst, 0) # replace the max element of the heap print(*lst) # to push into a max heap- is different lst.append(1) heapq._siftdown_max( lst, 0, len(lst) - 1) # heapq._siftdown_max(heap,startpos of heap,pos of inserted element) print(*lst) # also we can use heapq._siftup_max(heap,pos)
def median(_add=None, _remove=None): result = None max_l = len(max_heap) min_l = len(min_heap) if _add: if not min_l: heapq.heappush(min_heap, _add) elif max_l > min_l: if _add < max_heap[0]: temp = max_heap[0] heapq._heapreplace_max(max_heap, _add) heapq.heappush(min_heap, temp) else: heapq.heappush(min_heap, _add) elif max_l < min_l: if _add > min_heap[0]: temp = min_heap[0] heapq.heapreplace(min_heap, _add) max_heap.append(temp) heapq._siftdown_max(max_heap, 0, max_l) else: max_heap.append(_add) heapq._siftdown_max(max_heap, 0, max_l) else: if _add > min_heap[0]: heapq.heappush(min_heap, _add) else: max_heap.append(_add) heapq._siftdown_max(max_heap, 0, max_l) max_l = len(max_heap) min_l = len(min_heap) if _remove: if min_heap and _remove >= min_heap[0]: indx = min_heap.index(_remove) min_heap[indx] = min_heap[-1] min_heap.pop() if indx < len(min_heap): heapq._siftup(min_heap, indx) heapq._siftdown(min_heap, 0, indx) elif max_heap and _remove <= max_heap[0]: indx = max_heap.index(_remove) max_heap[indx] = max_heap[-1] max_heap.pop() if indx < len(max_heap): heapq._siftup_max(max_heap, indx) heapq._siftdown_max(max_heap, 0, indx) max_l = len(max_heap) min_l = len(min_heap) if max_l - min_l >= 2: temp = max_heap[0] heapq._heappop_max(max_heap) heapq.heappush(min_heap, temp) elif min_l - max_l >= 2: temp = min_heap[0] heapq.heappop(min_heap) max_heap.append(temp) heapq._siftdown_max(max_heap, 0, max_l) min_l = len(min_heap) max_l = len(max_heap) if min_l == max_l: result = (max_heap[0] + min_heap[0]) / 2 # print(f'debug: max_heap[0]: {max_heap[0]} min_heap[0]:{min_heap[0]}') elif min_l > max_l: result = min_heap[0] else: result = max_heap[0] return result
def add(self, element): self.heap.append(element) heapq._siftdown_max(self.heap, 0, len(self.heap) - 1)
# TLE from heapq import heappush, heappop, _heappop_max, _siftdown_max s = 0 l = [] r = [] n, k = map(int, input().split()) k = 100 - k for _ in range(n): x = int(input()) if x == 0: if len(l) == 0: print(r[0]) else: print(l[0]) else: s += 1 if len(l) == 0 or x <= l[0]: l.append(x) _siftdown_max(l, 0, len(l) - 1) if len(r) < s * k // 100: heappush(r, _heappop_max(l)) else: heappush(r, x) if len(r) > s * k // 100: l.append(heappop(r)) _siftdown_max(l, 0, len(l) - 1)
def maxpush(self, item): self._data.append(item) heapq._siftdown_max(self._data, 0, len(self._data) - 1)
import heapq # Define minHeap = [9, 3, 5, 1, 2] heapq.heapify(minHeap) maxHeap = [9, 3, 5, 1, 2] heapq._heapify_max(maxHeap) # Insertion heapq.heappush(minHeap, 10) # heapq doesn't have built in maxHeap .insert maxHeap.append(10) heapq._siftdown_max(maxHeap, 0, len(maxHeap) - 1) # Access smallest = minHeap[0] largest = maxHeap[0] # Deletion print(heapq.heappop(minHeap)) print(heapq._heappop_max(maxHeap))
def _heappush_max(h, item): h.append(item) _siftdown_max(h, 0, len(h) - 1)
def push(self, item): self._heap.append(item) _siftdown_max(self._heap, 0, len(self._heap) - 1)
def max_heappush(heap, item): heap.append(item) hq._siftdown_max(heap, 0, len(heap)-1)
# TLE from heapq import _heappop_max, _siftdown_max, _heapify_max n = int(input()) h = list(map(int, input().split())) h.append(0) _heapify_max(h) c = 0 while len(h) > 1: a = h[0] _heappop_max(h) a -= h[0] _heappop_max(h) if a != 0: c += a h.append(a) _siftdown_max(h, 0, len(h) - 1) print(c)
import heapq li = [1, 5, 4, 7, 8, 9, 2, 3] heapq._heapify_max(li) print(li) print(heapq._heappop_max(li)) print(li) heapq._heapreplace_max(li, 0) print(li) #No Inbuilt Push Function. Implement via own as shown below li.append(6) heapq._siftdown_max(li, 0, len(li) - 1) print(li)
# -*- coding: utf-8 -*- """ Created on Sat Jun 6 19:28:44 2020 @author: cheerag.verma """ #max heap inbuilts function import heapq li = [10,5,4,3,22,9,31] heapq._heapify_max(li) # builds the max heap heapq._heappop_max(li) # pops the max element from the list heapq._heapreplace_max(li,0) # pops out the top element and replace it with 0 (element) and perform downheapify # insert element in the max heap li.append(20) heapq._siftdown_max(li,0,len(li)-1) #li,startposition,endposition heapq._heapreplace_max(li)
def push(self, item): self.heap.append((self.key(item), item)) heapq._siftdown_max(self.heap, 0, len(self.heap) - 1)
def mheappush(heap, item): heap.append(item) _siftdown_max(heap, 0, len(heap) - 1)
def _heappush_max(heap, item): heap.append(item) heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappush_max(heap, item): """ why is this not in heapq """ heap.append(item) heapq._siftdown_max(heap, 0, len(heap) - 1)
def _heappush_max(heap, item): heap.append(item) heapq._siftdown_max(heap, 0, len(heap) - 1)
def heappush_max(self,hql,item): hql.append(item) heapq._siftdown_max(hql,0,len(hql)-1) return