Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 914e3aa

Browse files
Added Max Heap implementation
1 parent 395387d commit 914e3aa

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

‎Heap/heap.py‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
class Heap:
2+
def __init__(self, maxsize):
3+
'''
4+
Initialises the size of heap using maxsize parameter passed by users.
5+
'''
6+
self._maxsize = maxsize
7+
self._heap = [-1] * (self._maxsize + 1)
8+
self._size = 0
9+
10+
def __len__(self):
11+
'''
12+
Returns the size of heap.
13+
'''
14+
return self._size
15+
16+
def isempty(self):
17+
'''
18+
Returns True if heap is empty, else False.
19+
'''
20+
return self._size == 0
21+
22+
def insert(self, e):
23+
'''
24+
Insertion Operation.
25+
'''
26+
if self._size == self._maxsize:
27+
print(f"Max {self._maxsize} elements can be inserted !")
28+
else:
29+
self._size += 1
30+
hi = self._size
31+
32+
# while child > parent
33+
while e > self._heap[hi//2] and hi > 1:
34+
self._heap[hi] = self._heap[hi//2]
35+
hi = hi//2
36+
self._heap[hi] = e
37+
38+
def max(self):
39+
'''
40+
Returns the maximum element from the heap.
41+
'''
42+
return self._heap[1]
43+
44+
def deleteMax(self):
45+
'''
46+
Deletes the maximum element from the heap.
47+
'''
48+
root = self._heap[1]
49+
self._heap[1] = self._heap[self._size]
50+
self._heap[self._size] = -1
51+
self._size -= 1
52+
53+
i = 1
54+
j = i * 2
55+
while j <= self._size:
56+
if self._heap[j] < self._heap[j + 1]:
57+
j = j + 1
58+
if self._heap[i] < self._heap[j]:
59+
self._heap[i], self._heap[j] = self._heap[j], self._heap[i]
60+
i = j
61+
j = i * 2
62+
else:
63+
break
64+
return root
65+
66+
def display(self):
67+
'''
68+
Utility function to display the heap.
69+
'''
70+
if self.isempty():
71+
print("Heap is empty !")
72+
else:
73+
print("Heap: ", end='')
74+
for i in range(1, self._size + 1):
75+
print(self._heap[i], end=" ")
76+
77+
78+
def options():
79+
'''
80+
Prints Menu for operations
81+
'''
82+
options_list = ['Insert an item', 'Insert multiple items',
83+
'Max', 'Delete Max', 'Display', 'Exit']
84+
85+
print("\nMENU")
86+
for i, option in enumerate(options_list):
87+
print(f'{i + 1}. {option}')
88+
89+
choice = int(input("Enter choice: "))
90+
return choice
91+
92+
###############################################################################
93+
94+
95+
def switch_case(choice):
96+
'''
97+
Switch Case for operations
98+
'''
99+
# os.system('cls')
100+
if choice == 1:
101+
elem = int(input("Enter an Item: "))
102+
hp.insert(elem)
103+
elif choice == 2:
104+
A = list(map(int, input("Enter items: ").split()))
105+
for elem in A:
106+
hp.insert(elem)
107+
108+
elif choice == 3:
109+
print("Max element is: ", hp.max())
110+
111+
elif choice == 4:
112+
print("Deleted element is: ", hp.deleteMax())
113+
114+
elif choice == 5:
115+
hp.display()
116+
117+
elif choice == 6:
118+
import sys
119+
sys.exit()
120+
121+
###############################################################################
122+
123+
124+
if __name__ == '__main__':
125+
hp = Heap(10)
126+
while True:
127+
choice = options()
128+
switch_case(choice)

‎README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ Find the detailed description on operations performed [here.](Linked%20List/)
3737

3838
1. [Binary Tree](Binary%20Trees/BinaryTree.py)
3939
2. [Binary Search Tree](Binary%20Trees/BinarySearchTree.py)
40+
41+
### 5. Heap
42+
43+
1. [Max Heap](Heap/heap.py)

‎__init__.py‎

Whitespace-only changes.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /