Skip to content

Commit 2e982e5

Browse files
shaontyshaonty
authored andcommitted
updated
1 parent 0566faa commit 2e982e5

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

Data Structure/Heap/heap.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ def insert(self, item):
2424
def _bubbleUp(self, currentPosition):
2525
if currentPosition >= 1: # no need to do bubbleUp for 1 element
2626
index = currentPosition
27-
parrentIndex = index//2
27+
parentIndex = (index-1)//2
2828

29-
if parrentIndex >= 0 and self.heap[parrentIndex] < self.heap[index]:
30-
self._swap(parrentIndex, index)
31-
self._bubbleUp(parrentIndex)
29+
if parentIndex >= 0 and self.heap[parentIndex] < self.heap[index]:
30+
self._swap(parentIndex, index)
31+
self._bubbleUp(parentIndex)
3232

3333
def peek(self):
34-
return self.heap[0] if self.heap else False
34+
return self.heap[0] if self.heap else None
3535

3636
def pop(self):
3737
element = self.peek()
@@ -42,7 +42,7 @@ def pop(self):
4242
return element
4343

4444
def _bubbleDown(self, index):
45-
leftChildIndex = 2 * index + 1
45+
leftChildIndex = 2 * index + 1
4646
rightChildIndex = 2 * index + 2
4747
largest = index
4848
if len(self.heap) > leftChildIndex and self.heap[largest] < self.heap[leftChildIndex]:

Data Structure/Heap/test_heap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test(self):
2121
ob.pop()
2222
ob.pop()
2323
ob.pop()
24-
self.assertEqual(ob.peek(), False, msg="Max Element is not matched")
24+
self.assertEqual(ob.peek(), None, msg="Max Element is not matched")
2525

2626
if __name__ == '__main__':
2727
unittest.main()

0 commit comments

Comments
 (0)