-
Notifications
You must be signed in to change notification settings - Fork 31
/
ErasableHeap.py
66 lines (51 loc) · 1.7 KB
/
ErasableHeap.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
# 懒删除堆/可删除堆
from heapq import heapify, heappop, heappush
from typing import Generic, Iterable, Literal, Optional, TypeVar
T = TypeVar("T")
class ErasableHeap(Generic[T]):
__slots__ = ("_data", "_erased", "_size")
def __init__(self, items: Optional[Iterable[T]] = None) -> None:
self._erased = []
self._data = [] if items is None else list(items)
if self._data:
heapify(self._data)
self._size = len(self._data)
def push(self, value: T) -> None:
heappush(self._data, value)
self._normalize()
self._size += 1
def pop(self) -> T:
value = heappop(self._data)
self._normalize()
self._size -= 1
return value
def peek(self) -> T:
return self._data[0]
def remove(self, value: T) -> None:
"""从堆中删除一个元素,要保证堆中存在该元素."""
heappush(self._erased, value)
self._normalize()
self._size -= 1
def clear(self) -> None:
self._data.clear()
self._erased.clear()
self._size = 0
def __len__(self) -> int:
return self._size
def __getitem__(self, index: Literal[0]) -> T:
return self._data[index]
def _normalize(self) -> None:
while self._data and self._erased and self._data[0] == self._erased[0]:
heappop(self._data)
heappop(self._erased)
if __name__ == "__main__":
pq = ErasableHeap((3, 5, 4, 1, 2))
print(pq.pop()) # 1
print(pq.pop()) # 2
pq.remove(3)
print(pq.pop()) # 4
pq.remove(5)
print(len(pq)) # 1
pq.push(5)
num = 0
print(pq[num])