forked from Neterukun1993/purely-functional-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leftist_heap.py
63 lines (52 loc) · 1.98 KB
/
leftist_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
from __future__ import annotations
from typing import TypeVar, Generic, Optional
from src.basic.meta_singleton import MetaSingleton # type: ignore
from src.basic.comparable import Comparable # type: ignore
CT = TypeVar('CT', bound=Comparable)
class LeftistHeap(Generic[CT], metaclass=MetaSingleton):
rank: int
value: CT
a: LeftistHeap[CT]
b: LeftistHeap[CT]
def __init__(self, rank: int = 0, value: Optional[CT] = None,
a: Optional[LeftistHeap[CT]] = None,
b: Optional[LeftistHeap[CT]] = None) -> None:
self.rank = rank
if value is not None:
self.value = value
if a is not None:
self.a = a
if b is not None:
self.b = b
def __bool__(self) -> bool:
return self is not LeftistHeap()
@staticmethod
def _make(value: CT, a: LeftistHeap[CT],
b: LeftistHeap[CT]) -> LeftistHeap[CT]:
if a.rank >= b.rank:
a, b = b, a
return LeftistHeap(a.rank + 1, value, b, a)
@staticmethod
def merge(hl: LeftistHeap[CT], hr: LeftistHeap[CT]) -> LeftistHeap[CT]:
if not hl:
return hr
elif not hr:
return hl
elif hl.value <= hr.value:
return LeftistHeap._make(hl.value, hl.a,
LeftistHeap.merge(hl.b, hr))
else:
return LeftistHeap._make(hr.value, hr.a,
LeftistHeap.merge(hl, hr.b))
def insert(self, value: CT) -> LeftistHeap[CT]:
new: LeftistHeap[CT] = LeftistHeap(1, value,
LeftistHeap(), LeftistHeap())
return LeftistHeap.merge(new, self)
def find_min(self) -> CT:
if not self:
raise IndexError("find from empty heap")
return self.value
def delete_min(self) -> LeftistHeap[CT]:
if not self:
raise IndexError("delete from empty heap")
return self.merge(self.a, self.b)