Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

data structures

Heap implementation in Python


Heaps help you quickly retrieve objects with the smallest or the largest key.

From this definition, we can infer that we can use heaps when we to retrieve the maximum or minimum object in constant time.

MinHeap operations

  1. Insertion O(logn)O(log n): Finding the exact position of the new element is performed in lognlog n since it is only compared with the position of the parent nodes.
  2. Delete Min O(logn)O(log n): After the minimum element is removed, the heap has to put the new root in place.
  3. Find Min O(1)O(1): This is possible because the heap data structure always has the minimum element on the root node.
  4. Heapify O(n)O(n): This operation rearranges all the nodes after deletion or insertion operation. The cost of this operation is nn since all the elements have to be moved to keep the heap properties.
  5. Delete O(logn)O(log n): A specific element from the heap can be removed in lognlog n time.

Here’s a comparison between min heap vs. max heap.

1 of 3


Min Heap Implementation in Python
class MinHeap:
    def __init__(self):
        On this implementation the heap list is initialized with a value
        self.heap_list = [0]
        self.current_size = 0
    def sift_up(self, i):
        Moves the value up in the tree to maintain the heap property.
        # While the element is not the root or the left element
        while i // 2 > 0:
            # If the element is less than its parent swap the elements
            if self.heap_list[i] < self.heap_list[i // 2]:
                self.heap_list[i], self.heap_list[i // 2] = self.heap_list[i // 2], self.heap_list[i]
            # Move the index to the parent to keep the properties
            i = i // 2
    def insert(self, k):
        Inserts a value into the heap
        # Append the element to the heap
        # Increase the size of the heap.
        self.current_size += 1
        # Move the element to its position from bottom to the top
    def sift_down(self, i):
        # if the current node has at least one child
        while (i * 2) <= self.current_size:
            # Get the index of the min child of the current node
            mc = self.min_child(i)
            # Swap the values of the current element is greater than its min child
            if self.heap_list[i] > self.heap_list[mc]:
                self.heap_list[i], self.heap_list[mc] = self.heap_list[mc], self.heap_list[i]
            i = mc
    def min_child(self, i):
        # If the current node has only one child, return the index of the unique child
        if (i * 2)+1 > self.current_size:
            return i * 2
            # Herein the current node has two children
            # Return the index of the min child according to their values
            if self.heap_list[i*2] < self.heap_list[(i*2)+1]:
                return i * 2
                return (i * 2) + 1
    def delete_min(self):
        # Equal to 1 since the heap list was initialized with a value
        if len(self.heap_list) == 1:
            return 'Empty heap'
        # Get root of the heap (The min value of the heap)
        root = self.heap_list[1]
        # Move the last value of the heap to the root
        self.heap_list[1] = self.heap_list[self.current_size]
        # Pop the last value since a copy was set on the root
        *self.heap_list, _ = self.heap_list
        # Decrease the size of the heap
        self.current_size -= 1
        # Move down the root (value at index 1) to keep the heap property
        # Return the min value of the heap
        return root
Driver program
# Same tree as above example.
my_heap = MinHeap()

print(my_heap.delete_min()) # removing min node i.e 5 



data structures

View all Courses

Keep Exploring