Skip to content

Commit

Permalink
Updated binary_heap (keon#785)
Browse files Browse the repository at this point in the history
* Corrected spelling anf grammar errors in docstring for heap.

* Fixed method signature for remove_min by removing unused parameter i.

* Fixed grammar in min_child docstring.
  • Loading branch information
cliftonamccook authored May 31, 2021
1 parent 0d3e4c1 commit 9b6b252
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions algorithms/heap/binary_heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def perc_down(self,i):
def min_child(self,i):
pass
@abstractmethod
def remove_min(self,i):
def remove_min(self):
pass
class BinaryHeap(AbstractHeap):
def __init__(self):
Expand All @@ -64,8 +64,8 @@ def perc_up(self, i):

"""
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,
It inserts rightmost spot so as to maintain the complete tree property.
Then, it fixes 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)
Expand All @@ -76,7 +76,7 @@ def insert(self, val):
self.perc_up(self.currentSize)

"""
Method min_child returns index of smaller 2 childs of its parent
Method min_child returns the index of smaller of 2 children of parent at index i
"""
def min_child(self, i):
if 2 * i + 1 > self.currentSize: # No right child
Expand Down Expand Up @@ -104,7 +104,7 @@ def perc_down(self, i):
"""
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.heap[1] = self.heap[self.currentSize] # Replace it by the last value
self.currentSize = self.currentSize - 1
self.heap.pop()
self.perc_down(1)
Expand Down

0 comments on commit 9b6b252

Please sign in to comment.