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])
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))
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])
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)
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
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()
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)
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]
def test_it_can_make_heaps(self): minHeap = MinHeap() self.assertIsInstance(minHeap, MinHeap) maxHeap = MaxHeap() self.assertIsInstance(maxHeap, MaxHeap)
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 test_heaps_empty(self): maxHeap = MaxHeap() self.assertEqual(True, maxHeap.empty())
def __init__(self): self.map = defaultdict(list) self.heap = MaxHeap()
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)
def test_initialize(self): heap = MaxHeap([2,4,6,8,10,1,3,5,7,9]) self.assertEquals(heap.get(0), 10)
def init_max_heap(): test_heap = MaxHeap() return test_heap