-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_heap.py
64 lines (57 loc) · 1.7 KB
/
min_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
53
54
55
56
57
58
59
class HEAP:
def __init__(self) -> None:
self.minheap = list()
def insert(self,element):
self.minheap.append(element)
self.heap_up(len(self.minheap)-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 heap_up(self,index):
if index==0:
return
parent = int((index-1)/2)
if self.minheap[index] < self.minheap[parent]:
self.minheap[parent],self.minheap[index] = self.minheap[index],self.minheap[parent]
self.heap_up(parent)
def display(self):
print(self.minheap)
object = HEAP()
object.insert(5)
object.insert(7)
object.insert(9)
object.insert(0)
object.insert(1)
object.insert(5)
object.insert(12)
object.insert(11)
object.delete()
object.delete()
object.delete()
object.delete()
object.delete()
object.delete()
object.delete()
object.delete()
object.display()