From 003960a0311e5164318c4c46a1a0a29dea1fe89e Mon Sep 17 00:00:00 2001 From: InbarAbraham <152271955+InbarAbraham@users.noreply.github.com> Date: Thu, 16 Jan 2025 15:44:42 +0200 Subject: [PATCH] Update MaxHeap.py Updated the `peek` method to check if the heap contains at least one element before accessing `self.heap[1]`. This prevents a potential IndexError when the heap is empty. --- MaxHeap.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/MaxHeap.py b/MaxHeap.py index 0168aae5..2c2f978a 100644 --- a/MaxHeap.py +++ b/MaxHeap.py @@ -15,10 +15,10 @@ def push(self, data): self.__floatUp(len(self.heap) - 1) def peek(self): - if self.heap[1]: + if len(self.heap)>1: return self.heap[1] - else: - return False + else: + return false def pop(self): if len(self.heap) > 2: @@ -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())) \ No newline at end of file +print(str(m.pop()))