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
22
votes
8 answers

Implement Heap using a Binary Tree

This question has been asked before in Stack Exchange but it went unanswered. Link to the previously asked question: Binary Heap Implemented via a Binary Tree Structure How do I implement heap in a binary tree. To implement heap, it is important to…
user2200660
  • 1,261
  • 3
  • 18
  • 23
21
votes
1 answer

Python heapify() time complexity

def heapify(A): for root in xrange(len(A)//2-1, -1, -1): rootVal = A[root] child = 2*root+1 while child < len(A): if child+1 < len(A) and A[child] > A[child+1]: child += 1 if…
typing...
  • 934
  • 2
  • 10
  • 25
21
votes
8 answers

Is a sorted array a min-heap? What's the minimum value of a max-heap?

I have studied min-heaps and max-heaps, and I have a couple of questions: Is a sorted array a min-heap? What is the minimum value of a max-heap?
user355002
  • 871
  • 2
  • 10
  • 22
20
votes
3 answers

easy way to maintain a min heap with stl?

for user defined struct, as I understand, it's easy. Just overload the operator <. However, for int/float etc.., do I really need to overload operator < for int? Here is what I tried: #include #include
user268451
  • 759
  • 2
  • 8
  • 16
20
votes
3 answers

Python: Update value of element in heapq

If I have a heapq which contains some elements like: import heapq class Element(object): def __init__(self, name, val): self.name = name self.val = val if __name__ == "__main__": heap = [] e1 = Element('A', 1) e2 =…
Ziva
  • 3,181
  • 15
  • 48
  • 80
19
votes
2 answers

worst case in MAX-HEAPIFY: "the worst case occurs when the bottom level of the tree is exactly half full"

In CLRS, third Edition, on page 155, it is given that in MAX-HEAPIFY, "the worst case occurs when the bottom level of the tree is exactly half full" I guess the reason is that in this case, Max-Heapify has to "float down" through the left…
Happy Mittal
  • 3,667
  • 12
  • 44
  • 60
19
votes
3 answers

Convert a maximum heap to a binary search tree

We are given an array of 2m - 1 distinct, comparable elements, indexed starting from 1. We can view the array as a complete binary tree: Node is placed at index i. Left child is placed at 2i. Right child is placed at 2i+1. For instance, the…
sunmoon
  • 1,448
  • 1
  • 15
  • 27
19
votes
2 answers

Time Complexity of Java PriorityQueue (heap) insertion of n elements?

I would like to know what the time complexity of Java PriorityQueue.Add() is for n elements. I understand that potential worse case insertion a single element is O(log(n)), but it is not clear to me what the time complexity is for inserting a…
James Wierzba
  • 16,176
  • 14
  • 79
  • 120
19
votes
3 answers

Why is a binary heap better as an array than a tree?

When making a binary max heap, why is it better to implement it as a array based, and not a tree based (tree based as in, each node also having a pointer to it's parent)? In terms of run time analysis, memory usage, performance... For binary max…
omega
  • 40,311
  • 81
  • 251
  • 474
18
votes
5 answers

Priority queue with dynamic item priorities

I need to implement a priority queue where the priority of an item in the queue can change and the queue adjusts itself so that items are always removed in the correct order. I have some ideas of how I could implement this but I'm sure this is quite…
sean
  • 741
  • 2
  • 7
  • 14
18
votes
4 answers

What is the difference between A.length and A.heap-size?

I have a question about heap sort. It state in an Algorithms book that A.heap-size<= A.length I don’t understand the difference between the two. If an array represents a heap, why is there a possibility that A.heap-size is less than A.length. I know…
monkey doodle
  • 690
  • 5
  • 12
  • 22
17
votes
1 answer

How is make_heap in C++ implemented to have complexity of 3N?

I wonder what's the algorithm of make_heap in in C++ such that the complexity is 3*N? Only way I can think of to make a heap by inserting elements have complexity of O(N Log N). Thanks a lot!
Frederick Zhao
  • 173
  • 1
  • 4
17
votes
3 answers

Python heapq vs. sorted complexity and performance

I'm relatively new to python (using v3.x syntax) and would appreciate notes regarding complexity and performance of heapq vs. sorted. I've already implemented a heapq based solution for a greedy 'find the best job schedule' algorithm. But then I've…
ofer.sheffer
  • 5,417
  • 7
  • 25
  • 26
17
votes
4 answers

Can I have a heap-like contiguous layout for complete trees based on a depth first order rather than breadth first?

The heap is a classical data structure that puts a complete binary (or d-ary for the generalized version) tree into a contiguous array, storing the elements in breadth-first traversal order. In this way, all elements from the same level of the tree…
gigabytes
  • 3,104
  • 19
  • 35
16
votes
5 answers

Change priority of items in a priority queue

Using Scala 2.9 to implement a kind of Dijkstra algorithm (pseudo code) val queue = new PriorityQueue queue.insert(...) while (!queue.isEmpty) { val u = queue.extractMin queue.foreach { v => if (condition(u, v)) queue.decreaseKey(v,…
binuWADa
  • 616
  • 2
  • 7
  • 15