Binary Heap
A Binary Heap is a Complete Binary Tree. A binary heap is typically represented as an array.
def max_heapify(arr, i, N):
left_child = 2*i+1
right_child = 2*i+2
# Find the largest of left_child, right_child and parent which is i.
largest = left_child if left_child < N and arr[left_child] > arr[i] else i
largest = right_child if right_child < N and arr[right_child] > arr[largest]
# If Parent is not largest, swap and apply max_heapify on child to propagate it down
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
max_heapify(arr, largest, N)
def build_max_heap(arr):
n = len(arr)
for i in range(n//2, -1, -1):
max_heapify(arr, i, n)
return arr
import sys
from heapq import heappush, heappop, heapify
# Heap functions from python library
# • heappop - pop and return the smallest element from heap
# • heappush - push the value item onto the heap, maintaining heap invarient
# • heapify - transform list into heap, in place, in linear time
MIN = -sys.maxsize-1
class MinHeap:
def __init__(self):
self.heap = []
def parent(self, i):
return int((i-1)/2)
# Inserts a new key 'k'
def insertKey(self, k):
heappush(self.heap, k)
# Decrease value of key at index 'i' to new_val
# It is assumed that new_val is smaller than heap[i]
def decreaseKey(self, i, new_val):
self.heap[i] = new_val
while(i != 0 and self.heap[self.parent(i)] > self.heap[i]):
# Swap heap[i] with heap[parent(i)]
self.heap[i] , self.heap[self.parent(i)] = self.heap[self.parent(i)], self.heap[i]
# Method to remove minium element from min heap
def extractMin(self):
return heappop(self.heap)
# This functon deletes key at index i. It first reduces
# value to minus infinite and then calls extractMin()
def deleteKey(self, i):
self.decreaseKey(i, MIN)
self.extractMin()
# Get the minimum element from the heap
def getMin(self):
return self.heap[0]
print("Example:- Array Implementation of Heap")
print("InsertKey: 3, 2 then DeleteKey: 1 and then InsertKey: 15, 5, 4, 45")
my_heap = MinHeap()
my_heap.insertKey(3)
my_heap.insertKey(2)
my_heap.deleteKey(1)
my_heap.insertKey(15)
my_heap.insertKey(5)
my_heap.insertKey(4)
my_heap.insertKey(45)
print("Extracted Min = {}".format(my_heap.extractMin()))
print("Get Min = {}".format(my_heap.getMin()))
my_heap.decreaseKey(2, 1)
print("After Decreasing Key of 2 to 1, Now Get Min = {}".format(my_heap.getMin()))
Output:
Given an array of elements find k largest or smallest elements of it.
Examples:
Input: [1, 23, 12, 9, 30, 2, 50] and k=3
Output: 50, 30, 23
from heapq import heapify, nlargest, nsmallest
def find_k_largest(arr, k):
heapify(arr)
print("The {} largest numbers in list are : {}".format(k, nlargest(k, arr)))
def find_k_smallest(arr, k):
heapify(arr)
print("The {} smallest numbers in list are : {}".format(k, nsmallest(k, arr)))
print("Example-1: find_k_largest([1, 23, 12, 9, 30, 2, 50], 3)")
find_k_largest([1, 23, 12, 9, 30, 2, 50], 3)
print("\nExample-2: find_k_smallest([1, 23, 12, 9, 30, 2, 50], 2)")
find_k_smallest([1, 23, 12, 9, 30, 2, 50], 2)
Output:
← Previous: Binary Serach Tree Next: Hash →