-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeaps.py
More file actions
52 lines (46 loc) · 1.35 KB
/
Heaps.py
File metadata and controls
52 lines (46 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Heap:
def __init__(self):
self._maxsize = 10
self._data = [-1] * self._maxsize
self._csize = 0
def __len__(self):
return len(self._data)
def isempty(self):
return len(self._data) == 0
def insert(self, e):
if self._csize == self._maxsize:
print('No Space in Heap')
return
self._csize = self._csize + 1
hi = self._csize
while hi > 1 and e > self._data[hi // 2]:
self._data[hi] = self._data[hi // 2]
hi = hi // 2
self._data[hi] = e
def max(self):
if self._csize == 0:
print('Heap is Empty')
return
return self._data[1]
def deletemax(self):
if self._csize == 0:
print('Heap is Empty')
return
e = self._data[1]
self._data[1] = self._data[self._csize]
self._data[self._csize] = -1
self._csize = self._csize - 1
i = 1
j = i * 2
while j <= self._csize:
if self._data[j] < self._data[j+1]:
j = j + 1
if self._data[i] < self._data[j]:
temp = self._data[i]
self._data[i] = self._data[j]
self._data[j] = temp
i = j
j = i * 2
else:
break
return e