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
1 answer

Priority Queue not inserting things by Priority

I'm trying to implement a Priority Queue with an Array-Based heap. So far my heap class looks like this public class ArrayHeap { private Node[] data; //Array to hold nodes private int capacity; //Length to make the array private…
Nick Gilbert
  • 4,159
  • 8
  • 43
  • 90
0
votes
1 answer

Building a Min Heap

This might seem kind of a dumb question but I've been trying for hours now but just can't get it. I have to construct a Min Heap using the elements: 120 140 40 50 80 70 60 90 20 100 In the first method I adjusted the position of the element of the…
0
votes
0 answers

heapify function recursing endlessly

The following is a recursive heapify function for an array based priority queue/binary heap. Can anybody tell me why I'm going into an infinite recursion? private static void heapify(int i){ if(i < b_heap.size()/2){ int left = i * 2; …
jokeSlayer94
  • 143
  • 1
  • 9
0
votes
2 answers

Worst-Case Complexity of Below Code

So I'm a total newbie to Java and coding in general, having just learnt Big-O as well. I came across this on the internet yesterday (http://www.dsalgo.com/2013/02/MaxKUsingMinHeap.php.html), and would like to know if the complexity analysis [O(n log…
0
votes
1 answer

I'm using a min-heap here, but i'm doing it wrong. I'm trying to print out some sheep in breadth order while also being sorted lightest to heaviest

My heap is suppposed to print out in breadth order and sort the sheep from lightest to heaviest (min-heap). My test file should add 15 sheep and remove at least 5. I havent attempted the remove part yet because i'm getting an array index out of…
Coltstick
  • 5
  • 4
0
votes
1 answer

How to print a binary heap tree without recursion?

I've been looking for a way to complete this non-recursive method for hours. Our programming teacher has asked us to complete both a recursive and a non-recursive implementation of the same method, used to display the BinaryHeap in the console (I…
Zachariel
  • 96
  • 1
  • 13
0
votes
1 answer

Heap sort - C programmnig

I've been having trouble getting my heap sort program to properly sort integers from a read in file. The output current looks like this: Heap created successfully! size = 10 Insertion 9 8 7 6 3 4 2 5 1 0 Delete 8 7 6 5 4 3 2 1 0 0 I dont know what…
0
votes
2 answers

How to strip or slice the bottom items of a SplHeap (also SplMaxHeap, SplMinHeap)?

I have a SplMaxHeap with 30 items. Now I want to strip the last 10 ones. How to do this in a decent and efficient way? The only solutions I see so far is to copy the first 20 items to a new heap or to convert the heap to an array, use the…
flori
  • 14,339
  • 4
  • 56
  • 63
0
votes
0 answers

Checking for Max order heap?

I know that for a heap to be max ordered heap, the keys of parent nodes should be greater than or equal to those of the children and the highest key should be in the root node. I just wanted to make sure if the way I am thinking of it is right - If…
NatureDevil
  • 483
  • 1
  • 4
  • 14
0
votes
1 answer

Debugging Max Heap in Java

Awhile back I was programming in C++ and I found a tutorial on making a max heap. This was the code and it seems to produce a correctly order heap.` #include const int size = 14; static int A[size] = {1, 2, 5, 10, 2, 11, 12, 7, 23, 22,…
joshu
  • 463
  • 8
  • 18
0
votes
1 answer

heap interface for arrays and linked lists using java

The question is this Your task is to implement a heap that can work with any backing storage. Basically you need to take the abstraction one step down – for instance we use the heap without worrying about the backing storage simply because the…
Priyath Gregory
  • 927
  • 1
  • 11
  • 37
0
votes
1 answer

Leftist max d-heap post-order traversal

I have been sitting on this for about 2 days now. My assignment is to create a leftist max d-heap in java. I am provided with the outline as follows: public LeftistDHeap(int d) public void enqueue(T element) public T dequeue() public String…
Elterado
  • 5
  • 3
0
votes
1 answer

Priority queue with O(1) Get-Min, Delete-Min and Merge operations

I'm wondering if there is a way to construct a priority queue structure that supports constant-time get-min, delete-min, and merge operations. I don't care about the time complexity of insertions and it doesn't have to support the decrease-key…
hslaster
  • 45
  • 4
0
votes
1 answer

Java - How good is my implementation of a binary heap?

I've read a little about binary heaps/priority queues and decided to try my hand at making my own implementation of one. I'm not the most experienced programmer and I would be really grateful if you guys could take a look at my heap class and tell…
Just some guy
  • 1,909
  • 4
  • 21
  • 32
0
votes
2 answers

Deleting the topmost element in the binary heap

I'm trying to implement the binary heap. I've successfully implemented display() and insert() functions. Problem : Inside deleteHeap() function Approach 1.Copy the heap's root into a variable 2.Now copy the heap's last element into the…
Nitin Pandey
  • 649
  • 1
  • 9
  • 27
1 2 3
99
100