Skip to content

Commit 0cbf7ce

Browse files
shaontyshaonty
authored andcommitted
heap updated
1 parent 24f5dbf commit 0cbf7ce

File tree

2 files changed

+38
-56
lines changed

2 files changed

+38
-56
lines changed

Data Structure/Heap/heap.py

Lines changed: 28 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,75 +3,52 @@
33

44
class MaxHeap(object):
55

6-
HEAP_SIZE = 10
7-
8-
def __init__(self):
9-
self.heap = [0]*MaxHeap.HEAP_SIZE
10-
self.currentPosition = -1
6+
def __init__(self, maxSize=None):
7+
self.heap = [float("inf")] # heap starting from index 1
8+
self.HEAP_SIZE = maxSize
119

1210
def _swap(self,i,j):
1311
self.heap[i], self.heap[j] = self.heap[j], self.heap[i]
1412

1513
def _heapIsFull(self):
16-
if self.currentPosition == MaxHeap.HEAP_SIZE:
17-
return True
18-
return False
14+
return (self.HEAP_SIZE != None and len(self.heap) >= self.HEAP_SIZE)
1915

2016
def insert(self, item):
2117
if self._heapIsFull():
2218
"Heap is full..."
23-
return
24-
self.currentPosition+=1
25-
self.heap[self.currentPosition] = item
26-
# adjust parent node item
27-
self._bubbleUp(self.currentPosition)
19+
else:
20+
self.heap.append(item)
21+
# adjust parent node item
22+
self._bubbleUp(len(self.heap)-1)
2823

2924
def _bubbleUp(self, currentPosition):
30-
index = currentPosition
31-
parrentIndex = index//2
25+
if currentPosition >= 2: # no need to do bubbleUp for 1 element
26+
index = currentPosition
27+
parrentIndex = index//2
3228

33-
if parrentIndex>=0 and self.heap[parrentIndex] < self.heap[index]:
34-
self._swap(parrentIndex, index)
35-
# index = parrentIndex
36-
self._bubbleUp(parrentIndex)
29+
if parrentIndex >= 0 and self.heap[parrentIndex] < self.heap[index]:
30+
self._swap(parrentIndex, index)
31+
self._bubbleUp(parrentIndex)
3732

38-
39-
def getMax(self):
40-
if self.heap[0]:
41-
return self.heap[0]
42-
else:
43-
return "Empty Heap"
33+
def peek(self):
34+
return self.heap[1] if len(self.heap) > 1 else False
4435

4536
def pop(self):
46-
if self.currentPosition > 1:
47-
# swap first element with last element
48-
self._swap(0,self.currentPosition)
49-
del self.heap[self.currentPosition]
50-
# adjust parent node item
51-
self._bubbleDown(0)
52-
elif self.currentPosition == 1:
53-
del self.heap[0]
54-
else:
55-
print "No deletion operation for empty heap"
56-
return "False"
37+
element = self.peek()
38+
if element:
39+
self._swap(1, len(self.heap) - 1)
40+
self.heap.pop()
41+
self._bubbleDown(1)
42+
return element
5743

5844
def _bubbleDown(self, index):
59-
left = 2*index+1
60-
right = 2*index+2
45+
leftChildIndex = 2 * index
46+
rightChildIndex = 2 * index + 1
6147
largest = index
62-
if len(self.heap) > left and self.heap[largest] < self.heap[left]:
63-
largest = left
64-
if len(self.heap) > right and self.heap[largest] < self.heap[right]:
65-
largest = right
48+
if len(self.heap) > leftChildIndex and self.heap[largest] < self.heap[leftChildIndex]:
49+
largest = leftChildIndex
50+
if len(self.heap) > rightChildIndex and self.heap[largest] < self.heap[rightChildIndex]:
51+
largest = rightChildIndex
6652
if largest!=index:
6753
self._swap(index, largest)
6854
self._bubbleDown(largest)
69-
70-
def printHeap(self):
71-
all_hp_element = []
72-
for i in self.heap:
73-
if i!=0:
74-
all_hp_element.append(i)
75-
return all_hp_element
76-
77-

Data Structure/Heap/test_heap.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@ def test(self):
1212
ob.insert(3)
1313
ob.insert(8)
1414
ob.insert(20)
15-
15+
self.assertEqual(ob.peek(), 20, msg="Max Element is not matched")
16+
ob.pop()
17+
ob.pop()
18+
self.assertEqual(ob.peek(), 8, msg="Max Element is not matched")
19+
ob.pop()
20+
self.assertEqual(ob.peek(), 6, msg="Max Element is not matched")
1621
ob.pop()
1722
ob.pop()
18-
19-
get_max = ob.getMax()
20-
self.assertEqual(ob.printHeap(), [8, 6, 3, 5], msg="Heap Element Should be [8,6,3,5]")
21-
self.assertEqual(get_max, 8, msg="Max Element is not matched")
23+
ob.pop()
24+
self.assertEqual(ob.peek(), False, msg="Max Element is not matched")
2225

2326
if __name__ == '__main__':
2427
unittest.main()
28+
29+

0 commit comments

Comments
 (0)