Heap data structure can be implemented in a range using STL which allows faster input into heap and retrieval of a number always results in the largest number i.e. largest number of the remaining numbers is popped out each time. Other numbers of the heap are arranged depending upon the implementation.
Operations on heap :
1. make_heap() :- This function is used to convert a range in a container to a heap.
2. front() :- This function displays the first element of heap which is the maximum number.
The maximum element of heap is : 40
3. push_heap() :- This function is used to insert elements into heap. The size of the heap is increased by 1. New element is placed appropriately in the heap.
4. pop_heap() :- This function is used to delete the maximum element of the heap. The size of heap is decreased by 1. The heap elements are reorganised accordingly after this operation.
The maximum element of heap is : 40 The maximum element of heap after push is : 50 The maximum element of heap after pop is : 40
5. sort_heap() :- This function is used to sort the heap. After this operation, the container is no longer a heap.
The heap elements are : 40 30 20 25 15 The heap elements after sorting are : 15 20 25 30 40
6. is_heap() :- This function is used to check whether the container is heap or not. Generally, in most implementations, the reverse sorted container is considered as heap. Returns true if container is heap else returns false.
6. is_heap_until() :- This function returns the iterator to the position till the container is the heap. Generally, in most implementations, the reverse sorted container is considered as heap.
The container is not heap The heap elements in container are : 40 30 25
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.
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- Difference between Binary Heap, Binomial Heap and Fibonacci Heap
- Convert min Heap to max Heap
- Heap Sort for decreasing order using min heap
- Difference between Min Heap and Max Heap
- How to implement Min Heap using STL?
- Applications of Heap Data Structure
- Tournament Tree (Winner Tree) and Binary Heap
- Time Complexity of building a heap
- Why is Binary Heap Preferred over BST for Priority Queue?
- Fibonacci Heap | Set 1 (Introduction)
- Check if a given Binary Tree is Heap
- K-ary Heap
- Implementation of Binomial Heap
- Where is Heap Sort used practically?
- Binary Heap
- Array Representation Of Binary Heap
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- How to implement stack using priority queue or heap?
- Convert BST to Min Heap
- Leftist Tree / Leftist Heap
Improved By : SamirKhan