Skip to content

Commit

Permalink
Updated peek() function
Browse files Browse the repository at this point in the history
Replaced if else conditionals with try except suite in order to catch IndexErrors if self.heap[1] is attempting to subscript and index out of bounds.

In response to issue joeyajames#108.
  • Loading branch information
wvadhy authored Jul 29, 2022
1 parent ca9255a commit 96704f3
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions MaxHeap.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def push(self, data):
self.__floatUp(len(self.heap) - 1)

def peek(self):
if self.heap[1]:
try:
return self.heap[1]
else:
except IndexError:
return False

def pop(self):
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()))

0 comments on commit 96704f3

Please sign in to comment.