-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.py
82 lines (65 loc) · 2.95 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class Heap(object):
heap_size=10;
def __init__(self):
self.heap=[0]*Heap.heap_size;
self.currentposition=-1;
def insert(self,item):
if self.isfull():
print "Heap is full"
return
self.currentposition=self.currentposition+1
self.heap[self.currentposition]=item;
#print "before fixup:",self.heap
self.fixup(self.currentposition);
#print "after fixup:",self.heap
def fixup(self,index):#considering maxheap
parent_index =int((index-1)/2);
while parent_index>=0 and self.heap[parent_index] < self.heap[index]:
#if the parent is of smaller value;swap parent and child and repeat until the heap is balanced.
temp=self.heap[index]
self.heap[index]=self.heap[parent_index]
self.heap[parent_index]=temp;
index=parent_index
parent_index=int((parent_index-1)/2);#update parent_index #doubt
def heapsort(self):
for i in range(0,self.currentposition+1):
temp=self.heap[0]
print temp
self.heap[0]=self.heap[self.currentposition-i]
self.heap[self.currentposition-i]=temp
self.fixdown(0,self.currentposition-i-1)
def fixdown(self,start_ind,last_ind):
while start_ind<last_ind:
left_child=2*start_ind+1
right_child=2*start_ind+2
if left_child <=last_ind:
child_to_swap=None
if right_child >last_ind:
child_to_swap=left_child
else:
if self.heap[left_child]>self.heap[right_child]:
child_to_swap =left_child
else:
child_to_swap=right_child
if self.heap[start_ind]<self.heap[child_to_swap]:
temp=self.heap[start_ind]
self.heap[start_ind]=self.heap[child_to_swap]
self.heap[child_to_swap]=temp
else:
break
start_ind=child_to_swap
else:
break
def isfull(self):
if self.currentposition==Heap.heap_size:
return True
else:
return False
if __name__=="__main__":
heap=Heap()
heap.insert(23)
heap.insert(5)
heap.insert(100)
heap.insert(2)
heap.insert(210)
heap.heapsort()