def TestBuildMinHeap(self): input = [random.randrange(100) for _ in range(100)] h = Heap(input) sorted_input = sorted(input) for i in range(len(input)): self.assertEqual(h.peek_min(), sorted_input[i]) self.assertEqual(h.extract_min(), sorted_input[i])
def TestMinHeapWithTuples(self): input = [(random.randrange(100), "Hullo!") for _ in range(100)] h = Heap(key=lambda x: x[0]) for num in input: h.insert(num) sorted_input = sorted(input) for i in range(len(input)): self.assertEqual(h.extract_min(), sorted_input[i])
def TestMinHeapWithRandomInput(self): input = [random.randrange(100) for _ in range(100)] h = Heap() for num in input: h.insert(num) sorted_input = sorted(input) for i in range(len(input)): self.assertEqual(h.extract_min(), sorted_input[i])
def test_extract_root(self): # Arrange h = Heap() h.insert(6) h.insert(5) h.insert(3) h.insert(4) h.insert(2) h.insert(1) # Act root = h.extract_root() # Assert self.assertEqual(1, root) self.assertEqual(2, h._heap[0])
def TestSizeReducedAfterExtractMin(self): input = [6, 1, 4, 5, 3, 2] h = Heap() for num in input: h.insert(num) initial_size = h.size self.assertEqual(initial_size, len(h.a)) h.extract_min() self.assertEqual(initial_size - 1, h.size)
def test_insert_minimum_at_the_root(self): # Arrange h = Heap() # Act h.insert(6) h.insert(5) h.insert(3) h.insert(4) h.insert(2) h.insert(1) # Assert self.assertEqual(1, h._heap[0])
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
def construct_tree(freqs): # constructs the tree given the frequencies of each character nodes = [HuffmanNode(letter, freqs[letter]) for letter in freqs] # nodes contains the HuffmanNodes trees = [HuffmanTree(node) for node in nodes] h = Heap() for tree in trees: h.insert(tree) # the heap contains all the trees while len(h.elems) > 2: # including 0 t1 = h.minimum() # extract the two least frequent letters h.delete() t2 = h.minimum() h.delete() newTree = combine(t2, t1) # create a new tree combining the two nodes h.insert(newTree) # insert the new tree back into the heap # at this point, the heap contains only one element return h.minimum()
def TestMinHeapProperty(self): input = [6, 1, 4, 5, 3, 2] h = Heap() for num in input: h.insert(num) self.assertEqual(h.extract_min(), 1)
def _factory(l): _h = Heap() for item in l: _h.insert(item) return _h
"""Test driver to confirm build_heap method runs in O(n) time""" import random import time from heaps import Heap inputs = [] for x in range(1, 10): inputs.append([random.randrange(10000) for _ in range(x * 100000)]) for i, input in enumerate(inputs): start = time.time() Heap(input) ellapsed = time.time() - start print("Took {} seconds to build a heap from {}00,000 " "elements".format(ellapsed, i + 1))