def heap_sort(a): h = BinaryHeap() h.a = a h.n = len(a) m = h.n // 2 for i in range(m - 1, -1, -1): h.trickle_down(i) while h.n > 1: h.n -= 1 h.a[h.n], h.a[0] = h.a[0], h.a[h.n] h.trickle_down(0) a.reverse()
class HeapMemory(LearnerMemory): def __init__(self, memory_size=1e+6): self.history = BinaryHeap() self.memory_size = memory_size def append(self, item): if len(self.history) == self.memory_size: self.history.trim() self.history.insert(item, self.history.max_priority()) def sample(self, sample_size: int): return self.history.sample(sample_size) def change_priority(self, ind, priority): self.history.change_priority(ind, priority) def sort(self): self.history.sort() def __len__(self): return len(self.history)
def heap_sort(a): h = BinaryHeap() h.a = a h.n = len(a) m = h.n//2 for i in range(m-1, -1, -1): h.trickle_down(i) while h.n > 1: h.n -= 1 h.a[h.n], h.a[0] = h.a[0], h.a[h.n] h.trickle_down(0) a.reverse()
def __init__(self, memory_size=1e+6): self.history = BinaryHeap() self.memory_size = memory_size
def test_negative_priority(self): custom_heap = BinaryHeap() custom_heap.insert(wa, -50) custom_heap.insert(wb, -100) self.heap._check_rep() self.assertEqual(custom_heap.array(), [wa, wb])
class TestBinaryHeap(unittest.TestCase): def setUp(self): self.heap = BinaryHeap() # insert: def test_insert(self): self.heap.insert(wa, 100) self.heap._check_rep() self.assertEqual(self.heap.array(), [wa]) def test_insert_two(self): self.heap.insert(wa, 100) self.heap.insert(wb, 50) self.heap._check_rep() self.assertEqual(self.heap.array(), [wa, wb]) def test_insert_out_of_order(self): self.heap.insert(wa, 50) self.heap.insert(wb, 100) self.heap._check_rep() self.assertEqual(self.heap.array(), [wb, wa]) def test_negative_priority(self): custom_heap = BinaryHeap() custom_heap.insert(wa, -50) custom_heap.insert(wb, -100) self.heap._check_rep() self.assertEqual(custom_heap.array(), [wa, wb]) def test_insert_many(self): self.heap.insert(wa, -100) self.heap.insert(wb, -50) self.heap.insert(wc, 0) self.heap.insert(wd, 50) self.heap.insert(we, 100) self.assertEqual(set(self.heap.array()), {wa, wb, wc, wd, we}) self.heap._check_rep() def test_sort(self): self.heap.insert(wa, -100) self.heap.insert(wb, 50) self.heap.insert(wc, 0) self.heap.insert(wd, -50) self.heap.insert(we, 100) self.heap.sort() self.heap._check_rep() self.assertEqual(self.heap.array(), [we, wb, wc, wd, wa]) def test_decrease_key(self): self.heap.insert(wa, -100) self.heap.insert(wb, 50) self.heap.insert(wc, 0) self.heap.insert(wd, -50) self.heap.insert(we, 100) self.heap.sort() self.heap._check_rep() self.assertEqual(self.heap.array(), [we, wb, wc, wd, wa]) self.heap.change_priority(wb, -2000) self.assertNotEqual(self.heap.array(), [we, wb, wc, wd, wa]) self.heap._check_rep() self.heap.sort() self.assertEqual(self.heap.array(), [we, wc, wd, wa, wb]) self.heap._check_rep() def test_increase_key(self): self.heap.insert(wa, -100) self.heap.insert(wb, 50) self.heap.insert(wc, 0) self.heap.insert(wd, -50) self.heap.insert(we, 100) self.heap.sort() self.heap._check_rep() self.assertEqual(self.heap.array(), [we, wb, wc, wd, wa]) self.heap.change_priority(wb, 2000) self.assertNotEqual(self.heap.array(), [we, wb, wc, wd, wa]) self.heap._check_rep() self.heap.sort() self.assertEqual(self.heap.array(), [wb, we, wc, wd, wa]) def test_max_priority(self): self.heap.insert(wa, very_big_number) self.assertEqual(self.heap.max_priority(), very_big_number) def test_max_priority_multiple(self): self.heap.insert(wc, 20) self.heap.insert(wa, very_big_number) self.heap.insert(we, -500) self.assertEqual(self.heap.max_priority(), very_big_number)
def setUp(self): self.heap = BinaryHeap()
from binaryheap import BinaryHeap alist: list = [54, 26, 93, 17, 77, 31, 44, 55, 20] bh = BinaryHeap() bh.buildHeap(alist) sortedList: list = [None] * bh.size() pointer: int = 0 while not bh.isEmpty(): sortedList[pointer] = bh.deleteMin() pointer += 1 print(sortedList)
from binaryheap import BinaryHeap if __name__ == "__main__": heap = BinaryHeap() data = [("dog", 1), ("cat", 2), ("bat", 3), ("slug", 1), ("rat", 4), ("mule", 3), ("fish", 2), ("goat", 5), ("fly", 3), ("snail", 4), ("bird", 6), ("fox", 2)] for x in data: print("Insert:", x) heap.insert(x) print(heap) print("-" * 50) print("-" * 50) for i in range(11): print("Delete:", heap.peek()) heap.delete_max() print(heap) print("-" * 50)
def __init__(self): self._queue = BinaryHeap()
class APQ: def __init__(self): self._queue = BinaryHeap() def __str__(self): heap_items = [(i._element.to_string()) for i in self._queue._heap[1::] if i is not None] heap_items_str = ' -> '.join(heap_items) print(heap_items_str) def get_size(self): return self._queue.getSize() def add(self, k, v): temp_el = Element(k, v) self._queue.add(temp_el) def build_apq(self, temp_dict): temp_list = [Element(mydict[i], i) for i in temp_dict] return self._queue.heapHelper(temp_list) def min(self): return self._queue.getMin() def remove_min(self): # Some issues with __str__() return self._queue.removeMin() def get_parent(self, i): if (i - 1) // 2 == 0: return self._queue._heap[1] else: return self._queue.getParent(i) def update_key(self, element, newkey): for i in range(len(self._queue._heap[1::]) + 1): if self._queue._heap[i]._element: if self._queue._heap[i]._element._value == element: self._queue._heap[i]._element._key = newkey if self._queue._heap[i]._element < self.get_parent( i + 1)._element: self._queue.percUp(i) else: self._queue.percDown(i) def get_key(self, element): for i in range(len(self._queue._heap[1::]) + 1): if self._queue._heap[i]._element: # print(self._queue._heap[i]._element._value) if self._queue._heap[i]._element == element: return self._queue._heap[i]._element._key def remove_element(self, element): for i in range(len(self._queue._heap[1::]) + 1): if self._queue._heap[i]._element: # print(self._queue._heap[i]._element._value) if self._queue._heap[i]._element._value == element: removal = self._queue._heap[i]._element self._queue.swap(i, 1) self.remove_min() # return key, value return removal.__str__()
from binaryheap import BinaryHeap if __name__ == "__main__": heap = BinaryHeap() data = [ ("dog", 1), ("cat", 2), ("bat", 3), ("slug", 1), ("rat", 4), ("mule", 3), ("fish", 2), ("goat", 5), ("fly", 3), ("snail", 4), ("bird", 6), ("fox", 2), ] for x in data: print("Insert:", x) heap.insert(x) print(heap) print("-" * 50) print("-" * 50) for i in range(11): print("Delete:", heap.peek()) heap.delete_max() print(heap) print("-" * 50)