Created
January 12, 2012 12:47
-
-
Save marcinwyszynski/1600309 to your computer and use it in GitHub Desktop.
A simple PriorityQueue in Python
This file contains 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
class PriorityQueue(object): | |
def __init__(self): | |
self.store = {} | |
def enq(self, element, weight): | |
if weight in self.store: | |
self.store[weight].append(element) | |
else: | |
self.store[weight] = [element] | |
def deq(self): | |
if len(self.store.keys()) == 0: | |
return None | |
max_weight = sorted(self.store.keys())[-1] | |
element = self.store[max_weight].pop(0) | |
if len(self.store[max_weight]) == 0: | |
del self.store[max_weight] | |
return element |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment