Skip to content

Heap code and test file added #1

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

Merged
merged 6 commits into from
Mar 27, 2018
Merged
Show file tree
Hide file tree
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
updated
  • Loading branch information
shaonty authored and shaonty committed Mar 27, 2018
commit 2e982e5b88a794837c6bf1d297b2f6869af929e5
12 changes: 6 additions & 6 deletions Data Structure/Heap/heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ def insert(self, item):
def _bubbleUp(self, currentPosition):
if currentPosition >= 1: # no need to do bubbleUp for 1 element
index = currentPosition
parrentIndex = index//2
parentIndex = (index-1)//2

if parrentIndex >= 0 and self.heap[parrentIndex] < self.heap[index]:
self._swap(parrentIndex, index)
self._bubbleUp(parrentIndex)
if parentIndex >= 0 and self.heap[parentIndex] < self.heap[index]:
self._swap(parentIndex, index)
self._bubbleUp(parentIndex)

def peek(self):
return self.heap[0] if self.heap else False
return self.heap[0] if self.heap else None

def pop(self):
element = self.peek()
Expand All @@ -42,7 +42,7 @@ def pop(self):
return element

def _bubbleDown(self, index):
leftChildIndex = 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]:
Expand Down
2 changes: 1 addition & 1 deletion Data Structure/Heap/test_heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test(self):
ob.pop()
ob.pop()
ob.pop()
self.assertEqual(ob.peek(), False, msg="Max Element is not matched")
self.assertEqual(ob.peek(), None, msg="Max Element is not matched")

if __name__ == '__main__':
unittest.main()
Expand Down