What’s the relationship between “a” heap and “the” heap?

Nothing much, to be honest. I would imagine that the word heap was simply taken with it’s everday (non-technical) usage and applied to these two concepts individually as reasonably good analogies. In the first case (tree data structure meaning), the description heap is most appropiate because “greater” objects are placed higher up in the tree … Read more

O(klogk) time algorithm to find kth smallest element from a binary heap

Well, your intuition was right that we need extra data structure to achieve O(klogk) because if we simply perform operations on the original heap, the term logn will remain in the resulting complexity. Guessing from the targeted complexity O(klogk), I feel like creating and maintaining a heap of size k to help me achieve the … Read more

The reason of using `std::greater` for creating min heap via `priority_queue`

The logical argument is as follows std::priority_queue is a container adaptor; basic memory considerations make the back the preferred place for modifications (with pop_back() and push_back()) for sequence containers such as std::vector. the priority_queue primitives are based on std::make_heap (constructor), std::pop_heap + container::pop_back (priority_queue::pop) and on container::push_back + std::push_heap (priority_queue::push) pop_heap will take the front … Read more