• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
June 29, 2022 |48.2K Views

Heap in Python

  Share   Like
Description
Discussion

In Python, it is available using “heapq” module. The property of this data structure in Python is that each time the smallest of heap element is popped(min heap). 

Whenever elements are pushed or popped, heap structure in maintained. The heap[0] element also returns the smallest element each time. Let’s see various Operations on heap :

heapify(iterable) :- This function is used to convert the iterable into a heap data structure. i.e. in heap order.


heappush(heap, ele) :- This function is used to insert the element mentioned in its arguments into heap. The order is adjusted, so as heap structure is maintained.


heappop(heap) :- This function is used to remove and return the smallest element from heap. The order is adjusted, so as heap structure is maintained.

Heap in Python : https://www.geeksforgeeks.org/heap-queue-or-heapq-in-python/