MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / pop

Method pop

data_structures/heap/randomized_heap.py:125–151  ·  view source on GitHub ↗

Pop the smallest value from the heap and return it. >>> rh = RandomizedHeap([3, 1, 3, 7]) >>> rh.pop() 1 >>> rh.pop() 3 >>> rh.pop() 3 >>> rh.pop() 7 >>> rh.pop() Traceback (most recent call last):

(self)

Source from the content-addressed store, hash-verified

123 self._root = RandomizedHeapNode.merge(self._root, RandomizedHeapNode(value))
124
125 def pop(self) -> T | None:
126 """
127 Pop the smallest value from the heap and return it.
128
129 >>> rh = RandomizedHeap([3, 1, 3, 7])
130 >>> rh.pop()
131 1
132 >>> rh.pop()
133 3
134 >>> rh.pop()
135 3
136 >>> rh.pop()
137 7
138 >>> rh.pop()
139 Traceback (most recent call last):
140 ...
141 IndexError: Can't get top element for the empty heap.
142 """
143
144 result = self.top()
145
146 if self._root is None:
147 return None
148
149 self._root = RandomizedHeapNode.merge(self._root.left, self._root.right)
150
151 return result
152
153 def top(self) -> T:
154 """

Callers 1

to_sorted_listMethod · 0.95

Calls 2

topMethod · 0.95
mergeMethod · 0.45

Tested by

no test coverage detected