Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create AbstractHeap.py #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions AbstractHeap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from abc import ABCMeta, abstractmethod

class AbstractHeap(metaclass=ABCMeta):
"""Abstract Class for Binary Heap."""
def __init__(self):
pass
@abstractmethod
def perc_up(self, i):
pass
@abstractmethod
def insert(self, val):
pass
@abstractmethod
def perc_down(self,i):
pass
@abstractmethod
def min_child(self,i):
pass
@abstractmethod
def remove_min(self,i):
pass
class BinaryHeap(AbstractHeap):
def __init__(self):
self.currentSize = 0
self.heap = [(0)]

def perc_up(self, i):
while i // 2 > 0:
if self.heap[i] < self.heap[i // 2]:
# Swap value of child with value of its parent
self.heap[i], self.heap[i//2] = self.heap[i//2], self.heap[i]
i = i // 2

"""
Method insert always start by inserting the element at the bottom.
it inserts rightmost spot so as to maintain the complete tree property
Then, it fix the tree by swapping the new element with its parent,
until it finds an appropriate spot for the element. It essentially
perc_up the minimum element
Complexity: O(logN)
"""
def insert(self, val):
self.heap.append(val)
self.currentSize = self.currentSize + 1
self.perc_up(self.currentSize)

"""
Method min_child returns index of smaller 2 childs of its parent
"""
def min_child(self, i):
if 2 * i + 1 > self.currentSize: # No right child
return 2 * i
else:
# left child > right child
if self.heap[2 * i] > self.heap[2 * i +1]:
return 2 * i + 1
else:
return 2 * i

def perc_down(self, i):
while 2 * i < self.currentSize:
min_child = self.min_child(i)
if self.heap[min_child] < self.heap[i]:
# Swap min child with parent
self.heap[min_child], self.heap[i] = self.heap[i], self.heap[min_child]
i = min_child
"""
Remove Min method removes the minimum element and swap it with the last
element in the heap( the bottommost, rightmost element). Then, it
perc_down this element, swapping it with one of its children until the
min heap property is restored
Complexity: O(logN)
"""
def remove_min(self):
ret = self.heap[1] # the smallest value at beginning
self.heap[1] = self.heap[self.currentSize] # Repalce it by the last value
self.currentSize = self.currentSize - 1
self.heap.pop()
self.perc_down(1)
return ret