Algoteka
Log in to submit your own samples!

Heap

Problem by oml1111
# Tech tags Title Creator Created date
1 0
2022-07-31 03:21
2 0
2022-10-31 00:50
View all samples for this language (2 verified and 0 unverified)

Using the STL priority_queue | C++ |

By oml1111 |
0 likes

A heap class from the C++ Standard Template Library providing \(O(\log n)\) insertion and \(O(\log n)\) deletion.

Code

#include <queue>

int main() {
    std::priority_queue<int> heap;
    heap.push(5);
    int max_element = heap.top();
    heap.pop();
}

Further reading

Heap Data Structures - tutorialspoint.com
Chapter 6.1: Heaps - Introduction to Algorithms, Third Edition (Thomas H. Cormen, Charles E. Leiserson, Ronald Rivest, Clifford Stein)

References

classes
std::priority_queue en.cppreference.com cplusplus.com
functions
std::priority_queue::pop en.cppreference.com cplusplus.com
std::priority_queue::push en.cppreference.com cplusplus.com
std::priority_queue::top en.cppreference.com cplusplus.com

Problem Description

Implement a max-heap data structure. Insert an element into it, then fetch the maximum element, then delete said maximum element.

View sample discussion (0 comments)
View problem discussion (0 comments)