Skip to content

Commit e3a4cd3

Browse files
authored
Merge pull request #1 from s1s1ty/master
Heap code and test file added
2 parents 6e2a4c8 + 2e982e5 commit e3a4cd3

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Heap
2+
3+
A **Heap** is a tree based data structure and it is a complete binary tree which satisfies the heap ordering property. The ordering can be one of two types:
4+
5+
the **min-heap property:** the value of each node is greater than or equal to the value of its parent, with the minimum-value element at the root.
6+
7+
the **max-heap property:** the value of each node is less than or equal to the value of its parent, with the maximum-value element at the root.
8+
9+
![heap](max_heap_deletion_animation.gif)
10+
11+
**Basic Operations:**
12+
13+
Following are the basic operations supported by a list.
14+
15+
* **Insertion** − Adds an element at the max heap.
16+
17+
* **Deletion** − Deletes an element at the beginning of the heap.
18+
19+
* **Display** − Displays the complete heap.
20+
21+
* **Max** − Get the max element from the heap.
22+
23+
24+
#### Complexity Analysis
25+
- Space - O(n)
26+
- Search - O(n)
27+
- Insertion - O(1)
28+
- Delete - O(log n) (Deletion from beginning)
29+
- Peek - O(1)
30+
### More on this topic
31+
- https://en.wikipedia.org/wiki/Heap_(data_structure)
32+
- https://www.hackerearth.com/practice/data-structures/trees/heapspriority-queues/tutorial/
33+
- https://www.geeksforgeeks.org/binary-heap/

Data Structure/Heap/heap.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""MaxHeap implementation using python"""
2+
3+
4+
class MaxHeap(object):
5+
6+
def __init__(self, maxSize=None):
7+
self.heap = []
8+
self.HEAP_SIZE = maxSize
9+
10+
def _swap(self,i,j):
11+
self.heap[i], self.heap[j] = self.heap[j], self.heap[i]
12+
13+
def _heapIsFull(self):
14+
return (self.HEAP_SIZE != None and len(self.heap) >= self.HEAP_SIZE)
15+
16+
def insert(self, item):
17+
if self._heapIsFull():
18+
"Heap is full..."
19+
else:
20+
self.heap.append(item)
21+
# adjust parent node item
22+
self._bubbleUp(len(self.heap)-1)
23+
24+
def _bubbleUp(self, currentPosition):
25+
if currentPosition >= 1: # no need to do bubbleUp for 1 element
26+
index = currentPosition
27+
parentIndex = (index-1)//2
28+
29+
if parentIndex >= 0 and self.heap[parentIndex] < self.heap[index]:
30+
self._swap(parentIndex, index)
31+
self._bubbleUp(parentIndex)
32+
33+
def peek(self):
34+
return self.heap[0] if self.heap else None
35+
36+
def pop(self):
37+
element = self.peek()
38+
if element:
39+
self._swap(0, len(self.heap) - 1)
40+
self.heap.pop()
41+
self._bubbleDown(0)
42+
return element
43+
44+
def _bubbleDown(self, index):
45+
leftChildIndex = 2 * index + 1
46+
rightChildIndex = 2 * index + 2
47+
largest = index
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
52+
if largest!=index:
53+
self._swap(index, largest)
54+
self._bubbleDown(largest)
Loading

Data Structure/Heap/test_heap.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
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")
21+
ob.pop()
22+
ob.pop()
23+
ob.pop()
24+
self.assertEqual(ob.peek(), None, msg="Max Element is not matched")
25+
26+
if __name__ == '__main__':
27+
unittest.main()
28+
29+

0 commit comments

Comments
 (0)