Heap data structure is mainly used to represent a priority queue. 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 element also returns the smallest element each time.
Operations on heap :
1. heapify(iterable) :- This function is used to convert the iterable into a heap data structure. i.e. in heap order.
2. 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.
3. 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.
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 1
4. heappushpop(heap, ele) :- This function combines the functioning of both push and pop operations in one statement, increasing efficiency. Heap order is maintained after this operation.
5. heapreplace(heap, ele) :- This function also inserts and pops element in one statement, but it is different from above function. In this, element is first popped, then element is pushed.i.e, the value larger than the pushed value can be returned.
The popped item using heappushpop() is : 2 The popped item using heapreplace() is : 3
6. nlargest(k, iterable, key = fun) :- This function is used to return the k largest elements from the iterable specified and satisfying the key if mentioned.
7. nsmallest(k, iterable, key = fun) :- This function is used to return the k smallest elements from the iterable specified and satisfying the key if mentioned.
The 3 largest numbers in list are : [10, 9, 8] The 3 smallest numbers in list are : [1, 3, 4]
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Merge two sorted arrays in Python using heapq
- Python heapq to find K'th smallest element in a 2D array
- How to implement stack using priority queue or heap?
- Why is Binary Heap Preferred over BST for Priority Queue?
- heapq in Python to print all elements in sorted order from row and column wise sorted matrix
- Stack and Queue in Python using queue Module
- Priority Queue in Python
- Python Code for time Complexity plot of Heap Sort
- Heap Sort for decreasing order using min heap
- Check if a queue can be sorted into another queue using a stack
- queue::front() and queue::back() in C++ STL
- queue::empty() and queue::size() in C++ STL
- Convert min Heap to max Heap
- queue::push() and queue::pop() in C++ STL
- K-ary Heap