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

Constructing a data structure with specific requirements

I need to construct a data structure that uses only O(n) bits of storage. The worst time complexity of insert, delete, and maximum needs to be O(log n) but it needs to be O(1) for contains. I have been trying to use a binary heap with only 1s and 0s…
Daniel Cook
  • 1,856
  • 8
  • 23
  • 28
0
votes
1 answer

Java Heap Implementation

You will implement a heap from the following interface: public interface Heap> { public void add(V value); public V[] toArray(V[] array); public V remove(); public void fromArray(V[] array); public V[]…
0
votes
2 answers

C: insert element in empty heap

i'm supposed to write a code, that inserts numbers from stdin into an at first empty max-heap. my code just doesn't get the order of elements right, i found out, that it doesnt even enter the while loop before the third number. Anybody willing to…
Meowzen
  • 113
  • 1
  • 3
  • 15
0
votes
1 answer

C++ STL priority queue

I have a question regarding the implementation of priority_queue container adaptor. Now, I know it internally uses the push_heap, pop_heap functions. This is my question: make_heap turns a vector into a heap in O(n) time by utilizing the heapify…
Manav Garg
  • 512
  • 1
  • 3
  • 17
0
votes
0 answers

Delete certain element from heap

I know basic heap operations like insertion,deletion,finding minimum etc.I know deletion at certain index like deletion of element present at index 2 or so,but how to delete if we are to delete certain value that is present in the heap ? In my…
0
votes
1 answer

Cannot carry object to function properly C++

cliques is my class object. All my class commands work when I use them in my main but for some reason I cannot make changes to my heaps or stacks through my functions. I tried using reference markers on my function parameters but still I'm having…
0
votes
2 answers

What is the correct definition of a heap

I was reading about heaps in Java programming. In my textbook, I found this definition of a heap: a heap is a complete binary tree with the following properties: 1) the value in the root is the smallest item in the tree; 2) every subtree is a…
user5595273
0
votes
1 answer

Uniqueness of min heap?

Can we create multiple min heaps from a given unique non-heap list? Both L1 and L2 below (which share the exact same elements, but not in the same order) are legitimate min heaps of this original non-heap list:…
Antoine
  • 1,649
  • 4
  • 23
  • 50
0
votes
1 answer

R equivalent of Python's heapq.heapify()?

Everything is said in the title. I'm looking for the R equivalent of Python's heapq.heapify(). I could not find it, but I hope it exists somewhere. heapq is a heap queue implementation module and its member function heapify accepts as input a list…
Antoine
  • 1,649
  • 4
  • 23
  • 50
0
votes
0 answers

Find minimum in Max-Heap

I want to write the most efficient algorithm to find minimum element in Max-Heap. Important facts: The heap is represented as array (each element in index i has 2 children - left and right - indices 2i and 2i+1), all elements are different. This is…
Ran
  • 35
  • 1
  • 7
0
votes
1 answer

Issues creating a vector of class object in c++

I created the following class #include "cliques.h" #include "vector" #include using namespace std; cliques::cliques(){ } cliques::cliques(int i) { clique.push_back(i); clique_prob = 1; mclique_prob =…
0
votes
1 answer

minimal elements in heap for Python

Want to get the minimal elements for a min heap in Python using heapq, here is my code and wondering if using h[0] is the correct way or a more elegant public API for heapq? I tried to find is there is an API to get minimal element of a heap, but…
Lin Ma
  • 9,739
  • 32
  • 105
  • 175
0
votes
1 answer

How to write comparator class for STL priority queue?

This is the two classes I have class Graph { private: unordered_map _vertices; }; class Vertex { private: unordered_map _edges;//key: pointer to a vertex,…
0
votes
1 answer

Heapsort heapify

I found code for a heapsort from: http://rosettacode.org/wiki/Sorting_algorithms/Heapsort#C The way I understand it (which is wrong somewhere along the lines) is that the heapsort() function has two loops. The first loop is to create the heap…
kendall
  • 41
  • 1
  • 6
0
votes
1 answer

heap sort in Python for advice

I only need to retrieve the 3 smallest elements, and wondering if there is a way to improve my below code to keep heap size smaller -- I think if we only need to keep heap size as 3, it is enough. But cannot find an option in heapq to tweak. In…
Lin Ma
  • 9,739
  • 32
  • 105
  • 175