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
16
votes
3 answers

C++ overloading array operator

I'm creating a Heap, like this: struct Heap { int H[100]; int operator [] (int i) { return H[i]; } //... }; When I try to print elements from it I do like this: Heap h; // add some elements... printf("%d\n", h[3]); // instead of…
Daniel
  • 7,357
  • 7
  • 32
  • 84
16
votes
4 answers

Kth largest element in a max-heap

I'm trying to come up with something to solve the following: Given a max-heap represented as an array, return the kth largest element without modifying the heap. I was asked to do it in linear time, but was told it can be done in log time. I…
Alstor
  • 161
  • 1
  • 1
  • 4
16
votes
2 answers

Time complexity of inserting in to a heap

I am trying to mostly understand the reasoning behind the Big O and Omega of inserting a new element in a heap. I know I can find answers online but I really like having a thorough understanding rather than just finding answers online and just…
user1010101
  • 2,062
  • 7
  • 47
  • 76
16
votes
7 answers

Why does Java uses heap for memory allocation?

I just read this statement in a java book saying Objects in java reside on a heap. Is a heap used because it is the best way to store data and retrieve data fast ? I only have an idea about data structures being a beginner. I mean why not stack or…
Serenity
  • 4,968
  • 19
  • 65
  • 104
16
votes
3 answers

Can min/max of moving window achieve in O(N)?

I have input array A A[0], A[1], ... , A[N-1] I want function Max(T,A) which return B represent max value on A over previous moving window of size T where B[i+T] = Max(A[i], A[i+T]) By using max heap to keep track of max value on current moving…
ipoppo
  • 369
  • 2
  • 11
16
votes
5 answers

Print the biggest K elements in a given heap in O(K*log(K))?

Given the following problem , I'm not completely sure with my current solution : Question : Given a maximum heap with n elements , which is stored in an array A , is it possible to print all the biggest K elements in O(K*log(K)) ? My answer…
JAN
  • 21,236
  • 66
  • 181
  • 318
15
votes
2 answers

How can I configure std::priority_queue to ignore duplicates?

How can I configure std::priority_queue to ignore duplicates? When I add a key that is already contained then this new one should be ignored. (In my case, the priority for the old and the new one will always be exactly the same.) Complexity-wise it…
Frank
  • 64,140
  • 93
  • 237
  • 324
15
votes
4 answers

Keeping track of the median of an expanding array

Interview Question: Edited Below You are given an array. You make 2 heaps out of it, one minheap and the other max heap. Now find the median of the array using these 2 provided heaps in O(nlog n) time. Corrected Question Numbers are randomly…
EFreak
  • 3,119
  • 3
  • 27
  • 31
15
votes
5 answers

Is Heap considered an Abstract Data Type?

I'm taking Data-Structure course and got a little confused about what is considered to be an ADT (Abstract Data Type) and what isn't (and if it isn't an ADT then it must be the implementation?). Specifically, I'm talking about Heap. I've read in…
Noam
  • 1,640
  • 4
  • 26
  • 55
15
votes
4 answers

What is the right data structure for a queue that support Min, Max operations in O(1) time?

What is the right data structure for a queue that support Enque, Dequeue, Peak, Min, and Max operation and perform all these operations in O(1) time. The most obvious data structure is linked list but Min, Max operations would be O(n). Priority…
bman
  • 5,016
  • 4
  • 36
  • 69
14
votes
3 answers

What would you use the heapq Python module for in real life?

After reading Guido's Sorting a million 32-bit integers in 2MB of RAM using Python, I discovered the heapq module, but the concept is pretty abstract to me. One reason is that I don't understand the concept of a heap completely, but I do understand…
Bite code
  • 578,959
  • 113
  • 301
  • 329
14
votes
1 answer

Brodal priority queue implementation

Have someone ever implemented a Brodal queue? Is it worth implementing or has high running time constants like the Fibonacci Heap?
Simone
  • 2,261
  • 2
  • 19
  • 27
14
votes
8 answers

Priority Queue with a find function - Fastest Implementation

I am looking at implementing a priority queue with an added requirement, a find/search function which will tell whether an item is anywhere within the queue. So the functions will be: insert, del-min and find. I am unsure whether I should use a Heap…
Harry
  • 151
  • 1
  • 1
  • 4
14
votes
1 answer

What is the time complexity of constructing a PriorityQueue from a collection?

What is the complexity of Java's PriorityQueue constructor with a Collection? I used the constructor: PriorityQueue(Collection c) Is the complexity O(n) or O(n*log(n))?
Seffy Golan
  • 201
  • 3
  • 9
14
votes
5 answers

Is there a C++ MinMax Heap implementation?

I'm looking for algorithms like ones in the stl (push_heap, pop_heap, make_heap) except with the ability to pop both the minimum and maximum value efficiently. AKA double ended priority queue. As described here. Any clean implementation of a double…
porgarmingduod
  • 7,668
  • 10
  • 50
  • 83