Created
March 13, 2019 04:44
-
-
Save jdunck/5212e962da87699d513fe2d6c8a30cd2 to your computer and use it in GitHub Desktop.
Min/Max heap for python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import heapq | |
class Heap(object): | |
def __init__(self, max=False): | |
self.max = max | |
self.data = [] | |
@property | |
def delta(self): | |
return -1 if self.max else 1 | |
def push(self, item): | |
heapq.heappush(self.data, self.delta * item) | |
def pop(self): | |
return heapq.heappop(self.data) * self.delta | |
def top(self): | |
return self.delta * self.data[0] | |
def __len__(self): | |
return len(self.data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment