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

Merge multiple pre-ordered ArrayLists in to a new Array

I have 12 pre-ordered Integer[] arrays in 12 files. I'd like to merge them together just like I would in a mergeSort, with the difference that I can't combine them into one array before sorting. I must keep them separate and their size cannot exceed…
0
votes
1 answer

How to perform inplace sorting of min-heap using heap-Sort?

Whenever I do heap-Sort on min heap I get reverse sorted array. Is there any way of sorting min heap using heap sort without using extra space?
quintin
  • 812
  • 1
  • 10
  • 35
0
votes
1 answer

Binomial Heaps structure doesn't let you to have pointer to nodes (or iterators)

I've been playing with Binomial Heaps, and I encountered a problem I'd like to discuss here, because I think it may concern some user implementing a Binomial Heap data structure. My aim is to have pointers to nodes, or handles, which directly points…
Marco Pagliaricci
  • 1,366
  • 17
  • 31
0
votes
1 answer

Are my first two iterations of heap sort correct?

Given an array [3, 1, 8, 5, 4, 7, 6], I want to show the result of the first two iterations of heap sort, plus the result of initial build-heap to make a max heap. Here is what I have so far Initial: [3, 1, 8, 5, 4, 7, 6] After max-heap:…
Omar N
  • 1,720
  • 2
  • 21
  • 33
0
votes
0 answers

How do I implement a min heap from a max heap?

I have a heap sorted by max values, I want to have a function where I can use min values instead to on top. I can either convert the max heap to a min, or create a min heap altogether. Which one would be easier ? Here is my Heap.cpp //…
CRod
  • 315
  • 1
  • 3
  • 16
0
votes
1 answer

Finding the max value in a heap (Scheme)

I'm trying to return a function that returns the maximum value in a heap (where the heap goes from min on top to max on the bottom). I understand how I could do something similar to this in BST. But I don't know how I would do this in a heap because…
Anon
  • 69
  • 7
0
votes
1 answer

Handling duplicate values while merging k sorted arrays

I am trying to merge k sorted array of structs into a single one. I know the algorithm of using a min heap to merge the arrays. I am using priority_queue in C++ to implement the heap. My code looks like below. struct Num { int key; int…
Pattu
  • 3,481
  • 8
  • 32
  • 41
0
votes
1 answer

Implementation of decrease key function for a min heap priority queue

I'm trying to run Dijkstra's algorithm and I need to implement a decreaseKey function. I'm having trouble doing it. I read one solution here but it uses a lot of memory by storing a hash map in the heap. Is there a way to implement decreaseKey…
David Velasquez
  • 2,346
  • 1
  • 26
  • 46
0
votes
0 answers

NoClassDefFoundError in java tester

class dHeap> implements dHeapInterface public dHeap(int heapSize) { dVar = 2; size = heapSize; array = (T[]) new Comparable[heapSize]; nelems = 0; } . public class dHeapTester { private…
Jakkie Chan
  • 317
  • 4
  • 14
0
votes
1 answer

Am I implementing a heap-based priority queue correctly? Is downheap necessary?

I believe I just correctly completed the following assignment: Implement a heap ­based priority queue class using the vector representation, containing characters. My program compiles and when implemented for the remainder of the assignment I…
0
votes
2 answers

Given a stream of number, like 1,3,5,4,6,9, I was asked to print them like 1,3-6,9

Given a stream of number, like 1,3,5,4,6,9, I was asked to print them like 1,3-6,9. My approach was to hold min 2 numbers in a maxHeap and max 2 numbers in a minHeap. And I have come up with a following solution. Do you have any suggestion to make…
Gökhan Akduğan
  • 533
  • 1
  • 8
  • 17
0
votes
0 answers

Determining heap positions

I'm working on the following problem. This question was asked earlier, but I want to make sure I understood it thoroughly. "The largest item in a heap must appear in position 1, and the second largest must be in position 2 or position 3. Give the…
Mr.Rabbit
  • 101
  • 8
0
votes
1 answer

Find the three most common words in a dictionary

Question is to find the three most common words in a dictionary. I have come up with below code, but it does not work for some reason (I mean when I try to run it in eclipse, it directly leads me to debugging page, although I do not get any error on…
Gökhan Akduğan
  • 533
  • 1
  • 8
  • 17
0
votes
1 answer

Container for time stamp organization

the following is an excerpt from some code I have. the hpp: class message_receiver { struct Chunk { // inits timestamp and other data memebers Chunk(); boost::uint64_t m_timeStamp; bool last; …
joshu
  • 463
  • 8
  • 18
0
votes
1 answer

improving heapq efficiency

So I find myself taking advantage of heapq for some calculations. However, for the problem I am working on, it runs slow because the heap gets pretty big. I thought I had an option to speed it up. Rather than creating a giant heap, make it a heap…
Joel
  • 22,598
  • 6
  • 69
  • 93