please explain is not better to use set over heap because the operations with the set also take o(log n) time and we get the unique elements and in sorted fashion .so please tell me benefits of heap over set.
Time complexity comparison between set and heap?
Hey @namangarg31
A priority queue only gives you access to one element in sorted order – i.e., you can get the highest priority item, and when you remove that, you can get the next highest priority, and so on. A priority queue also allows duplicate elements, so it’s more like a multiset than a set. Also, building a heap is also linear on the number of items in the heap, where building a set is O(N log N) .
Memory overhead for std::set
is much bigger also because it has to store many pointers between its nodes.
Also check this:https://www.geeksforgeeks.org/why-is-binary-heap-preferred-over-bst-for-priority-queue/