Skip to content

Commit 0e2e6ab

Browse files
authored
Added doctest to heap.py (TheAlgorithms#11129)
* Added doctest to heap.py * Update heap.py
1 parent fb17eea commit 0e2e6ab

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

Diff for: data_structures/heap/heap.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,37 @@ def __repr__(self) -> str:
5353
return str(self.h)
5454

5555
def parent_index(self, child_idx: int) -> int | None:
56-
"""return the parent index of given child"""
56+
"""
57+
returns the parent index based on the given child index
58+
59+
>>> h = Heap()
60+
>>> h.build_max_heap([103, 9, 1, 7, 11, 15, 25, 201, 209, 107, 5])
61+
>>> h
62+
[209, 201, 25, 103, 107, 15, 1, 9, 7, 11, 5]
63+
64+
>>> h.parent_index(-1) # returns none if index is <=0
65+
66+
>>> h.parent_index(0) # returns none if index is <=0
67+
68+
>>> h.parent_index(1)
69+
0
70+
>>> h.parent_index(2)
71+
0
72+
>>> h.parent_index(3)
73+
1
74+
>>> h.parent_index(4)
75+
1
76+
>>> h.parent_index(5)
77+
2
78+
>>> h.parent_index(10.5)
79+
4.0
80+
>>> h.parent_index(209.0)
81+
104.0
82+
>>> h.parent_index("Test")
83+
Traceback (most recent call last):
84+
...
85+
TypeError: '>' not supported between instances of 'str' and 'int'
86+
"""
5787
if child_idx > 0:
5888
return (child_idx - 1) // 2
5989
return None

0 commit comments

Comments
 (0)