Created
October 30, 2017 14:48
-
-
Save leikahing/28c9b7e26cfb2714a230b764d36aa3a5 to your computer and use it in GitHub Desktop.
A really basic Heap implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MaxHeap(object): | |
def __init__(self): | |
self.data = [0] | |
self.size = 0 | |
def left_child(self, index): | |
if 2 * index <= self.size: | |
return 2*index | |
return None | |
def right_child(self, index): | |
if 2 * index + 1 <= self.size: | |
return 2 * index + 1 | |
return None | |
def insert(self, val): | |
# add element to end of heap data | |
# max-heap it | |
# Compare element to its parent (child-index / 2) | |
# if it's greater than parent, swap | |
# repeat | |
self.data.append(val) | |
self.size += 1 | |
self.upheap(self.size) | |
def upheap(self, index): | |
current_index = index | |
while current_index // 2 > 0: | |
parent_index = current_index // 2 | |
if self.data[current_index] > self.data[parent_index]: | |
# swap them | |
self.data[current_index], self.data[parent_index] = self.data[parent_index], self.data[current_index] | |
current_index = parent_index | |
else: | |
break | |
def extract_max(self): | |
self.data[1], self.data[len(self.data)-1] = self.data[len(self.data)-1], self.data[1] | |
val = self.data.pop() | |
self.size -= 1 | |
self.max_heapify(1) | |
return val | |
def max_heapify(self, index): | |
left = self.left_child(index) | |
right = self.right_child(index) | |
if left and self.data[left] > self.data[index]: | |
largest = left | |
else: | |
largest = index | |
if right and self.data[right] > self.data[largest]: | |
largest = right | |
if largest != index: | |
# exchange largest with index | |
self.data[largest], self.data[index] = self.data[index], self.data[largest] | |
self.max_heapify(largest) | |
def min_heapify(self, index): | |
left = self.left_child(index) | |
right = self.right_child(index) | |
if left and self.data[left] < self.data[index]: | |
smallest = left | |
else: | |
smallest = index | |
if right and self.data[right] < self.data[smallest]: | |
smallest = right | |
if smallest != index: | |
self.data[smallest], self.data[index] = self.data[index], self.data[smallest] | |
self.min_heapify(smallest) | |
def sort(self): | |
for index in range(len(self.data)-1, 1, -1): | |
self.data[1], self.data[index] = self.data[index], self.data[1] | |
self.size -= 1 | |
self.max_heapify(1) | |
self.size = len(self.data) - 1 | |
def dec_sort(self): | |
for index in range(len(self.data)-1, 1, -1): | |
self.data[1], self.data[index] = self.data[index], self.data[1] | |
self.size -= 1 | |
self.min_heapify(1) | |
self.size = len(self.data) - 1 | |
def rebuild_max(self): | |
# rebuild into max heap if we're not a heap anymore | |
for i in range(len(self.data)//2, 0, -1): | |
self.max_heapify(i) | |
def rebuild_min(self): | |
for i in range(len(self.data)//2, 0, -1): | |
self.min_heapify(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment