|
| 1 | +from collections import deque |
| 2 | + |
| 3 | +class MaxHeap: |
| 4 | + def __init__(self, arr=[]): |
| 5 | + self.data = deque() |
| 6 | + self.size = 0 |
| 7 | + if len(arr) > 0: |
| 8 | + self.size = len(arr) |
| 9 | + self.__heapify(arr) |
| 10 | + |
| 11 | + def getMax(self): |
| 12 | + if self.size > 0: |
| 13 | + ret = self.data.popleft() |
| 14 | + self.size -= 1 |
| 15 | + self.data.appendleft(self.data.pop()) |
| 16 | + self.__bubbleDown(0) |
| 17 | + return ret |
| 18 | + |
| 19 | + def peek(self): |
| 20 | + if self.size > 0: |
| 21 | + return self.data[0] |
| 22 | + |
| 23 | + def push(self, val): |
| 24 | + self.data.append(val) |
| 25 | + self.size += 1 |
| 26 | + self.__bubbleUp(self.size-1) |
| 27 | + |
| 28 | + def isEmpty(self): |
| 29 | + return self.size == 0 |
| 30 | + |
| 31 | + def __heapify(self, arr): |
| 32 | + self.data = deque(arr) |
| 33 | + for i in xrange(len(arr)-1, -1, -1): |
| 34 | + self.__bubbleDown(i) |
| 35 | + |
| 36 | + def __bubbleDown(self, index): |
| 37 | + left = self.__left(index) |
| 38 | + right = self.__right(index) |
| 39 | + |
| 40 | + larger = index |
| 41 | + |
| 42 | + if left < self.size and self.data[index] < self.data[left]: |
| 43 | + larger = left |
| 44 | + |
| 45 | + if right < self.size and self.data[left] < self.data[right]: |
| 46 | + larger = right |
| 47 | + |
| 48 | + if larger != index: |
| 49 | + self.data[larger], self.data[index] = self.data[index], self.data[larger] |
| 50 | + self.__bubbleDown(larger) |
| 51 | + |
| 52 | + def __bubbleUp(self, index): |
| 53 | + p = self.__parent(index) |
| 54 | + |
| 55 | + if p >= 0 and self.data[index] > self.data[p]: |
| 56 | + self.data[index], self.data[p] = self.data[p], self.data[index] |
| 57 | + self.__bubbleUp(p) |
| 58 | + |
| 59 | + def __parent(self, i): |
| 60 | + return i/2 |
| 61 | + |
| 62 | + def __left(self, i): |
| 63 | + return 2*i + 1 |
| 64 | + |
| 65 | + def __right(self, i): |
| 66 | + return 2*i + 2 |
| 67 | + |
| 68 | +def testPush(): |
| 69 | + h = MaxHeap() |
| 70 | + h.push(2) |
| 71 | + assert h.data[0] == 2 |
| 72 | + |
| 73 | + h.push(3) |
| 74 | + assert h.data[0] == 3 |
| 75 | + |
| 76 | + h.push(1) |
| 77 | + assert h.data[0] == 3 |
| 78 | + |
| 79 | + h.push(5) |
| 80 | + assert h.data[0] == 5 |
| 81 | + |
| 82 | + h.push(-1) |
| 83 | + assert h.data[0] == 5 |
| 84 | + |
| 85 | +def testGetMax(): |
| 86 | + heap = MaxHeap([2, 5, 20]) |
| 87 | + assert heap.getMax() == 20 |
| 88 | + assert heap.getMax() == 5 |
| 89 | + |
| 90 | + heap1 = MaxHeap([1, 2, 3, 4, 5]) |
| 91 | + assert heap1.getMax() == 5 |
| 92 | + assert heap1.getMax() == 4 |
| 93 | + |
| 94 | + heap2 = MaxHeap([5, 4, 3, 2, 1]) |
| 95 | + assert heap2.getMax() == 5 |
| 96 | + assert heap2.getMax() == 4 |
| 97 | + |
| 98 | + heap3 = MaxHeap([2, 5, 3, 7, -1]) |
| 99 | + assert heap3.getMax() == 7 |
| 100 | + assert heap3.getMax() == 5 |
| 101 | + |
| 102 | +def testPeek(): |
| 103 | + heap = MaxHeap([2, 5, 20]) |
| 104 | + assert heap.peek() == 20 |
| 105 | + assert heap.peek() == 20 |
| 106 | + |
| 107 | + heap1 = MaxHeap([1, 2, 3, 4, 5]) |
| 108 | + assert heap1.peek() == 5 |
| 109 | + assert heap1.peek() == 5 |
| 110 | + |
| 111 | + heap2 = MaxHeap([5, 4, 3, 2, 1]) |
| 112 | + assert heap2.peek() == 5 |
| 113 | + assert heap2.peek() == 5 |
| 114 | + |
| 115 | + heap3 = MaxHeap([2, 5, 3, 7, -1]) |
| 116 | + assert heap3.peek() == 7 |
| 117 | + assert heap3.peek() == 7 |
| 118 | + |
| 119 | +def testSize(): |
| 120 | + heap = MaxHeap() |
| 121 | + assert heap.size == 0 |
| 122 | + |
| 123 | + heap2 = MaxHeap([1]) |
| 124 | + assert heap2.size == 1 |
| 125 | + |
| 126 | + heap3 = MaxHeap([1, 2, 3, 4]) |
| 127 | + assert heap3.size == 4 |
| 128 | + |
| 129 | +def testIsEmpty(): |
| 130 | + heap = MaxHeap() |
| 131 | + assert heap.isEmpty() |
| 132 | + |
| 133 | + heap2 = MaxHeap([1]) |
| 134 | + assert not heap2.isEmpty() |
| 135 | + |
| 136 | + heap3 = MaxHeap([1, 2, 3]) |
| 137 | + assert not heap3.isEmpty() |
| 138 | + |
| 139 | +def testEverything(): |
| 140 | + assert True |
| 141 | + |
| 142 | +def tests(): |
| 143 | + testPush() |
| 144 | + testGetMax() |
| 145 | + testPeek() |
| 146 | + testSize() |
| 147 | + testIsEmpty() |
| 148 | + testEverything() |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + tests() |
0 commit comments