Questions tagged [heap]

A heap (data structure) is a tree that is ordered with respect to depth. When the question concerns process memory set aside for dynamic allocation, tag with heap-memory instead.

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: If A is a parent node of B then key(A) is ordered with respect to key(B) with the same ordering applying across the heap. Either the keys of parent nodes are always greater than or equal to those of the children and the highest key is in the root node (this kind of heap is called max heap) or the keys of parent nodes are less than or equal to those of the children (min heap).

There are several variants of this data structure, and when this is relevant for the question, add the more specific tag as well: , , , , , ,

For questions on the heap-sort algorithm, add the tag.

Don't use this tag for the following:

1845 questions
66
votes
5 answers

How to delete in a heap data structure?

I understand how to delete the root node from a max heap but is the procedure for deleting a node from the middle to remove and replace the root repeatedly until the desired node is deleted? Is O(log n) the optimal complexity for this…
tesfa koli
  • 725
  • 2
  • 8
  • 11
66
votes
2 answers

Python: delete element from heap

Python has heapq module which implements heap data structure and it supports some basic operations (push, pop). How to remove i-th element from the heap in O(log n)? Is it even possible with heapq or do I have to use another module? Note, there is…
Ecir Hana
  • 10,864
  • 13
  • 67
  • 117
62
votes
2 answers

Peeking in a heap in python

What is the official way of peeking in a python heap as created by the heapq libs? Right now I have def heappeak(heap): smallest = heappop(heap) heappush(heap, smallest) return smallest which is arguably, not very nice. Can I always assume…
Thomas
  • 2,769
  • 5
  • 21
  • 21
60
votes
4 answers

How to implement O(logn) decrease-key operation for min-heap based Priority Queue?

I am working on an application that demonstrates the Djikstra's algorithm, and to use it, I need to restore the heap property when my elements' value is decreased. The problem regarding the complexity is that when the algorithm changes the value of…
jathanasiou
  • 882
  • 2
  • 8
  • 17
58
votes
4 answers

Why in a heap implemented by array the index 0 is left unused?

I'm learning data structures and every source tells me not to use index 0 of the array while implementing heap, without giving any explanation why. I searched the web, searched StackExchange, and couldn't find an answer.
xji
  • 7,341
  • 4
  • 40
  • 61
57
votes
11 answers

What's the relationship between "a" heap and "the" heap?

A heap is a tree data structure where higher levels of the tree always contain greater (or lesser, if it's set up that way) values than lower levels. "The" heap is a bunch of free RAM that a program has available for dynamic allocation. They're…
Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
52
votes
7 answers

How do I find the median of numbers in linear time using heaps?

Wikipedia says: Selection algorithms: Finding the min, max, both the min and max, median, or even the k-th largest element can be done in linear time using heaps. All it says is that it can be done, and not how. Can you give me some start on how…
Lazer
  • 90,700
  • 113
  • 281
  • 364
49
votes
6 answers

When should I use make_heap vs. Priority Queue?

I have a vector that I want to use to create a heap. I'm not sure if I should use the C++ make_heap function or put my vector in a priority queue? Which is better in terms of performance? When should I use one vs. the other?
rolloff
  • 543
  • 1
  • 4
  • 5
46
votes
10 answers

Is there a PriorityQueue implementation with fixed capacity and custom comparator?

Related questions: Java PriorityQueue with fixed size How do I use a PriorityQueue? get indexes of n smallest elements in an array Scala: Is there a way to use PriorityQueue like I would in Java? I have a very large data set (more than 5 millions…
ffriend
  • 27,562
  • 13
  • 91
  • 132
46
votes
7 answers

How can I implement decrease-key functionality in Python's heapq?

I know it is possible to realize decrease-key functionality in O(log n) but I don't know how?
zerodx
41
votes
7 answers

PriorityQueue/Heap Update

Does Java have an easy way to reevaluate a heap once the priority of an object in a PriorityQueue has changed? I can't find any sign of it in Javadoc, but there has to be a way to do it somehow, right? I'm currently removing the object then…
kevmo314
  • 4,223
  • 4
  • 32
  • 43
41
votes
4 answers

What is the time complexity of heapq.nlargest?

I was looking at this pycon talk, 34:30 and the speaker says that getting the t largest elements of a list of n elements can be done in O(t + n). How is that possible? My understanding is that creating the heap will be O(n), but what's the…
foo
  • 940
  • 2
  • 9
  • 20
40
votes
8 answers

Search an element in a heap

I remembered that heap can be used to search whether an element is in it or not with O(logN) time complexity. But suddenly I can't get the details. I can only find getmin delete add and so on. Can anybody give a hint?
skydoor
  • 25,218
  • 52
  • 147
  • 201
39
votes
3 answers

Is there a standard Java implementation of a Fibonacci heap?

I was looking at the different kind of heap data structures. The Fibonacci heap seems to have the better worst case complexity for (1) insertion, (2) deletion and (2) finding the minimum element. I have found that in Java there is a class…
Phil
  • 3,375
  • 3
  • 30
  • 46
38
votes
4 answers

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

I am wondering why for creating a min heap using the priority_queue, the std::greater should be used? std::priority_queue, std::greater > min_heap; To me, since the smallest value is always located at the top of the heap, the…
Vahid Noormofidi
  • 748
  • 10
  • 17