Questions tagged [heaps]

249 questions
129
votes
5 answers

What's the difference between a binary search tree and a binary heap?

These two seem very similar and have almost an identical structure. What's the difference? What are the time complexities for different operations of each?
Piper
  • 1,779
  • 2
  • 12
  • 13
26
votes
2 answers

What is the advantage of heaps over sorted arrays?

I'm fairly new to heaps and am trying to wrap my head around why min and max heaps are represented as trees when a sorted array appears to both provide min / max properties by default. And a follow up: what is the advantage of dealing with the…
Nick Olinger
  • 363
  • 1
  • 3
  • 6
23
votes
1 answer

How many different max-heaps exist for a list of n integers?

How many different max-heaps exist for a list of $n$ integers? Example: list [1, 2, 3, 4] The max-heap can be either 4 3 2 1: 4 / \ 3 2 / 1 or 4 2 3 1: 4 / \ 2 3 / 1
Pratik Deoghare
  • 1,751
  • 2
  • 14
  • 22
22
votes
4 answers

Increase-key and decrease-key in a binary min-heap

In many discussions of binary heap, normally only decrease-key is listed as supported operation for a min-heap. For example, CLR chapter 6.1 and this wikipedia page. Why isn't increase key normally listed for min-heap? I imagine it is possible to do…
GatotPujo
  • 221
  • 1
  • 2
  • 3
16
votes
2 answers

Heap - Give an $O(n \lg k)$ time algorithm to merge $k$ sorted lists into one sorted list

Most probably, this question is asked before. It's from CLRS (2nd Ed) problem 6.5-8 -- Give an $O(n \lg k)$ time algorithm to merge $k$ sorted lists into one sorted list, where $n$ is the total number of elements in all the input lists. (Hint: Use…
ramgorur
  • 541
  • 1
  • 5
  • 16
12
votes
1 answer

Potential function binary heap extract max O(1)

I need help figuring the potential function for a max heap so that extract max is completed in $O(1)$ amortised time. I should add that I do not have a good understanding of the potential method. I know that the insert function should "pay" more in…
12
votes
1 answer

Why use heap over red-black tree?

Heap supports insert operation in $O(\log n)$ time. And while heap supports remove min/max in $O(\log n)$ time, to remove any element (non min/max) heap takes $O(n)$ time. However, red-black tree supports insert/remove both in $O(\log n)$ time. We…
Wonjoo Lee
  • 123
  • 1
  • 5
10
votes
2 answers

Is search a binary heap operation?

According to the Wikipedia page, search is "not an operation" on binary heaps (see complexity box at top-right). Why not? Binary heaps may not be sorted, but they are ordered, and a full graph traversal can find any object in $O(n)$ time, no? Is the…
Barry Fruitman
  • 755
  • 3
  • 10
  • 17
9
votes
1 answer

Randomized Meldable Heap - Expected Height

Randomized Meldable Heaps have an operation "meld", which we then use to define all other operations, including insert. The question is, what is an expected height of that tree with $n$ nodes? Theorem 1 of Gambin and Malinkowski, Randomized Meldable…
8
votes
3 answers

Best and worse case inputs for heap sort and quick sort?

So given an input of lets say 10 strings, what way can we input these so we get the best or worst case for these two given sorts? Heap sort: best case - nlogn worst case - nlogn Quick sort: best case - nlogn worst case - n^2 Where I get confused…
aisdmsaidmas
  • 81
  • 1
  • 1
  • 2
7
votes
3 answers

Find common min in logarithmic time

I am looking for a data structure to store a set such that given two instances of size $O(n)$ which are known to have non-empty intersection, the minimum element of the intersection can be found in $O(\log n)$ time. Is this possible to achieve,…
pre-kidney
  • 121
  • 6
7
votes
1 answer

What is the purpose of Mark field in Fibonacci Heaps?

In Fibonacci heaps, we keep a mark field for every node in the heap. Initially all the nodes are unmarked. Once a node is deleted, its parent is marked. If a node is deleted and its parent is already marked, the parent will be cut and inserted into…
Helium
  • 541
  • 3
  • 15
6
votes
2 answers

Finding the height of a d-ary heap

I would like to find the height of a d-ary heap. Assuming you have an Array that starts indexing at $1$ we have the following: The parent of a node $i$ is given by: $\left\lfloor\frac{i+1}{d}\right\rfloor$ The $d$ children of a parent at node $i$…
CodeKingPlusPlus
  • 517
  • 1
  • 6
  • 14
6
votes
1 answer

Min Fibonacci Heap - increase key

I have been trying to implementing heap data structures for use in my research work. As part of that, I am trying to implement increase-key operations for min-heaps. I know that min-heaps generally support decrease-key. I was able to write the…
6
votes
2 answers

An algorithm to efficiently insert a list of elements into a binary heap ("bulk insertion")

I wonder if there is any elegant algorithm for inserting a list of elements into a binary heap (at once) whose performance would be close to that of inserting elements one by one when there are only a few elements to insert, and which would still…
Alexey
  • 251
  • 1
  • 13
1
2 3
16 17