22
33from abc import abstractmethod
44from collections .abc import Iterable
5+ from functools import total_ordering
56from typing import Protocol , TypeVar
67
78
89class Comparable (Protocol ):
9- @abstractmethod
10- def __lt__ (self : T , other : T , / ) -> bool :
11- pass
12-
1310 @abstractmethod
1411 def __gt__ (self : T , other : T , / ) -> bool :
1512 pass
1613
17- @abstractmethod
18- def __eq__ (self : T , other : object , / ) -> bool :
19- pass
20-
2114
2215T = TypeVar ("T" , bound = Comparable )
2316
2417
18+ @total_ordering
2519class Heap [T : Comparable ]:
2620 """A Max Heap Implementation
2721
@@ -54,7 +48,7 @@ def __repr__(self) -> str:
5448
5549 def parent_index (self , child_idx : int ) -> int | None :
5650 """
57- returns the parent index based on the given child index
51+ Returns the parent index based on the given child index
5852
5953 >>> h = Heap()
6054 >>> h.build_max_heap([103, 9, 1, 7, 11, 15, 25, 201, 209, 107, 5])
@@ -90,8 +84,8 @@ def parent_index(self, child_idx: int) -> int | None:
9084
9185 def left_child_idx (self , parent_idx : int ) -> int | None :
9286 """
93- return the left child index if the left child exists.
94- if not, return None.
87+ Return the left child index if the left child exists.
88+ If not, return None.
9589 """
9690 left_child_index = 2 * parent_idx + 1
9791 if left_child_index < self .heap_size :
@@ -100,8 +94,8 @@ def left_child_idx(self, parent_idx: int) -> int | None:
10094
10195 def right_child_idx (self , parent_idx : int ) -> int | None :
10296 """
103- return the right child index if the right child exists.
104- if not, return None.
97+ Return the right child index if the right child exists.
98+ If not, return None.
10599 """
106100 right_child_index = 2 * parent_idx + 2
107101 if right_child_index < self .heap_size :
@@ -110,10 +104,10 @@ def right_child_idx(self, parent_idx: int) -> int | None:
110104
111105 def max_heapify (self , index : int ) -> None :
112106 """
113- correct a single violation of the heap property in a subtree's root.
107+ Correct a single violation of the heap property in a subtree's root.
114108
115109 It is the function that is responsible for restoring the property
116- of Max heap i.e the maximum element is always at top.
110+ of a max heap, i.e the maximum element is always at the top.
117111 """
118112 if index < self .heap_size :
119113 violation : int = index
@@ -133,7 +127,7 @@ def max_heapify(self, index: int) -> None:
133127
134128 def build_max_heap (self , collection : Iterable [T ]) -> None :
135129 """
136- build max heap from an unsorted array
130+ Build a max heap from an unsorted array
137131
138132 >>> h = Heap()
139133 >>> h.build_max_heap([20,40,50,20,10])
@@ -164,7 +158,7 @@ def build_max_heap(self, collection: Iterable[T]) -> None:
164158
165159 def extract_max (self ) -> T :
166160 """
167- get and remove max from heap
161+ Get and remove max from heap
168162
169163 >>> h = Heap()
170164 >>> h.build_max_heap([20,40,50,20,10])
@@ -195,7 +189,7 @@ def extract_max(self) -> T:
195189
196190 def insert (self , value : T ) -> None :
197191 """
198- insert a new value into the max heap
192+ Insert a new value into the max heap
199193
200194 >>> h = Heap()
201195 >>> h.insert(10)
@@ -241,7 +235,6 @@ def heap_sort(self) -> None:
241235if __name__ == "__main__" :
242236 import doctest
243237
244- # run doc test
245238 doctest .testmod ()
246239
247240 # demo
0 commit comments