1+ """
2+ This module implements a max-heap data structure that allows for efficient
3+ retrieval and removal of the maximum element. The heap supports adding
4+ elements, retrieving the maximum element, and removing the maximum element
5+ while maintaining the heap property.
6+ """
7+
18from __future__ import annotations
29
310from typing import Generic , Iterator , Optional , Protocol , TypeVar
@@ -16,6 +23,9 @@ def _parent(i: int) -> int:
1623
1724
1825class Comparable (Protocol ):
26+ """
27+ Protocol for comparison methods required for a key in the heap.
28+ """
1929
2030 def __lt__ (self : K , other : K ) -> bool : ...
2131
@@ -32,6 +42,14 @@ def __ge__(self: K, other: K) -> bool: ...
3242
3343
3444class _HeapNode (Generic [K , V ]):
45+ """
46+ A node in the heap that holds a key-value pair.
47+
48+ The key is used for comparison, and the value is stored alongside it.
49+
50+ :param key: The key used for comparison.
51+ :param value: The value associated with the key.
52+ """
3553
3654 _key : K
3755 _value : V
@@ -41,6 +59,11 @@ def __init__(self, key: K, value: V) -> None:
4159 self ._value = value
4260
4361 def get (self ) -> tuple [K , V ]:
62+ """
63+ Returns the key-value pair stored in the node.
64+
65+ :return: A tuple containing the key and value.
66+ """
4467 return self ._key , self ._value
4568
4669 def __lt__ (self , other : _HeapNode [K , V ]) -> bool :
@@ -57,6 +80,12 @@ def __ge__(self, other: _HeapNode[K, V]) -> bool:
5780
5881
5982class MaxHeap (Generic [K , V ]):
83+ """
84+ A max-heap implementation that allows for efficient retrieval of the
85+ maximum element. This heap supports adding elements, retrieving the maximum
86+ element, and removing the maximum element while maintaining the heap
87+ property.
88+ """
6089
6190 _heap : list [_HeapNode [K , V ]]
6291 _iter : Iterator [_HeapNode [K , V ]]
@@ -76,11 +105,23 @@ def __len__(self) -> int:
76105 return len (self ._heap )
77106
78107 def top (self ) -> Optional [tuple [K , V ]]:
108+ """
109+ Returns the maximum element in the heap without removing it.
110+
111+ :return: A tuple containing the key and value of the maximum element,
112+ or None if the heap is empty.
113+ """
79114 if not self ._heap :
80115 return None
81116 return self ._heap [0 ].get ()
82117
83118 def pop (self ) -> Optional [tuple [K , V ]]:
119+ """
120+ Removes and returns the maximum element from the heap.
121+
122+ :return: A tuple containing the key and value of the maximum element,
123+ or None if the heap is empty.
124+ """
84125 if not self ._heap :
85126 return None
86127 max_val = self ._heap [0 ]
@@ -89,8 +130,14 @@ def pop(self) -> Optional[tuple[K, V]]:
89130 self ._bubble_down ()
90131 return max_val .get ()
91132
92- def add (self , key : K , val : V ) -> None :
93- self ._heap .append (_HeapNode (key , val ))
133+ def add (self , key : K , value : V ) -> None :
134+ """
135+ Adds a new key-value pair to the heap.
136+
137+ :param key: The key used for comparison.
138+ :param value: The value associated with the key.
139+ """
140+ self ._heap .append (_HeapNode (key , value ))
94141 self ._bubble_up ()
95142
96143 def _get_local_max (self , i : int ) -> int :
0 commit comments