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
850
votes
19 answers

How can building a heap be O(n) time complexity?

Can someone help explain how can building a heap be O(n) complexity? Inserting an item into a heap is O(log n), and the insert is repeated n/2 times (the remainder are leaves, and can't violate the heap property). So, this means the complexity…
GBa
  • 17,509
  • 15
  • 49
  • 67
427
votes
19 answers

What do I use for a max-heap implementation in Python?

Python includes the heapq module for min-heaps, but I need a max-heap. What should I use for a max-heap implementation in Python?
Douglas Mayle
  • 21,063
  • 9
  • 42
  • 57
254
votes
10 answers

Find running median from a stream of integers

Possible Duplicate: Rolling median algorithm in C Given that integers are read from a data stream. Find median of elements read so far in efficient way. Solution I have read: We can use a max heap on left side to represent elements that are…
Luv
  • 5,381
  • 9
  • 48
  • 61
242
votes
12 answers

Priority queue in .Net

I am looking for a .NET implementation of a priority queue or heap data structure Priority queues are data structures that provide more flexibility than simple sorting, because they allow new elements to enter a system at arbitrary intervals. It is…
Doug McClean
  • 14,265
  • 6
  • 48
  • 70
226
votes
9 answers

Why are two different concepts both called "heap"?

Why are the runtime heap used for dynamic memory allocation in C-style languages and the data structure both called "the heap"? Is there some relation?
Andrey Fedorov
  • 9,148
  • 20
  • 67
  • 99
212
votes
8 answers

Heap vs Binary Search Tree (BST)

What is the difference between a heap and BST? When to use a heap and when to use a BST? If you want to get the elements in a sorted fashion, is BST better over heap?
kc3
  • 4,281
  • 7
  • 20
  • 16
125
votes
7 answers

Is there a Heap in java?

I am porting a C++ library to Java and I need a heap data structure. Is there a standard implementation or will I need to do it myself?
user1796942
  • 3,228
  • 5
  • 29
  • 38
122
votes
6 answers

When would I want to use a heap?

Besides the obvious answer of a Priority Queue, when would a heap be useful in my programming adventures?
Mithrax
  • 7,603
  • 18
  • 55
  • 60
104
votes
9 answers

How to make heapq evaluate the heap off of a specific attribute?

I wish to hold a heap of objects, not just numbers. They will have an integer attribute in them that the heap can sort by. The easiest way to use heaps in python is heapq, but how do I tell it to sort by a specific attribute when using heapq?
coffee
  • 1,121
  • 2
  • 9
  • 5
98
votes
3 answers

What's the difference between heapq and PriorityQueue in python?

In python there's a built-in heapq algorithm that gives you push, pop, nlargest, nsmallest... etc that you can apply to lists. However, there's also the queue.PriorityQueue class that seems to support more or less the same functionality. What's the…
yelsayed
  • 5,236
  • 3
  • 27
  • 38
90
votes
5 answers

Worst case in Max-Heapify - How do you get 2n/3?

In CLRS, third Edition, on page 155, it is given that in MAX-HEAPIFY, The children’s subtrees each have size at most 2n/3—the worst case occurs when the bottom level of the tree is exactly half full. I understand why it is worst when the bottom…
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
82
votes
6 answers

Difference between priority queue and a heap

It seems that a priority queue is just a heap with normal queue operations like insert, delete, top, etc. Is this the correct way to interpret a priority queue? I know you can build priority queues in different ways but if I were to build a priority…
Mars
  • 4,677
  • 8
  • 43
  • 65
79
votes
4 answers

What is Python's heapq module?

I tried "heapq" and arrived at the conclusion that my expectations differ from what I see on the screen. I need somebody to explain how it works and where it can be useful. From the book Python Module of the Week under paragraph 2.2 Sorting it is…
minerals
  • 6,090
  • 17
  • 62
  • 107
72
votes
5 answers

What's the time complexity of functions in heapq library

My question is from the solution in leetcode below, I can't understand why it is O(k+(n-k)log(k)). Supplement: Maybe the complexity isn't that, in fact I don't know the time complexity of heappush() and heappop() # O(k+(n-k)lgk) time, min-heap def…
user6617337
70
votes
9 answers

Finding the median of an unsorted array

To find the median of an unsorted array, we can make a min-heap in O(nlogn) time for n elements, and then we can extract one by one n/2 elements to get the median. But this approach would take O(nlogn) time. Can we do the same by some method in O(n)…
Luv
  • 5,381
  • 9
  • 48
  • 61
1
2 3
99 100