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
14
votes
6 answers

Time complexity to get min elements from max-heap

I was asked in an interview: What is the best time complexity in getting the min element(s) from a max-heap? I replied as O(1) assuming the heap size is known and the heap is implemented as a binary heap using an array. This way as per my…
hytriutucx
  • 1,614
  • 6
  • 26
  • 37
13
votes
2 answers

Why is using a key function so much slower?

There is a drastic performance hit when using a keyfunc in heapq.nlargest: >>> from random import random >>> from heapq import nlargest >>> data = [random() for _ in range(1234567)] >>> %timeit nlargest(10, data) 30.2 ms ± 1.19 ms per loop (mean ±…
wim
  • 338,267
  • 99
  • 616
  • 750
13
votes
2 answers

how to get the max heap in python

I use heapq module in python, I find I only can the min heap, even if I use reverse=True I still get the min top heap from heapq import * h=[] merge(h,key=lambda e:e[0],reverse=True) heappush(h, (200, 1)) heappush(h, (300,2)) heappush(h,…
user504909
  • 9,119
  • 12
  • 60
  • 109
13
votes
1 answer

Define heap key for an array of tuples

A simple example for the usage of the python heap implementation is from heapq import heappush, heappop heap = [] data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0] for item in data: heappush(heap, item) In a more complicated scenario, I have an array of…
Oblomov
  • 8,953
  • 22
  • 60
  • 106
13
votes
2 answers

When building heap, is heap unique?

I'm studying heap & heap sorting. There is a array : arr[8] = {6,9,3,1,8,7,2,11} When I'm trying to build the heap, using code and pencil, I faced two kinds of heap. When using code, MaxHeap : 11 9 7 6 8 3 2 1 When using insertion theory, MaxHeap…
user3595632
  • 5,380
  • 10
  • 55
  • 111
12
votes
4 answers

Is there a heap class in C++ that supports changing the priority of elements other than the head?

I have a priority queue of events, but sometimes the event priorities change, so I'd like to maintain iterators from the event requesters into the heap. If the priority changes, I'd like the heap to be adjusted in log(n) time. I will always have…
Neil G
  • 32,138
  • 39
  • 156
  • 257
12
votes
1 answer

Max heap vs Min heap for kth smallest element

I am having trouble understanding why solutions for finding the kth smallest element uses a Max heap approach. And for the kth largest element a min heap approach. Wouldn't it make more sense to use min heap to find kth smallest element, since the…
user7722505
12
votes
2 answers

What is the use of the Heap data structure?

I'm working on some homework involving Heaps, and I understand how they are structured. A heap must have each node satisfying the heap property, the max-heap property is that for every node i other then the root, Heap[Parent(i)] >= Heap[i] So…
jb.
  • 9,921
  • 12
  • 54
  • 90
12
votes
1 answer

Creating a heap with heapify vs heappush. Which one is faster?

Question I have to create a priority queue storing distances. To build the heap I am thinking about the following two possibilities: from heapq import heapify, heappush n = 35000 # input size # way A: using heapify dist = [] for i in range(n): …
Daniel Reina
  • 5,764
  • 1
  • 37
  • 50
12
votes
3 answers

Comparators in std::priority_queue

Is there a reason that the std::priority_queue's constructor accepts a comparator by constant reference? What if the comparator goes out of scope? I was thinking about this in the context of possibly moving the comparator as @LightnessRacesInOrbit…
Curious
  • 20,870
  • 8
  • 61
  • 146
12
votes
2 answers

siftUp and siftDown operation in heap for heapifying an array

Assume MAX-HEAPIFY operation. where the parent element value is greater than its child values. siftDown swaps a node that is too small with its largest child (thereby moving it down) until it is at least as large as both nodes below it. siftUp…
ViX28
  • 277
  • 1
  • 2
  • 11
12
votes
2 answers

Soft heaps: what is corruption and why is it useful?

I recently read Bernard Chazelle's paper "The Soft Heap, An Approximate Priority Queue with Optimal Error Rate by Bernard Chazelle" (http://www.link.cs.cmu.edu/15859-f07/papers/chazelle-soft-heap.pdf) The paper talks a lot about "corruption." What…
kalibra
  • 139
  • 1
  • 5
12
votes
2 answers

How to update element priorities in a heap for Prim's Algorithm?

I am studying Prim's Algorithm. There is a part within the code the next vertex across the cut will be coming to the set of the vertices belonging to the MST. While doing that, we also have to 'update all vertices in the other set which are…
11
votes
3 answers

Why is element zero of a heap array not used?

Here's my rough sketch of the beginning of a heap with arbitrary values 0 1 2 3 4 5 6 7 8 9 ... [-] [10] [14] [15] [22] [21] [24] [23] [44] [30] ... Why must the element in array[0] always be set to null? Or why is it…
Ernest
  • 1,370
  • 13
  • 23
11
votes
4 answers

Can we use binary search tree to simulate heap operation?

I was wondering if we can use a binary search tree to simulate heap operations (insert, find minimum, delete minimum), i.e., use a BST for doing the same job? Are there any kind of benefits for doing so?
Junaid
  • 1,668
  • 7
  • 30
  • 51