コード例 #1
0
class PriorityQueue(object):
    def __init__(self):
        self._heap = BinaryHeap()

    def peek(self):
        return self._heap.peek_min()

    def is_empty(self):
        return self._heap.is_empty()

    def enqueue(self, value):
        self._heap.insert(value)

    def dequeue(self):
        self._heap.extract_min()

    def __iter__(self):
        yield from iter(self._heap)

    def __len__(self):
        return len(self._heap)
コード例 #2
0
ファイル: sort.py プロジェクト: zlou/coding-exercises
def heap_sort(arr):
    '''
    This consists of 2 steps:
    1. build a min heap, which is O(nlogn)
    2. extract all n elements of the heap, which is O(nlogn)
    Overall, this takes O(nlogn)
    '''
    heap = BinaryHeap(arr)
    result = []
    while not heap.is_empty():
        result.append(heap.extract_min())
    return result
コード例 #3
0
class PriorityQueue(object):
	def __init__(self):
		self._heap = BinaryHeap()

	def peek(self):
		return self._heap.peek_min()

	def is_empty(self):
		return self._heap.is_empty()

	def enqueue(self, value):
		self._heap.insert(value)

	def dequeue(self):
		self._heap.extract_min()

	def __iter__(self):
		yield from iter(self._heap)

	def __len__(self):
		return len(self._heap)
コード例 #4
0
def heap_sort(arr):
    '''
    This consists of 2 steps:
    1. build a min heap, which is O(nlogn)
    2. extract all n elements of the heap, which is O(nlogn)
    Overall, this takes O(nlogn)
    '''
    heap = BinaryHeap(arr)
    result = []
    while not heap.is_empty():
        result.append(heap.extract_min())
    return result
コード例 #5
0
class PriorityQueue:
    def __init__(self):
        self.queue = BinaryHeap()

    # log(n) worst case (both time and space)
    def enqueue(self, elem):
        self.queue.insert_element(elem)

    # log(n) worst case (both time and space)
    def dequeue(self):
        return self.queue.extract_min()

    def peek_min(self):
        return self.queue[0]

    def isEmpty(self):
        return self.queue.isEmpty()

    def __str__(self):
        return self.queue.__str__()