Skip to content

Update MaxHeap.py #178

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

Open
wants to merge 1 commit into
base: master
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
14 changes: 7 additions & 7 deletions MaxHeap.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class MaxHeap:
def __init__(self, items=[]):
super().__init__()
self.heap = [0]
self.heap = None
for i in items:
self.heap.append(i)
self.__floatUp(len(self.heap) - 1)
Expand All @@ -18,18 +18,18 @@ def peek(self):
if self.heap[1]:
return self.heap[1]
else:
return False
return None

def pop(self):
if len(self.heap) > 2:
self.__swap(1, len(self.heap) - 1)
max = self.heap.pop()
max1 = self.heap.pop()
self.__bubbleDown(1)
elif len(self.heap) == 2:
max = self.heap.pop()
max1 = self.heap.pop()
else:
max = False
return max
max1 = None
return max1

def __swap(self, i, j):
self.heap[i], self.heap[j] = self.heap[j], self.heap[i]
Expand Down Expand Up @@ -57,4 +57,4 @@ def __bubbleDown(self, index):
m = MaxHeap([95, 3, 21])
m.push(10)
print(str(m.heap[0:len(m.heap)]))
print(str(m.pop()))
print(str(m.pop()))