예제 #1
0
 def TestBuildMaxHeap(self):
     input = [random.randrange(100) for _ in range(100)]
     h = MaxHeap(input)
     sorted_input = sorted(input)
     for i in reversed(range(len(input))):
         self.assertEqual(h.peek_max(), sorted_input[i])
         self.assertEqual(h.extract_max(), sorted_input[i])
예제 #2
0
 def test_heap_invariant(self):
     heap = MaxHeap([2,4,6,8,10,1,3,5,7,9])
     for k in range(len(heap)):
         if 2*k+1 < len(heap):
             self.assertTrue(heap.get(k) >= heap.get(2*k+1))
         if 2*k+2 < len(heap):
             self.assertTrue(heap.get(k) >= heap.get(2*k+2))
예제 #3
0
 def TestMaxHeapWithRandomInput(self):
     input = [random.randrange(100) for _ in range(100)]
     h = MaxHeap()
     for num in input:
         h.insert(num)
     sorted_input = sorted(input)
     for i in reversed(range(len(input))):
         self.assertEqual(h.extract_max(), sorted_input[i])
예제 #4
0
 def test_max_heap_has_max_at_top(self):
     maxHeap = MaxHeap()
     maxHeap.push(2)
     maxHeap.push(10)
     maxHeap.push(20)
     maxHeap.push(1)
     r = maxHeap.peek()
     self.assertEqual(20, maxHeap.peek())
     self.assertEqual(r, 20)
예제 #5
0
def full_max_heap():
    test_heap = MaxHeap()
    test_heap.add(4)
    test_heap.add(6)
    test_heap.add(3)
    test_heap.add(7)
    test_heap.add(5)
    return test_heap
def calculate_medians_with_heaps(numbers):
    """Calculate the sum of all 10,000 medians, modulo 10000"""
    max_heap = MaxHeap()  # For storing the smaller half of numbers
    min_heap = MinHeap()  # For storing the larger half of numbers
    medians = []
    for number in numbers:
        if max_heap.peek_max() is None or max_heap.peek_max() > number:
            max_heap.insert(number)
            if max_heap.size > min_heap.size + 1:
                min_heap.insert(max_heap.extract_max())
        else:
            min_heap.insert(number)
            if min_heap.size > max_heap.size + 1:
                max_heap.insert(min_heap.extract_min())
        if max_heap.size >= min_heap.size:
            medians.append(max_heap.peek_max())
        else:
            medians.append(min_heap.peek_min())
    return medians
예제 #7
0
class BuildingMap:
    def __init__(self):
        self.map = defaultdict(list)
        self.heap = MaxHeap()

    def put(self, height):
        self.heap.push(height)
        self.map[height].append(height)

    def remove(self, height):
        if height not in self.map:
            print(f'height {height} not in map')
            return

        n = self.map[height].pop()

        self.heap.remove(n)

        if len(self.map[height]) == 0:
            self.map.pop(height)

    def getMax(self):
        if self.heap.empty():
            return 0
        return self.heap.peek()
예제 #8
0
 def TestSizeReducedAfterExtractMax(self):
     input = [6, 1, 4, 5, 3, 2]
     h = MaxHeap()
     for num in input:
         h.insert(num)
     initial_size = h.size
     self.assertEqual(initial_size, len(h.a))
     h.extract_max()
     self.assertEqual(initial_size - 1, h.size)
예제 #9
0
def find_k_smallest(a, k):
    h = MaxHeap()
    for i in range(len(a)):
        if i < k:
            h.push(a[i])
        elif a[i] < h.peek():
            popped = h.pop()
            h.push(a[i])

    return h.getItems()
    h = []
    heapq.heapify(h)

    for i in range(len(a)):
        if i < k:
            heapq.heappush(h, -a[i])
        elif a[i] < -h[0]:
            heapq.heapreplace(h, -a[i])

    return [-i for i in h]
예제 #10
0
 def test_it_can_make_heaps(self):
     minHeap = MinHeap()
     self.assertIsInstance(minHeap, MinHeap)
     maxHeap = MaxHeap()
     self.assertIsInstance(maxHeap, MaxHeap)
예제 #11
0
    def test_max_heap_max_is_retained(self):
        maxHeap = MaxHeap()
        maxHeap.push(7)
        maxHeap.push(4)
        maxHeap.push(10)
        maxHeap.push(6)
        maxHeap.push(5)

        self.assertEqual(10, maxHeap.peek())
        maxHeap.remove(10)
        self.assertEqual(7, maxHeap.peek())
        maxHeap.remove(6)
        self.assertEqual(7, maxHeap.peek())
예제 #12
0
 def test_heaps_empty(self):
     maxHeap = MaxHeap()
     self.assertEqual(True, maxHeap.empty())
예제 #13
0
 def __init__(self):
     self.map = defaultdict(list)
     self.heap = MaxHeap()
예제 #14
0
 def TestMaxHeapProperty(self):
     input = [6, 1, 4, 5, 3, 2]
     h = MaxHeap()
     for num in input:
         h.insert(num)
     self.assertEqual(h.extract_max(), 6)
예제 #15
0
 def test_initialize(self):
     heap = MaxHeap([2,4,6,8,10,1,3,5,7,9])
     self.assertEquals(heap.get(0), 10)
예제 #16
0
def init_max_heap():
    test_heap = MaxHeap()
    return test_heap