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

Q: Optimized data structure for Branched and Versioned entities

Given an entity (say a file or a library/package) that can evolve its versions and branches over time, looking for a optimized data structure that lets me navigate through the versions to a specific instance of the entity. For example (a bit…
V P
  • 845
  • 10
  • 28
0
votes
1 answer

Issues with HeapSort

my task is to write the code to a heapsort according to pseudo code. It should heapsort the input Array (4 3 2 5 6 7 8 9 12 1) and then print it with the printHeap method. I know for a fact that the printHeap works, because I have already used it…
Aere
  • 17
  • 1
  • 5
0
votes
1 answer

Show heapsort repeats comparisons

how would one prove that heapsort repeats comparisons that it has made before? (i.e. it would perform a comparison that has been done previously) Thanks
verticese
  • 273
  • 1
  • 4
  • 11
0
votes
2 answers

Representation of an array as a Heap

I am trying to represent an array in form of a Minimum-Heap. And I am facing a problem in one of the leaf nodes, where parent is greater than (12 grater than 6) the right child. I am not understanding what is wrong in my coding, please help. Here…
Johnny Sins
  • 71
  • 1
  • 10
0
votes
1 answer

Prove that the height of a heap with n nodes is floor(log2n)

How would I prove that the height of a heap with n nodes is floor(log2N)? Any explanation would be great...
JWhatDoe
  • 29
  • 1
  • 7
0
votes
1 answer

heapSort issues java

Looking for some simple tips to fix my heapSort. Hopefully it is something simple that I am not comprehending. I am having a problem with my dumpHeap() and extractMax() methods. It seems to put the dumpheap in order but adds a 0 to my array. It…
Eagles11
  • 31
  • 8
0
votes
1 answer

OCaml Heap Missing Nodes

I have defined a type heap: type 'a heap = | Node of int * int * 'a heap * 'a heap | Leaf;; And the following functions: let rank = function Leaf -> 0 | Node (r,_,_,_) -> r;; let makeNode x a b = if rank a>= rank b then Node(rank b+1,x,a,b) else…
David Farthing
  • 237
  • 2
  • 13
0
votes
0 answers

Java PriorityQueue no way to initialize with a Collection and custom comparator?

I read through the implementation of Java PriorityQueue, the constructors it has are: PriorityQueue() PriorityQueue(Collection c) PriorityQueue(int initialCapacity) PriorityQueue(int initialCapacity, Comparator
Weel
  • 132
  • 2
  • 7
0
votes
1 answer

Implement the min Priority_queue using vector

I have to implement the minimum priority_queue using vector. I am confused with the following code snippet. priority_queue(int , vector , greater pq; But this is absolutely wrong.
Noob
  • 174
  • 1
  • 2
  • 19
0
votes
2 answers

Check if an heap is a min-max heap

I've written a min-max heap, i.e. an heap where finding the minimum and the maximum are constant operations. Now I want to create tests for my class, so I decided to implement a function that checks if a heap is a min-max-heap. Here it is, but I'm…
nbro
  • 15,395
  • 32
  • 113
  • 196
0
votes
2 answers

python - deleting element at index i from heap

I am trying to write a delete function for a heap. I am given a head class and heapify functions to work with, I just need to implement delete. I want to implement it in O(log n) time. This is what I've tried: from heap import * class…
donut juice
  • 257
  • 6
  • 19
0
votes
1 answer

Can I find the second largest element with a min heap with O(lgn) time?

My opinion: In a min heap, the largest element is always on the leaves. The number of leaves is between lg(n+1)/2 and lg(n+1). By linear searching among the leaves I can always find the largest element in the heap with O(lgn) time. Deleting that…
0
votes
1 answer

Downheaping from an interior node with a recursive call to the index of the biggest child?

I am attempting to downheap on an array from the last interior node all the way up to the root, so that I am making mini-heaps from the bottom up. I have an algorithm that I believe will do this though I far from 100% certain at this point, plus I'm…
imgoingmad
  • 95
  • 3
  • 10
0
votes
0 answers

C Binary Heap Without Array implementation

So I am working on a project to implement a heap and heapsort, but it should be implemented with nodes(with parent, left, right pointers) instead of the usual array implementation. It seems pretty straightforward, but the one thing I can't figure…
johnsmith101
  • 179
  • 1
  • 2
  • 11
0
votes
2 answers

Find if min-heap has k smaller elements than query

I've been scratching my head for a long time thinking about this. I need to find an O(k) algorithm to find if a min-heap has k smaller elements than a query q. I've tried a recursive algorithm such as this: count = 0; def kSmaller(H, q, k){ if…
MarksCode
  • 8,074
  • 15
  • 64
  • 133