-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeapPriorityQueue.py
68 lines (59 loc) · 1.89 KB
/
HeapPriorityQueue.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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File : HeapPriorityQueue.py
@Time : 2024/09/30 10:22:52
@Author : Zihao Zheng
@Email : [email protected]
'''
class PriorityQueueBase():
class _item():
__slots__='_key','_value'
def __init__(self,k,v):
self._key=k
self._value=v
def __lt__(self,other):#比较器
return self._key < other._key
def is_empty(self):
return len(self)==0
class HeapPriorityQueue(PriorityQueueBase):
def __init__(self):
self._data=[]
def _parent(self,j):
return (j-1)//2
def _left(self,j):
return (2*j)+1
def _right(self,j):
return (2*j)+2
def _has_left(self,j):
return self._left(j)<len(self._data)
def _has_right(self,j):
return self._right(j)<len(self._data)
def _swap(self,i,j):
self._data[i],self._data[j]=self._data[j],self._data[i]
def _upheap(self,j):
parent=self._parent(j)
if j>0 and self._data[j]<self._data[parent]:
self._swap(j,parent)
self._upheap(parent)
def _downheap(self,j):
if self._has_left(j):
left=self._left(j)
small_child=left
if self._has_right(j):
right=self._right
if self._data[right]<self._data[left]:
small_child=right
self._swap(small_child,j)
self._downheap(small_child)
def add(self,key,value):
self._data.append(self._item(key,value))
self._upheap(len(self._data)-1)
def remove_min(self):
if self.is_empty():
return None
else:
self._swap(0,len(self._data)-1)
item=self._data.pop()
self._downheap(0)
return(item._key,item._value)