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
36
votes
4 answers

python, heapq: difference between heappushpop() and heapreplace()

I couldn't figure out the difference (other than ordinality of push/pop actions) between functions heapq.heappushpop() and heapq.heapreplace() when i tested out the following code. >>> from heapq import * >>> a=[2,7,4,0,8,12,14,13,10,3,4] >>>…
Ayush
  • 3,695
  • 1
  • 32
  • 42
35
votes
7 answers

How can I use binary heap in the Dijkstra algorithm?

I am writing code of dijkstra algorithm, for the part where we are supposed to find the node with minimum distance from the currently being used node, I am using a array over there and traversing it fully to figure out the node. This part can be…
Max
  • 9,100
  • 25
  • 72
  • 109
34
votes
2 answers

O(klogk) time algorithm to find kth smallest element from a binary heap

We have an n-node binary heap which contains n distinct items (smallest item at the root). For a k<=n, find a O(klogk) time algorithm to select kth smallest element from the heap. O(klogn) is obvious, but couldn't figure out a O(klogk) one. Maybe we…
user978837
34
votes
3 answers

Algorithm for merging two max heaps?

Is there an efficient algorithm for merging 2 max-heaps that are stored as arrays?
ThP
  • 2,312
  • 4
  • 19
  • 25
31
votes
4 answers

Understanding how to create a heap in Python

The collections.Count.most_common function in Python uses the heapq module to return the count of the most common word in a file, for instance. I have traced through the heapq.py file, but I'm having a bit of trouble understanding how a heap is…
Sam Hammamy
  • 10,819
  • 10
  • 56
  • 94
30
votes
3 answers

SQL Server heap v.s. clustered index

I am using SQL Server 2008. I know if a table has no clustered index, then it is called heap, or else the storage model is called clustered index (B-Tree). I want to learn more about what exactly means heap storage, what it looks like and whether it…
George2
  • 44,761
  • 110
  • 317
  • 455
29
votes
1 answer

Argument for O(1) average-case complexity of heap insertion

The claim on the Wikipedia page for binary heaps is that insertion is O(log n) in the worst case, but O(1) on average: The number of operations required depends only on the number of levels the new element must rise to satisfy the heap property,…
chiastic-security
  • 20,430
  • 4
  • 39
  • 67
28
votes
3 answers

How to implement Priority Queues in Python?

Sorry for such a silly question but Python docs are confusing... Link 1: Queue Implementation http://docs.python.org/library/queue.html It says that Queue has a class for the priority queue. But I could not find how to implement it. class…
codersofthedark
  • 9,183
  • 8
  • 45
  • 70
26
votes
3 answers

Comparator for min-heap in C++

I am trying to make a min-heap1 of longs in C++ using the STL make_heap, etc., but my comparator doesn't seem to be comparing properly. The following is my current comparator: struct greater1{ bool operator()(const long& a,const long& b) const{ …
Jakob Weisblat
  • 7,450
  • 9
  • 37
  • 65
25
votes
2 answers

Heap class in .NET

Possible Duplicate: Fibonacci, Binary, or Binomial heap in c#? Is there any class like heap in .NET? I need some kind of collection from which I can retrieve min. element. I just want 3 methods: Add() RemoveMinElement() GetMinElement() I can't…
Tomek Tarczynski
  • 2,785
  • 8
  • 37
  • 43
25
votes
8 answers

What is the definition for the height of a tree?

I can't seem to find a definitive answer for this, I'm trying to do some elementary proofs on heaps but here's what's throwing me off a little bit: Is an empty tree valid? If so, what is its height? I would think this would be 0. What is the height…
Brad
  • 562
  • 1
  • 5
  • 11
24
votes
2 answers

Is there an easy way to make a min heap in C++?

I'm very new to C++, and I was wondering if there was a way to make a min heap in C++ from the standard library.
Alex
  • 3,767
  • 5
  • 19
  • 10
23
votes
2 answers

Dijkstra algorithm. Min heap as a min-priority queue

I'm reading about Dijkstra's algorithm in CLRS, Third Edition (p. 662). Here is a part from the book I don't understand: If the graph is sufficiently sparse — in particular, E = o(V^2/lg V) — we can improve the algorithm by implementing the…
Maksim Dmitriev
  • 5,985
  • 12
  • 73
  • 138
23
votes
8 answers

How to maintain dictionary in a heap in python?

I have a dictionary as follows: {'abc':100,'xyz':200,'def':250 .............} It is a dictionary with keys as a name of a entity and the value is count of that entity. I need to return the top 10 elements of from the dictionary. I can write a heap…
gizgok
  • 7,303
  • 21
  • 79
  • 124
22
votes
5 answers

Why are heaps in c++ implemented as algorithms instead of containers?

I was wondering why the heap concept is implemented as algorithms (make_heap, pop_heap, push_heap, sort_heap) instead of a container. I am especially interested is some one's solution can also explain why set and map are containers instead of…
KitsuneYMG
  • 12,753
  • 4
  • 37
  • 58