Пример #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 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
Пример #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 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)
Пример #6
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())
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
Пример #8
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]
Пример #9
0
 def test_it_can_make_heaps(self):
     minHeap = MinHeap()
     self.assertIsInstance(minHeap, MinHeap)
     maxHeap = MaxHeap()
     self.assertIsInstance(maxHeap, MaxHeap)
Пример #10
0
 def test_heaps_empty(self):
     maxHeap = MaxHeap()
     self.assertEqual(True, maxHeap.empty())
Пример #11
0
 def __init__(self):
     self.map = defaultdict(list)
     self.heap = MaxHeap()
Пример #12
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)
Пример #13
0
def init_max_heap():
    test_heap = MaxHeap()
    return test_heap