# 10 examples of 'min heap in python' in Python

Every line of 'min heap in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

## All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
``68def min_heapify(self, i):69    l = self.left(i)70    r = self.right(i)71    if (l &lt;= (self.heap_size - 1)) and (self[l] &lt; self[i]):72        smallest = l73    else:74        smallest = i75    if (r &lt;= (self.heap_size - 1)) and (self[r] &lt; self[smallest]):76        smallest = r77    if smallest != i:78        self[i], self[smallest] = self[smallest], self[i]79        self.min_heapify(smallest)``
``47def min_heap_sort(arr, simulation=False):48    """ Heap Sort that uses a min heap to sort an array in ascending order49        Complexity: O(n log(n))50    """51    iteration = 052    if simulation:53        print("iteration",iteration,":",*arr)54        55    for i in range(0, len(arr) - 1):56        iteration = min_heapify(arr, i, simulation, iteration)5758    return arr``
``164def downheap(self):165	tmp = 0166	i = 0167	while True:168		left = (i &lt;&lt; 1) + 1;169		right = left + 1;170		if left &gt;= self.nheap:171			return172		if right &gt;= self.nheap:173			if self.heap[i] &lt; self.heap[left]:174				tmp = self.heap[left]175				self.heap[left] = self.heap[i]176				self.heap[i] = tmp177			return178179		if self.heap[i] &gt;= self.heap[left] and self.heap[i] &gt;= self.heap[right]:180			return181182		if self.heap[left] &gt; self.heap[right]:183			tmp = self.heap[left]184			self.heap[left] = self.heap[i]185			self.heap[i] = tmp186			i = left187		else:188			tmp = self.heap[right]189			self.heap[right] = self.heap[i]190			self.heap[i] = tmp191			i = right``
``171def test_find_min_when_heap_has_random_size(self):172    a = [randint(-100, 100) for _ in range(3, 100)]173    h = MinMaxHeap(a)174    self.assertEqual(h.find_min(), min(a))``
``112def build_heap(arr, heapType):113    if heapType == "MAX_HEAP":114        new_heap = MaxHeap()115    else:116        new_heap = MinHeap()117    new_heap.size = len(arr)118    new_heap.heap = [0] + arr119    i = len(arr) // 2120    while i &gt; 0:121        new_heap.perc_down(i)122        i-=1123    return new_heap``
``51def max_heapify(ls: list, heap_size: int, i: int) -&gt; None:52    """This operation is also sometimes called `push_down`, `shift_down` or `bubble_down`.5354    Time complexity: O(log(n))."""55    m = i56    left = 2 * i + 157    right = 2 * i + 258    if left &lt; heap_size and ls[left] &gt; ls[m]:59        m = left60    if right &lt; heap_size and ls[right] &gt; ls[m]:61        m = right62    if i != m:63        ls[i], ls[m] = ls[m], ls[i]64        max_heapify(ls, heap_size, m)``
``6def test_min_heap(self):7    heap = MinHeap()8    assert_equal(heap.peek_min(), None)9    assert_equal(heap.extract_min(), None)10    heap.insert(20)11    assert_equal(heap.array[0], 20)12    heap.insert(5)13    assert_equal(heap.array[0], 5)14    assert_equal(heap.array[1], 20)15    heap.insert(15)16    assert_equal(heap.array[0], 5)17    assert_equal(heap.array[1], 20)18    assert_equal(heap.array[2], 15)19    heap.insert(22)20    assert_equal(heap.array[0], 5)21    assert_equal(heap.array[1], 20)22    assert_equal(heap.array[2], 15)23    assert_equal(heap.array[3], 22)24    heap.insert(40)25    assert_equal(heap.array[0], 5)26    assert_equal(heap.array[1], 20)27    assert_equal(heap.array[2], 15)28    assert_equal(heap.array[3], 22)29    assert_equal(heap.array[4], 40)30    heap.insert(3)31    assert_equal(heap.array[0], 3)32    assert_equal(heap.array[1], 20)33    assert_equal(heap.array[2], 5)34    assert_equal(heap.array[3], 22)35    assert_equal(heap.array[4], 40)36    assert_equal(heap.array[5], 15)37    mins = []38    while heap:39        mins.append(heap.extract_min())40    assert_equal(mins, [3, 5, 15, 20, 22, 40])41    print('Success: test_min_heap')``
``219def smallest_child(self,k):220    '''221    return the index of smallest child of K222    @precondition: 2*k &lt;= self.count (has at least 1 child)223    '''224    if 2*k == self.count or self.array[2*k] &lt; self.array[2*k+1]:225       return 2*k226    else:227        return 2*k+1``
``180def test_remove_min_when_heap_has_size_1(self):181    h = MinMaxHeap([13])182    self.assertEqual(h.remove_min(), 13)183    self.assertTrue(h.is_empty())184    self.assertEqual(h.size, 0)``
``11def heappushpop(heap: List[_T], item: _T) -&gt; _T: pass``