-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.py
83 lines (65 loc) · 2.24 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
83
#
# https://wiki.python.org/moin/TimeComplexity
# https://docs.python.org/3/library/heapq.html
#
# heap [] is ordered as binary tree where parent is smaller than children
# so it wouldn't be list ordered from time to time
# binary_search_push would be O(N) due to list.insert()
#
from typing import Iterator
class Heap:
def __init__(self) -> None:
self.heap = []
def __len__(self) -> int:
return len(self.heap)
def __str__(self):
return str(self.heap)
def __iter__(self) -> Iterator:
return iter(self.heap)
def _parent(self, i: int) -> int:
"""Represent a binary tree in list format."""
return (i - 1) // 2
def _left(self, i: int) -> int:
return 2 * i + 1
def _right(self, i: int) -> int:
return 2 * i + 2
def _swap(self, i: int, j: int) -> None:
self.heap[i], self.heap[j] = self.heap[j], self.heap[i]
def _move_up(self, i: int) -> None:
"""Move the smaller up.
time complexity: O(logN)
"""
parent = self._parent(i)
while i > 0 and self.heap[i] < self.heap[parent]:
self._swap(i, parent)
i = parent
parent = self._parent(i)
def _move_down(self, i) -> None:
"""Move the bigger down.
time complexity: O(logN)
"""
min_index, left, right = i, self._left(i), self._right(i)
if left < len(self.heap) and self.heap[min_index] > self.heap[left]:
min_index = left
if right < len(self.heap) and self.heap[min_index] > self.heap[right]:
min_index = right
if i != min_index:
self._swap(i, min_index)
self._move_down(min_index)
def push(self, val) -> None:
"""Time complexity: O(logN)."""
self.heap.append(val)
self._move_up(len(self.heap) - 1)
def pop(self) -> None:
"""Time complexity: O(logN)."""
if not self.heap:
raise IndexError("not able to pop from empty heap")
self._swap(0, len(self.heap) - 1)
val = self.heap.pop()
self._move_down(0)
return val
def peek(self) -> None:
if not self.heap:
raise IndexError("heap is empty")
return self.heap[0]
# TODO: heapify()