From 62781352649e3ec044120058bddbcd181d2365d3 Mon Sep 17 00:00:00 2001 From: Parthiv Date: Sat, 17 Jun 2023 22:32:29 +0530 Subject: [PATCH] checked for length of heap before accessing second element --- MaxHeap.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/MaxHeap.py b/MaxHeap.py index 0168aae5..6da6d305 100644 --- a/MaxHeap.py +++ b/MaxHeap.py @@ -15,8 +15,9 @@ def push(self, data): self.__floatUp(len(self.heap) - 1) def peek(self): - if self.heap[1]: - return self.heap[1] + if len(self.heap)>=2: + if self.heap[1]: + return self.heap[1] else: return False