def test_getitem(self): l = range(11) h = MaxHeap() caught_exception = False try: print h[1] except HeapEmptyError: caught_exception = True assert caught_exception == True for i in l: h.add(i) caught_exception = False try: print h[-2] except IndexError: caught_exception = True assert caught_exception == True assert len(h) == 11 caught_exception = False try: print h[11] except IndexError: caught_exception = True assert caught_exception == True x = [10, 9, 5, 6, 8, 1, 4, 0, 3, 2, 7] for i in xrange(11): assert h[i] == x[i]
def test_build_heap(self): l = range(10, -1, -1) MaxHeap.build_heap(l) assert l == range(10, -1, -1) l = range(11) MaxHeap.build_heap(l) assert l == [10, 9, 6, 8, 4, 5, 2, 7, 3, 1, 0]
def test_after_single_push_delete_max_removes_the_value(self): heap = MaxHeap() heap.insert(0) assert heap.size() == 1 assert not heap.is_empty() heap.delete_max() assert heap.size() == 0 assert heap.is_empty()
def test_custom_comparator(self): # Max heap using custom comparator making it a minheap h = MaxHeap(lambda a, b: cmp(b, a)) for i in range(10): h.add(i) assert h.items == range(10) for i in range(10): assert h.remove() == i
def test_after_single_push_extract_max_removes_and_returns_value(self): heap = MaxHeap() heap.insert(0) assert heap.size() == 1 assert not heap.is_empty() assert heap.extract_max() == 0 assert heap.size() == 0 assert heap.is_empty()
def test_add(self): l = [5, 4, 3, 2, 1, 6, 7, 8, 9, 0] intermediate_heaps = [[5], [5, 4], [5, 4, 3], [5, 4, 3, 2], [5, 4, 3, 2, 1], [6, 4, 5, 2, 1, 3], [7, 4, 6, 2, 1, 3, 5], [8, 7, 6, 4, 1, 3, 5, 2], [9, 8, 6, 7, 1, 3, 5, 2, 4], [9, 8, 6, 7, 1, 3, 5, 2, 4, 0]] h = MaxHeap() for i in xrange(len(l)): h.add(l[i]) assert h.items == intermediate_heaps[i]
def test_remove(self): l = [5, 4, 3, 2, 1, 6, 7, 8, 9, 0] h = MaxHeap() caught_exception = False try: assert h.peek() == None except HeapEmptyError as e: assert str(e) == "HeapEmptyError: 'peek(): Heap is empty'" caught_exception = True assert caught_exception == True caught_exception = False try: assert h.remove() == None except HeapEmptyError as e: assert str(e) == "HeapEmptyError: 'remove(): Heap is empty'" caught_exception = True assert caught_exception == True for i in xrange(len(l)): h.add(l[i]) assert len(h) == 10 for i in xrange(9, -1, -1): item = h.remove() assert (len(h) == i) assert item == i
def test_setitem(self): l = [0, 1, 4, 2, 6, 5, 8, 3, 7, 9, 10] h = MaxHeap() for x in l: h.add(x) ''' 10 / \ 9 6 / \ / \ 4 8 1 5 / \ / \ 0 3 2 7 ''' assert h.items == [10, 9, 6, 4, 8, 1, 5, 0, 3, 2, 7] assert h[4] == 8 h[4] = 11 # calls MaxHeap's decreaseKey ''' 11 / \ 10 6 / \ / \ 4 9 1 5 / \ / \ 0 3 2 7 ''' assert h.items == [11, 10, 6, 4, 9, 1, 5, 0, 3, 2, 7] assert h[0] == 11 h[0] = -1 # calls decreaseKey ''' 10 / \ 9 6 / \ / \ 4 7 1 5 / \ / \ 0 3 2 -1 ''' assert h.items == [10, 9, 6, 4, 7, 1, 5, 0, 3, 2, -1]
def test_sorted(self): l = range(10, -1, -1) MaxHeap.build_heap(l) h = MaxHeap() h.items = l new_l = [] h.sorted(lambda i, l: l.append(i), new_l) assert new_l == range(10, -1, -1)
def test_increaseKey(self): l = [0, 1, 4, 2, 6, 5, 8, 3, 7, 9, 10] h = MaxHeap() for x in l: h.add(x) ''' 10 / \ 9 6 / \ / \ 4 8 1 5 / \ / \ 0 3 2 7 ''' assert h.items == [10, 9, 6, 4, 8, 1, 5, 0, 3, 2, 7] caught_exception = False try: h.increaseKey(4, 5) except ValueError as v: assert v.message == "ValueError: increaseKey() - New key should be greater than current value" caught_exception = True assert caught_exception == True h.increaseKey(4, 11) ''' 11 / \ 10 6 / \ / \ 4 9 1 5 / \ / \ 0 3 2 7 ''' assert h.items == [11, 10, 6, 4, 9, 1, 5, 0, 3, 2, 7]
def test_decreaseKey(self): l = [0, 1, 4, 2, 6, 5, 8, 3, 7, 9, 10] h = MaxHeap() for x in l: h.add(x) ''' 10 / \ 9 6 / \ / \ 4 8 1 5 / \ / \ 0 3 2 7 ''' assert h.items == [10, 9, 6, 4, 8, 1, 5, 0, 3, 2, 7] caught_exception = False try: h.decreaseKey(4, 10) except ValueError as v: assert v.message == "ValueError: decreaseKey() - New key should be lesser than current value" caught_exception = True assert caught_exception == True h.decreaseKey(0, -1) ''' 9 / \ 8 6 / \ / \ 4 7 1 5 / \ / \ 0 3 2 -1 ''' assert h.items == [9, 8, 6, 4, 7, 1, 5, 0, 3, 2, -1]
def test_extract_max_of_empty_heap_raises_exception(self): heap = MaxHeap() with raises(MaxHeap.EmptyHeapException): heap.extract_max()
def test_initialization(self): heap = MaxHeap() assert isinstance(heap, MaxHeap)
def test_isMaxHeap_i(self): h = MaxHeap() isHeap = h.isHeap_i h.items = [] assert isHeap() == True h.items = [1, 1] assert isHeap() == True h.items = [1, 1, 1] assert isHeap() == True ''' 5 / \ 4 3 / \ 2 1 ''' h.items = [5, 4, 3, 2, 1] assert isHeap() == True ''' 2 / 1 ''' h.items = [2, 1] assert isHeap() == True h.items = [1, 2] assert isHeap() == False ''' 3 / \ 1 2 ''' h.items = [3, 1, 2] assert isHeap() == True ''' 1 / \ 2 3 / \ 4 5 ''' h.items = [1, 2, 3, 4, 5] assert isHeap() == False h.items = range(10, -1, -1) assert isHeap() == True h.items = range(10) assert isHeap() == False
def test_after_single_push_find_max_returns_value(self): heap = MaxHeap() heap.insert(0) assert heap.find_max() == 0
def test_heap_size_is_3_after_three_pushes(self): heap = MaxHeap() heap.insert(0) heap.insert(1) heap.insert(2) assert heap.size() == 3
def test_heap_size_is_1_after_single_push(self): heap = MaxHeap() heap.insert(0) assert heap.size() == 1
def test_heap_is_not_empty_after_insert(self): heap = MaxHeap() heap.insert(0) assert not heap.is_empty()
def test_empty_heap_is_empty(self): heap = MaxHeap() assert heap.is_empty()
def test_size_of_empty_heap_is_0(self): heap = MaxHeap() assert heap.size() == 0
def test_delete_max_of_empty_heap_raises_exception(self): heap = MaxHeap() with raises(MaxHeap.EmptyHeapException): heap.delete_max()
def test_after_two_pushes_find_max_finds_larger_value(self): heap = MaxHeap() heap.insert(0) heap.insert(1) assert heap.find_max() == 1
def test_custom_items(self): class Record: def __init__(self, a, b, c): self.a = a self.b = b self.c = c # by default, using 'b' as key to compare def __cmp__(self, other): return cmp(self.b, other.b) def __str__(self): return "(%s, %s, %s)" % (self.a, self.b, self.c) h = MaxHeap() h.add(Record("record1", 1, 100)) h.add(Record("record4", 5, 125)) h.add(Record("record3", 2, 50)) h.add(Record("record2", 3, 25)) h.add(Record("record5", 4, 5)) sorted_records_bs = [] h.sorted(lambda x, l: l.append(x.b), sorted_records_bs) assert sorted_records_bs == range(5, 0, -1) h2 = MaxHeap(lambda r1, r2: cmp(r1.a, r2.a)) h2.add(Record("record1", 1, 100)) h2.add(Record("record4", 5, 125)) h2.add(Record("record3", 2, 50)) h2.add(Record("record2", 3, 25)) h2.add(Record("record5", 4, 5)) sorted_records_as = [] h2.sorted(lambda x, l: l.append(x.a), sorted_records_as) assert sorted_records_as == [ "record" + str(i) for i in xrange(5, 0, -1) ] h3 = MaxHeap(lambda r1, r2: cmp(r1.c, r2.c)) h3.add(Record("record1", 1, 100)) h3.add(Record("record4", 5, 125)) h3.add(Record("record3", 2, 50)) h3.add(Record("record2", 3, 25)) h3.add(Record("record5", 4, 5)) sorted_records_cs = [] h3.sorted(lambda x, l: l.append(x.c), sorted_records_cs) assert sorted_records_cs == sorted([100, 125, 50, 25, 5])[::-1]
def test_sort(self): assert MaxHeap.sort([1, 3, 5, 6, 4, 2]) == range(1, 7)
def test_two_pushes_and_two_extract_max_correct_values_empty_at_end(self): heap = MaxHeap() heap.insert(0) heap.insert(1) assert heap.size() == 2 assert not heap.is_empty() assert heap.extract_max() == 1 assert heap.size() == 1 assert not heap.is_empty() assert heap.extract_max() == 0 assert heap.size() == 0 assert heap.is_empty()
def maxSlidingWindow(nums, k): if not nums: return [] h = MaxHeap() for i in xrange(k): h.add((nums[i], i)) max_list = [h.peek()[0]] for i in xrange(1, len(nums) - k + 1): # max is to the left of window # remove all previous max items that are no longer in the window, # from the heap while h and h.peek()[1] < i: h.remove() # add item at end of current window, i+k-1, to the heap h.add((nums[i + k - 1], i + k - 1)) # add current window's max from the top of the heap max_list.append(h.peek()[0]) return max_list