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 ]]
@@ -75,12 +104,32 @@ def __next__(self) -> tuple[K, V]:
75104 def __len__ (self ) -> int :
76105 return len (self ._heap )
77106
107+ def is_empty (self ) -> bool :
108+ """
109+ Check if the heap is empty.
110+
111+ :return: True if the heap is empty, False otherwise.
112+ """
113+ return len (self ._heap ) == 0
114+
78115 def top (self ) -> Optional [tuple [K , V ]]:
116+ """
117+ Returns the maximum element in the heap without removing it.
118+
119+ :return: A tuple containing the key and value of the maximum element,
120+ or None if the heap is empty.
121+ """
79122 if not self ._heap :
80123 return None
81124 return self ._heap [0 ].get ()
82125
83126 def pop (self ) -> Optional [tuple [K , V ]]:
127+ """
128+ Removes and returns the maximum element from the heap.
129+
130+ :return: A tuple containing the key and value of the maximum element,
131+ or None if the heap is empty.
132+ """
84133 if not self ._heap :
85134 return None
86135 max_val = self ._heap [0 ]
@@ -89,8 +138,14 @@ def pop(self) -> Optional[tuple[K, V]]:
89138 self ._bubble_down ()
90139 return max_val .get ()
91140
92- def add (self , key : K , val : V ) -> None :
93- self ._heap .append (_HeapNode (key , val ))
141+ def add (self , key : K , value : V ) -> None :
142+ """
143+ Adds a new key-value pair to the heap.
144+
145+ :param key: The key used for comparison.
146+ :param value: The value associated with the key.
147+ """
148+ self ._heap .append (_HeapNode (key , value ))
94149 self ._bubble_up ()
95150
96151 def _get_local_max (self , i : int ) -> int :
0 commit comments