Answers for "Min Heap Python"

1

max heap python

import heapq

Since the built in heapq library is a minheap, multiply your values by -1
and it will function as a max heap. Just remeber that all your numbers 
have been inverted.
Posted by: Guest on August-19-2021
2

python code for heap using heapify

#Implementing Heap Using Heapify Method in Python 3
#MaxHeapify,MinHeapify,Ascending_Heapsort,Descending_Heapsort
class heap:
    
    def maxheapify(self,array):
        n=len(array)
        for i in range(n//2-1,-1,-1):
            self._maxheapify(array,n,i)
            
            
    def _maxheapify(self,array,n,i):
        l=2*i+1
        r=2*i+2
        if l<n and array[l]>array[i]:
            largest=l
        else:
            largest=i
        if r<n and array[r]>array[largest]:
            largest=r
        if (largest!=i):
            array[largest],array[i]=array[i],array[largest]
            self._maxheapify(array,n,largest)
            
            
    def minheapify(self,array):
        n = len(array)
        for i in range(n//2-1,-1,-1):
            self._minheapify(array,n,i)
            
            
    def _minheapify(self,array,n,i):
        l=2*i+1
        r=2*i+2
        if l<n and array[l]<array[i]:
            smallest = l
        else:
            smallest = i
        if r < n and array[r]<array[smallest]:
            smallest = r
        if (smallest != i):
            array[smallest], array[i] = array[i], array[smallest]
            self._minheapify(array, n, smallest)
            
            
    def descending_heapsort(self,array):
        n = len(array)
        for i in range(n // 2 - 1, -1, -1):
            self._minheapify(array, n, i)
        for i in range(n - 1, 0, -1):
            array[0], array[i] = array[i], array[0]
            self._minheapify(array, i, 0)


    def ascending_heapsort(self,array):
        n=len(array)
        for i in range(n//2-1,-1,-1):
            self._maxheapify(array,n,i)
        for i in range(n-1,0,-1):
            array[0],array[i]=array[i],array[0]
            self._maxheapify(array,i,0)

b=[550,4520,3,2340,12]
a=heap()

a.maxheapify(b)
print('Max Heapify -->',b)

a.minheapify(b)
print('Min Heapify -->',b)

a.ascending_heapsort(b)
print('Ascending Heap Sort -->',b)

a.descending_heapsort(b)
print('Descending Heap Sort -->',b)
Posted by: Guest on January-15-2021
0

Min Heap Python

def min_heapify(A,k):
    l = left(k)
    r = right(k)
    if l < len(A) and A[l] < A[k]:
        smallest = l
    else:
        smallest = k
    if r < len(A) and A[r] < A[smallest]:
        smallest = r
    if smallest != k:
        A[k], A[smallest] = A[smallest], A[k]
        min_heapify(A, smallest)

def left(k):
    return 2 * k + 1

def right(k):
    return 2 * k + 2

def build_min_heap(A):
    n = int((len(A)//2)-1)
    for k in range(n, -1, -1):
        min_heapify(A,k)

A = [3,9,2,1,4,5]
build_min_heap(A)
print(A)
Posted by: Guest on July-09-2021
0

Max Heap Python

def max_heapify(A,k):
    l = left(k)
    r = right(k)
    if l < len(A) and A[l] > A[k]:
        largest = l
    else:
        largest = k
    if r < len(A) and A[r] > A[largest]:
        largest = r
    if largest != k:
        A[k], A[largest] = A[largest], A[k]
        max_heapify(A, largest)

def left(k):
    return 2 * k + 1

def right(i):
    return 2 * k + 2

def build_max_heap(A):
    n = int((len(A)//2)-1)
    for k in range(n, -1, -1):
        max_heapify(A,k)

A = [3,9,2,1,4,5]
build_max_heap(A)
print(A)
Posted by: Guest on July-09-2021
0

Heap in python

Heap Implementation at this link:

https://github.com/shreyasvedpathak/Data-Structure-Python/tree/master/Hashing
Posted by: Guest on March-29-2021

Python Answers by Framework

Browse Popular Code Answers by Language