Skip to content

Commit 12ec380

Browse files
committed
Heaps
1 parent 2338b4c commit 12ec380

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

PQ & Heap/HeapInsert.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Heap:
2+
def __init__(self):
3+
self._maxsize = 10
4+
self._data = [-1] * self._maxsize
5+
self._csize = 0
6+
7+
def __len__(self):
8+
return len(self._data)
9+
10+
def isempty(self):
11+
return len(self._data) == 0
12+
13+
def insert(self, e):
14+
if self._csize == self._maxsize:
15+
print('No Space in Heap')
16+
return
17+
self._csize = self._csize + 1
18+
hi = self._csize
19+
while hi > 1 and e > self._data[hi // 2]:
20+
self._data[hi] = self._data[hi // 2]
21+
hi = hi // 2
22+
self._data[hi] = e
23+
24+
def max(self):
25+
if self._csize == 0:
26+
print('Heap is Empty')
27+
return
28+
return self._data[1]
29+
30+
S = Heap()
31+
S.insert(25)
32+
S.insert(14)
33+
S.insert(2)
34+
S.insert(20)
35+
S.insert(10)
36+
print(S._data)
37+
S.insert(40)
38+
print(S._data)
39+
40+

0 commit comments

Comments
 (0)