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

How do I modify the Min Heap insert and delete function to accept a second comparison if the primary comparison happens to be equal?

below I have a standard insert and delete function for a Min Heap, what I need to do is add a special case to both the functions when the T.num comparison happen to be equal, I then need to then compare the T.Letter where the lower Ascii value is…
user342231
  • 519
  • 1
  • 6
  • 6
0
votes
2 answers

Null pointer exception while implementing Heapify for removing minimum value in MinHeap

For the below program: public class MinHeap> implements HeapInterface { private T[] backingArray; private int size; // Do not add any more instance variables /** * Creates a Heap. */ public MinHeap() { …
user2398046
0
votes
1 answer

Invalid heap while pushing object into vector and then pushing vector into heap

I'm trying here to use a class Bar that stores a ordered array of Foo, by it's member var as seen bellow. I was expecting the output of this like 3,4,5 but I get 5,3,4. I'm using my own compare function (mandatory because my Foo struct has more…
waas1919
  • 2,365
  • 7
  • 44
  • 76
0
votes
2 answers

same key multiple times in a heap

In a heap (whether max heap or min heap), is it possible that there can be the same key twice or more than once? How can this scenario corrupt the time complexity of O(n) for makeheap() and O(log(n)) for insertion and removing? example: is the…
ThunderWiring
  • 738
  • 1
  • 7
  • 29
0
votes
2 answers

Secondary Order in Heap::Simple

How do I define a secondary ordering to the Heap::Simple interface in Perl?
syker
  • 10,912
  • 16
  • 56
  • 68
0
votes
1 answer

Creation of max Heap from stream of integer

Since i know that if there is n element given in array and i want to create a max heap it took o(n) because logic is we apply heapify from last parent to root. But my question is suppose if there is stream of integer is coming and i want to insert…
nikhil jain
  • 105
  • 9
0
votes
1 answer

complexity analysis of kth smallest element in min heap

I am working on to find the kth smallest element in min heap. I have got a code for this whose complexity is O(k log k). I tried to improve it to O(k). Below is the code. struct heap{ int *array; int count; int capacity; }; …
Vasu Dev Garg
  • 121
  • 1
  • 15
0
votes
1 answer

Does the median value in a heap have to be in the bottom row?

We learned in class that a binary heap has n/2 leaves, but does that have to assure us that the median of that heap is a leaf? Or can it also be a node who has a left/right child? Also, does this depend on n?
0
votes
1 answer

Best case time complexity of deletion in Max-Heap

I've been told that the best case time complexity of deleting an element from a max heap is O(1). From what I understand, the best case should be O(logn) since we always have to perculate down after deleting the root and placing the last eleement in…
0
votes
3 answers

max heap and binary tree

this is an example in my data structure book and for this exercise was written that this is not a max heap,but it doesn't say its reason would you please help me that why it is not a max heap thanks. 35 / \ / \ 27 28 …
user355002
  • 871
  • 2
  • 10
  • 22
0
votes
2 answers

about heap(max-heap and min heap)

I have this question that in the heap data structure , a left child can be more than a right child in its own level ? I mean that consider these three numbers 9,5,8 and I want to make a max-heap data structure so the root will be 9 and is it true…
user355002
  • 871
  • 2
  • 10
  • 22
0
votes
1 answer

std::push_heap and std::pop_heap with MoveConstructible objects

I want to maintain a heap where the payloads are MoveConstructible (because they hold a std::unique_ptr inside.) Although the documentation suggests that the object must be MoveAssignable and MoveConstructible, doing so throws an error both in GCC…
Arindam
  • 342
  • 1
  • 12
0
votes
1 answer

Would building a max heap from an Unsorted array would follow Binary Tree properties?

Given an unsorted array of size 10 int[] arr={∞,1,2,3,4,5,6,7,8,9,10}; If I execute code public void build_heap(){ for(int i=size/2;i>=1;i--) max_heapify(i); } The resulting array in what case follow binary tree…
0
votes
2 answers

MinHeap implementation in c#

I am working on a MinHeap implementation for school and I have encountered a problem. The code typically works well but sometimes generates an argument out of range exception in my heapify method. I have tried to isolate the problem but I am a…
Hempo
  • 3
  • 2
0
votes
1 answer

how do you compute the constant, c, for the asymptotic runtimes of heapSort?

I am trying to understand how to compute the constant, c, when given the data. Before showing the data, I will inform you that I have already graphed the data with a linear trend on Excel. I am still quite baffled as to what I should use to…
Neil2o
  • 3
  • 1