Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
updated
  • Loading branch information
shaonty authored and shaonty committed Mar 27, 2018
commit 0566faa95bb759ee0c64e8646826336565f86483
14 changes: 7 additions & 7 deletions Data Structure/Heap/heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class MaxHeap(object):

def __init__(self, maxSize=None):
self.heap = [float("inf")] # heap starting from index 1
self.heap = []
self.HEAP_SIZE = maxSize

def _swap(self,i,j):
Expand All @@ -22,7 +22,7 @@ def insert(self, item):
self._bubbleUp(len(self.heap)-1)

def _bubbleUp(self, currentPosition):
if currentPosition >= 2: # no need to do bubbleUp for 1 element
if currentPosition >= 1: # no need to do bubbleUp for 1 element
index = currentPosition
parrentIndex = index//2

Expand All @@ -31,19 +31,19 @@ def _bubbleUp(self, currentPosition):
self._bubbleUp(parrentIndex)

def peek(self):
return self.heap[1] if len(self.heap) > 1 else False
return self.heap[0] if self.heap else False

def pop(self):
element = self.peek()
if element:
self._swap(1, len(self.heap) - 1)
self._swap(0, len(self.heap) - 1)
self.heap.pop()
self._bubbleDown(1)
self._bubbleDown(0)
return element

def _bubbleDown(self, index):
leftChildIndex = 2 * index
rightChildIndex = 2 * index + 1
leftChildIndex = 2 * index + 1
rightChildIndex = 2 * index + 2
largest = index
if len(self.heap) > leftChildIndex and self.heap[largest] < self.heap[leftChildIndex]:
largest = leftChildIndex
Expand Down