forked from siddhesh10/Heap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.py
67 lines (51 loc) · 2.04 KB
/
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
60
61
62
63
64
65
66
67
#making max heap
class heap(object):
def __init__(self,l):
self.heap=[0]*len(l)
self.index=0
self.heap[0]=l[0]
self.insert(l)
def insert(self,l):
for i in l[1:]:
self.index=self.index+1
self.heap[self.index]=i
self.heapify_up(self.index)
def heapify_up(self,index):
parent_index=(index-1)//2
while (parent_index>=0) and (self.heap[index]>self.heap[parent_index]):
temp=self.heap[parent_index]
self.heap[parent_index]=self.heap[index]
self.heap[index]=temp
index=parent_index
parent_index=(index-1)//2
def show_heap(self):
print(self.heap)
def max_element(self):
print(self.heap[0])
#pop always take place from top_most element of heap(ie max element if heap)
def pop(self):
self.heap[0]=self.heap[self.index]
del self.heap[self.index]
self.index=self.index-1
print(self.index)
left_child_index=1
right_child_index=2
while(left_child_index<=self.index):
if(left_child_index<=self.index):
swap_with=0
if(right_child_index > self.index):
swap_with=left_child_index
else:
if self.heap[right_child_index]>self.heap[left_child_index] and self.heap[right_child_index]>self.heap[swap_with] :
swap_with=right_child_index
elif self.heap[right_child_index]<self.heap[left_child_index] and self.heap[left_child_index]>self.heap[swap_with] :
swap_with=left_child_index
else:
swap_with=swap_with
break
temp=self.heap[swap_with]
self.heap[swap_with]=self.heap[0]
self.heap[0]=temp
print(self.heap)
left_child_index=2*swap_with+1
right_child_index=2*swap_with+2