Skip to content

Commit 9907492

Browse files
shaontyshaonty
authored andcommitted
Heap code and test added
1 parent 6e2a4c8 commit 9907492

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
**/__pycache__/*
2+
*.pyc

Data Structure/Heap/README.md

Whitespace-only changes.

Data Structure/Heap/heap.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""MaxHeap implementation using python"""
2+
3+
4+
class MaxHeap(object):
5+
6+
HEAP_SIZE = 10
7+
8+
def __init__(self):
9+
self.heap = [0]*MaxHeap.HEAP_SIZE
10+
self.currentPosition = -1
11+
12+
def _swap(self,i,j):
13+
self.heap[i], self.heap[j] = self.heap[j], self.heap[i]
14+
15+
def _heapIsFull(self):
16+
if self.currentPosition == MaxHeap.HEAP_SIZE:
17+
return True
18+
return False
19+
20+
def insert(self, item):
21+
if self._heapIsFull():
22+
"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)
28+
29+
def _bubbleUp(self, currentPosition):
30+
index = currentPosition
31+
parrentIndex = index//2
32+
33+
if parrentIndex>=0 and self.heap[parrentIndex] < self.heap[index]:
34+
self._swap(parrentIndex, index)
35+
# index = parrentIndex
36+
self._bubbleUp(parrentIndex)
37+
38+
39+
def getMax(self):
40+
if self.heap[0]:
41+
return self.heap[0]
42+
else:
43+
return "Empty Heap"
44+
45+
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"
57+
58+
def _bubbleDown(self, index):
59+
left = 2*index+1
60+
right = 2*index+2
61+
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
66+
if largest!=index:
67+
self._swap(index, largest)
68+
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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import unittest
2+
from heap import MaxHeap
3+
4+
class MaxHeapTestCase(unittest.TestCase):
5+
""" Test for Heap.py"""
6+
def test(self):
7+
# test data
8+
ob = MaxHeap()
9+
ob.insert(10)
10+
ob.insert(5)
11+
ob.insert(6)
12+
ob.insert(3)
13+
ob.insert(8)
14+
ob.insert(20)
15+
16+
ob.pop()
17+
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")
22+
23+
if __name__ == '__main__':
24+
unittest.main()

0 commit comments

Comments
 (0)