-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_heap.py
52 lines (51 loc) · 1.8 KB
/
build_heap.py
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
52
class HEAP:
def insert(self,list):
self.minheap = list
mid = int(len(self.minheap)-1)//2
while(mid>=0):
self.build_heap(mid)
mid-=1
def delete(self):
print(self.minheap[0])
if len(self.minheap)==1:
self.minheap.pop()
return
last=self.minheap.pop()
self.minheap[0] = last
self.extract_min(0)
def extract_min(self,parent):
if parent>=len(self.minheap):
return
index = parent
leftchild = 2*index+1
rightchild = leftchild+1
minimum = self.minheap[index]
if leftchild<len(self.minheap) and minimum > self.minheap[leftchild]:
minimum=self.minheap[leftchild]
index = leftchild
if rightchild<len(self.minheap) and minimum>self.minheap[rightchild]:
minimum = self.minheap[rightchild]
index = rightchild
if index != parent:
self.minheap[parent],self.minheap[index] = self.minheap[index],self.minheap[parent]
self.extract_min(index)
def build_heap(self,parent):
index = parent
leftchild = 2*index+1
rightchild = leftchild+1
minimum = self.minheap[index]
if leftchild<len(self.minheap) and minimum > self.minheap[leftchild]:
minimum=self.minheap[leftchild]
index = leftchild
if rightchild<len(self.minheap) and minimum>self.minheap[rightchild]:
minimum = self.minheap[rightchild]
index = rightchild
if index != parent:
self.minheap[parent],self.minheap[index] = self.minheap[index],self.minheap[parent]
def display(self):
print(self.minheap)
list = [5,7,9,0,1,5,12,11]
object = HEAP()
object.insert(list)
object.delete()
object.display()