|
| 1 | +class BinaryHeap: |
| 2 | + def __init__(self, maxsize, heapType): |
| 3 | + self.customList = [None] * (maxsize + 1) |
| 4 | + self.maxsize = maxsize + 1 |
| 5 | + self.heapSize = 0 |
| 6 | + self.heapType = heapType |
| 7 | + |
| 8 | + def levelOrderTraversal(self): |
| 9 | + if len(self.customList) == 0: |
| 10 | + return |
| 11 | + else: |
| 12 | + for i in range(1, self.heapSize + 1): |
| 13 | + print(self.customList[i]) |
| 14 | + |
| 15 | + def swap(self, firstIndex, secondIndex): |
| 16 | + temp = self.customList[firstIndex] |
| 17 | + self.customList[firstIndex] = self.customList[secondIndex] |
| 18 | + self.customList[secondIndex] = temp |
| 19 | + |
| 20 | + def heapify(self, index): |
| 21 | + parentIndex = int(index / 2) |
| 22 | + if index <= 1: |
| 23 | + return |
| 24 | + elif self.heapType == "Min": |
| 25 | + if self.customList[index] < self.customList[parentIndex]: |
| 26 | + self.swap(index, parentIndex) |
| 27 | + self.heapify(parentIndex) |
| 28 | + elif self.heapType == "Max": |
| 29 | + if self.customList[index] > self.customList[parentIndex]: |
| 30 | + self.swap(index, parentIndex) |
| 31 | + self.heapify(parentIndex) |
| 32 | + |
| 33 | + def insert(self, nodeValue): |
| 34 | + if self.heapSize + 1 == self.maxsize: |
| 35 | + return "Binary heap is full" |
| 36 | + self.heapSize += 1 |
| 37 | + self.customList[self.heapSize] = nodeValue |
| 38 | + self.heapify(self.heapSize) |
| 39 | + |
| 40 | + def heapifyExtractNode(self, index): |
| 41 | + leftIndex = index * 2 |
| 42 | + rightIndex = index * 2 + 1 |
| 43 | + |
| 44 | + if leftIndex > self.heapSize or rightIndex > self.heapSize: |
| 45 | + return |
| 46 | + |
| 47 | + if self.heapType == "Min": |
| 48 | + if self.customList[leftIndex] <= self.customList[rightIndex]: |
| 49 | + self.swap(index, leftIndex) |
| 50 | + self.heapifyExtractNode(leftIndex) |
| 51 | + else: |
| 52 | + self.swap(index, rightIndex) |
| 53 | + self.heapifyExtractNode(rightIndex) |
| 54 | + elif self.heapType == "Max": |
| 55 | + if self.customList[leftIndex] >= self.customList[rightIndex]: |
| 56 | + self.swap(index, leftIndex) |
| 57 | + self.heapifyExtractNode(leftIndex) |
| 58 | + else: |
| 59 | + self.swap(index, rightIndex) |
| 60 | + self.heapifyExtractNode(rightIndex) |
| 61 | + |
| 62 | + def extractNode(self): |
| 63 | + if self.heapSize < 1: |
| 64 | + return "There are no elements in the binary heap" |
| 65 | + |
| 66 | + elementToRemove = self.customList[1] |
| 67 | + |
| 68 | + if self.heapSize < 2: |
| 69 | + self.customList[1] = None |
| 70 | + self.heapSize -= 1 |
| 71 | + else: |
| 72 | + self.customList[1] = self.customList[self.heapSize] |
| 73 | + self.customList[self.heapSize] = None |
| 74 | + self.heapSize -= 1 |
| 75 | + |
| 76 | + self.heapifyExtractNode(1) |
| 77 | + return elementToRemove |
| 78 | + |
| 79 | + |
| 80 | +binaryHeapMin = BinaryHeap(10, "Min") |
| 81 | + |
| 82 | +binaryHeapMin.insert(70) |
| 83 | +binaryHeapMin.insert(40) |
| 84 | +binaryHeapMin.insert(30) |
| 85 | +binaryHeapMin.insert(90) |
| 86 | +binaryHeapMin.insert(80) |
| 87 | +binaryHeapMin.extractNode() |
| 88 | +binaryHeapMin.levelOrderTraversal() |
0 commit comments