def __init__(self, vals=None, priorities=None): if priorities is not None and vals is not None: if len(priorities) != len(vals): raise ValueError("Must be as many priorities as values.") elements = [ PriorityQueueElement(p, v) for v, p in zip(vals, priorities) ] else: elements = None self._h = MaxHeap(elements=elements)
def test_max_heap_property(self): max_heap = MaxHeap(self.array) length = max_heap.length() elements = max_heap.elements() for i in range(0, length): current_value = elements[i] left_index = (2 * i) + 1 right_index = (2 * i) + 2 if left_index <= length - 1: self.assertGreaterEqual(current_value, elements[left_index]) if right_index <= length - 1: self.assertGreaterEqual(current_value, elements[right_index])
def test_add_element_to_maxheap(self): max_heap = MaxHeap(self.array) max_heap.add_element(13) self.assertEqual(6, max_heap.length()) self.assertEqual([13, 8, 11, 4, 3, 6], max_heap.elements()) self.assertEqual(13, max_heap.get_root_value()) self.assertEqual([13, 8, 11, 4, 3, 6], max_heap.elements())
class PriorityQueue(object): def __init__(self, maxsize): self.maxsize = maxsize self._maxheap = MaxHeap(maxsize) def push(self, priority, value): entry = (priority, value) self._maxheap.add(entry) def pop(self, with_priority=False): entry = self._maxheap.extract() if with_priority: return entry else: return entry[1] def is_empty(self): return len(self._maxheap) == 0
def test_pop(self): h = MaxHeap(elements=to_add, do_copy=True) self.assertEqual(h.peek(), max(to_add)) self.assertEqual(len(h), len(to_add)) for i, v in enumerate(reversed(sorted(to_add))): print(i, v) self.assertEqual(h.pop(), v) self.assertEqual(len(h), len(to_add) - i - 1) self.assertEqual(len(h), 0) self.assertIsNone(h.pop()) for i in range(len(to_add_2)): h.insert(to_add_2[i]) self.assertEqual(h.peek(), max(to_add_2[:i + 1])) self.assertEqual(len(h), i + 1)
class PriorityQueueHeap: "Implementation of a priority queue as a heap" # Reuse of elements used in tree implementation is inefficient # because we don't need a linked list. Oh well. def __init__(self, vals=None, priorities=None): if priorities is not None and vals is not None: if len(priorities) != len(vals): raise ValueError("Must be as many priorities as values.") elements = [ PriorityQueueElement(p, v) for v, p in zip(vals, priorities) ] else: elements = None self._h = MaxHeap(elements=elements) def insert(self, value, priority): element = PriorityQueueElement(priority, value) self._h.insert(element) def pop_max(self): element = self._h.pop() if element is None: return element return element.pop() def peek(self): element = self._h.peek() if element is None: return element return element.peek() def is_empty(self): return len(self) < 1 def shrink_to_size(self): "Shrinks heap capacity to current size" self._h.shrink_to_size() def __len__(self): return len(self._h)
def test_max_heap_count_and_elements(self): max_heap = MaxHeap(self.array) self.assertEqual(5, max_heap.length()) self.assertEqual([11, 8, 6, 4, 3], max_heap.elements())
def test_multiple_operations_maxheap(self): max_heap = MaxHeap(self.array) max_heap.add_element(5) self.assertEqual([11, 8, 6, 4, 3, 5], max_heap.elements()) self.assertEqual(11, max_heap.extract_root()) self.assertEqual([8, 5, 6, 4, 3], max_heap.elements()) max_heap.add_element([11, 13, 5, 6]) self.assertEqual([13, 6, 11, 5, 3, 6, 8, 4, 5], max_heap.elements()) self.assertEqual(13, max_heap.extract_root()) self.assertEqual(11, max_heap.extract_root()) self.assertEqual([8, 6, 6, 5, 3, 4, 5], max_heap.elements()) self.assertEqual([3, 4, 5, 5, 6, 6, 8], sorted(max_heap.elements()))
def test_extract_root_value_maxheap(self): max_heap = MaxHeap(self.array) self.assertEqual(11, max_heap.extract_root()) self.assertEqual([8, 4, 6, 3], max_heap.elements())
def test_search_value_maxheap(self): max_heap = MaxHeap(self.array) index = max_heap.search_value(8) self.assertEqual(index, 1) index = max_heap.search_value(44) self.assertEqual(index, -1)
def test_construct(self): h = MaxHeap(elements=to_add, do_copy=True) self.assertEqual(h.peek(), max(to_add))
def test_add_array_of_elements_to_maxheap(self): max_heap = MaxHeap(self.array) max_heap.add_element([1, 14, 7, 5]) self.assertEqual(9, max_heap.length()) self.assertEqual([14, 8, 11, 7, 3, 1, 6, 4, 5], max_heap.elements()) self.assertEqual(14, max_heap.get_root_value())
def __init__(self, maxsize): self.maxsize = maxsize self._maxheap = MaxHeap(maxsize)
def test_insert(self): h = MaxHeap() for i in range(len(to_add)): h.insert(to_add[i]) self.assertEqual(h.peek(), max(to_add[:i + 1])) self.assertEqual(len(h), i + 1)
def test_sort(self): s1 = MaxHeap.heapsort(copy(to_add)) s2 = list(sorted(to_add)) self.assertEqual(s1, s2)
def test_delete_element_at_index_maxheap(self): max_heap = MaxHeap(self.array) max_heap.delete_element_at_index(2) self.assertEqual(4, max_heap.length()) self.assertEqual([11, 8, 3, 4], max_heap.elements())
def test_empty(self): h = MaxHeap() self.assertEqual(len(h), 0) self.assertEqual(h.capacity(), 256) self.assertEqual(h.get_array(), [])