-
Notifications
You must be signed in to change notification settings - Fork 0
/
hand_heap.py
53 lines (51 loc) · 1.48 KB
/
hand_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
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 25 16:49:29 2020
@author: guguoqiang
"""
class heap():
def __init__(self):
self.temp=[]
def get_parent_index(self,index):
if index==0:
return 0
else:
return (index-1)//2
def swap(self,a,b):
self.temp[a],self.temp[b]=self.temp[b],self.temp[a]
def insert(self,num):
self.temp.append(num)
index=len(self.temp)-1
parent_index=self.get_parent_index(index)
while self.temp[index]>self.temp[parent_index]:
self.swap(index,parent_index)
index=parent_index
parent_index=self.get_parent_index(index)
def remove(self):
t=self.temp[0]
self.temp[0]=self.temp[-1]
self.temp.pop()
total_index=len(self.temp)-1
index=0
maxindex=0
while True:
maxindex=index
maxindex1=index
maxindex2=index
if 2*index+1<=total_index and self.temp[2*index+1]>self.temp[index]:
maxindex1=2*index+1
if 2*index+2<=total_index and self.temp[2*index+2]>self.temp[index]:
maxindex2=2*index+2
maxindex=max(maxindex1,maxindex2)
if index==maxindex:
break
self.swap(index,maxindex)
index=maxindex
return t
h=heap()
h.insert(3)
h.insert(5)
h.insert(1)
h.remove()
for i in h.temp:
print(i)