Added doctest to randomized_heap.py (#11151)

This commit is contained in:
Suyash Dongre 2023-11-12 07:43:04 +05:30 committed by GitHub
parent a13e9c2137
commit 8b7352626e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,14 +22,40 @@ class RandomizedHeapNode(Generic[T]):
@property @property
def value(self) -> T: def value(self) -> T:
"""Return the value of the node.""" """
Return the value of the node.
>>> rhn = RandomizedHeapNode(10)
>>> rhn.value
10
>>> rhn = RandomizedHeapNode(-10)
>>> rhn.value
-10
"""
return self._value return self._value
@staticmethod @staticmethod
def merge( def merge(
root1: RandomizedHeapNode[T] | None, root2: RandomizedHeapNode[T] | None root1: RandomizedHeapNode[T] | None, root2: RandomizedHeapNode[T] | None
) -> RandomizedHeapNode[T] | None: ) -> RandomizedHeapNode[T] | None:
"""Merge 2 nodes together.""" """
Merge 2 nodes together.
>>> rhn1 = RandomizedHeapNode(10)
>>> rhn2 = RandomizedHeapNode(20)
>>> RandomizedHeapNode.merge(rhn1, rhn2).value
10
>>> rhn1 = RandomizedHeapNode(20)
>>> rhn2 = RandomizedHeapNode(10)
>>> RandomizedHeapNode.merge(rhn1, rhn2).value
10
>>> rhn1 = RandomizedHeapNode(5)
>>> rhn2 = RandomizedHeapNode(0)
>>> RandomizedHeapNode.merge(rhn1, rhn2).value
0
"""
if not root1: if not root1:
return root2 return root2