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
0
votes
2 answers

delMax() api - Priority Queue

In the lecture from Sedgewick, below is the java code, for deleting max element from heap, Question: If I consider line, exch(1, N--), why to decrement N after swapping? Again he access right index with +1 to loiter in pq[N+1] = null for…
overexchange
  • 15,768
  • 30
  • 152
  • 347
0
votes
2 answers

formula for index of a children of a node in a d-ary heap

Assume we have a d-ary heap and we index the nodes from top to bottom and from left to right(starting with 1). Then the children from node i are the nodes with index di,...,di+(d-1). I read this formula in a couple of books but in none of them were…
user3726947
  • 501
  • 5
  • 16
0
votes
2 answers

Are the equations of right and left children of heap accurate?

In the heap data structure, do the two equations left = 2i+1 right = 2i+2 apply on any given heap? if not, in what condition they shall be efficient?
user5665770
0
votes
1 answer

Formation of Binary Heaps using Arrays Shortcut

If we arrange array in ascending order, then we'll get Binary Heap. Is there any drawback of this advantage. If yes then what will be the reason of that?
0
votes
1 answer

K nearest neighbour in a 2d plane

The Question statement is as follows: Find the K closest points to the origin in a 2D plane, given an array containing N points.The output must be in non decreasing order. Solution: I have solved this using comparator and priority queue and…
ojas
  • 2,150
  • 5
  • 22
  • 37
0
votes
1 answer

Replacement selection sorting

So I've been trying to implement this algorithm but I'm not so sure on where to start. Basically from what I understood you can implement it in two ways, by sorting it that the top level is the minimum (minHeap) or that the top level is the max of…
0
votes
1 answer

Binomial Min-Heap Priority Queue with generic types

I am currently implementing Binomial Min-Heap Priority Queue with generic types. I have given the following binomialminheap.java: class BinomialMinHeap , P> { public static class Entry { private P…
0
votes
2 answers

Inserting letters onto an empty heap

I'm trying to insert "KONTINUASJONSEKSAMEN" onto an empty heap. The result is supposed to produce "USTSOSNOMNJNNEKAAKEI", which I'm not capable of. I end up with "UTSSOONSMNJNNEKAAKEI" which I work out from the visualization under. …
Thomas
  • 315
  • 1
  • 5
  • 15
0
votes
0 answers

My median maintenance algorithm doesn't work...(Java)

This is a task from a Coursera course Graph Search, Shortest Paths, and Data Structures by Stanford University.The task is to implement a median maintenance algorithm for this particular stream…
ds998
  • 65
  • 1
  • 8
0
votes
5 answers

C++ heap and ifstream read function

for my assignment I am building a heap, the data for the heap is coming from a file. One of the functions is to get the data, but I am having trouble understanding the ifstream read() function and am getting quite a nasty error because of it this is…
rajh2504
  • 1,266
  • 2
  • 21
  • 37
0
votes
1 answer

Heap comparision between object and static position

I'm working on pathfinding for a 2D tile game. I've found this similar answer, but I'm not sure how to do create the comparison operator when heap compares i <> i+i, when i need manhattan(i) <> manhattan(i+1)? I'm insanely rusty with cpp so go…
Tony
  • 1,318
  • 1
  • 14
  • 36
0
votes
1 answer

Persisting state when implementing MinHeap with heapq

I'm trying to implement a StreamingMedian object to maintain the median through successive calls of get_median(). To do so, I've also implemented a MinHeap and MaxHeap class via the heapq module. I've run into a very odd bug though. For some reason…
cmishra
  • 86
  • 1
  • 4
0
votes
1 answer

Priority Queue Heap: fixing bubbleDown method

I'm working on a priority queue (heap) and think I have a good ground basis. I think my methods all make sense for the most part but really struggling on my bubbleDown and deleteMin methods. public class Heap { private int n; private Node[]…
Jim Derkin
  • 69
  • 1
  • 1
  • 9
0
votes
1 answer

Counting the number of nodes in certain level binomial heap

We have a binomial heap that consist of 2016 nodes. Decompositing into binary we get 11111100000 THe heap consist of 6 strees with nodes 512 256 128 64 32 and 16. But how can we calculate the number of nodes in certain level? What is the formula…
J.dd
  • 145
  • 15
0
votes
0 answers

Percolating down method for heap

I have read a file of integers into an array and I have to turn that one array into 3 separate heaps. Here is my current code but I am having trouble with a percolating down method. I have managed to create an array with the first integer n and…
rahulchawla
  • 170
  • 1
  • 3
  • 20