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

Is it possible to implement a heap without knowing the size of the input beforehand?

Say, I have a continuous input of integers of unknown size. At any instant in time, upon receiving k elements as input, I would like to have a heap of size k. How can this be achieved without initializing an array of some length beforehand?
saltandwater
  • 791
  • 2
  • 9
  • 25
0
votes
1 answer

Heapify each sublist in a list of lists in python?

I have a list of lists called opened which was declared like this: opened = [[] for i in range(5)] Now opened = [[], [], [], [], []] How can I heapify each of the sublists using the heapq.heapify() function? i.e, opened[0], opened[1], opened[2],…
Nihal Rp
  • 484
  • 6
  • 15
0
votes
1 answer

Improving efficiency of set backed by max heap

What is the worst-case time complexity for search in a set backed by a max heap? If you look for the smallest item in the set, that could take order of O(n) time, right? Is there a faster way?
Max Chang
  • 129
  • 1
  • 1
  • 8
0
votes
0 answers

What is the maximum index of kth smallest element of max heap when stored in the form of an array?

I am new to the data structures. I am not getting from where to start. Actually, i am not getting how to solve this? What is the maximum index of kth smallest element of max heap having N numbers when stored in the form of an array ? Any idea or…
Garrick
  • 677
  • 4
  • 15
  • 34
0
votes
0 answers

How to build a heap out of a list in python

I'm given a list and I am to create a function that heapifys this list as well as count all the steps it took to complete this process. I'm brand new to heaps so I have little to no idea what to do. I kind of understand WHAT a heap is, but I don't…
oneman
  • 811
  • 1
  • 13
  • 31
0
votes
1 answer

Max heap throwing index out of bounds

Below is my max heap implementation. I can really figure out why I am getting the index out of bounds error. I have assigned array to self.heap class heap: def __init__(self): self.heapsize=0 self.heap=[0] def…
user1006915
  • 19
  • 1
  • 2
0
votes
0 answers

C - construct max heap from given binary tree

Given a binary tree: typedef struct node { int info; struct node *left,*right; }node; How to implement a recursive function void heapify(node *root) to construct a max heap from a binary tree given above?
ufo
  • 93
  • 1
  • 11
0
votes
2 answers

Max Heapify issue

I have been trying to debug this code for hours. I don't know why it is not rearranging the terms. I have tried everything I can think of. Can someone help? Thanks. public void heapify(int i) // utility routine to percolate down from index i { …
Math4Life
  • 125
  • 8
0
votes
1 answer

Ternary tree and max heap - How does it work visually?

I have a set of numbers I want to insert: (left to right) 61, 49, 90, 76, 46, 1, 82, 44, 62 ,79 Can anyone please show how it looks visually with a tree, I fail to follow after inserting 82 to the heap. 61 61 49 90 49 61 90 49 61 76 90 49 61 76…
Ilan Aizelman WS
  • 1,630
  • 2
  • 21
  • 44
0
votes
0 answers

In Python, without inporting anything, is it possible to do constant time searching on a Double Ended Priority Queue?

Double Ended Priority Queue is implemented using Interval Heaps. My double ended priority queue is of the form [['abc',xyz'],['c'.'t'],['f','n']]; so here minimum is 'abc' and maximum is 'xyz'. Heap is represented as list.
0
votes
2 answers

Heap Bit-Shift Macros for Left, Right and Parent

I am reading about Heaps, which describes that you can do the operations of accessing the left child, the RIGHT/LEFT child and the PARENT with bit shift operations. While Left and Parent seems trivial i am not sure with the right one. Do i just have…
user1767754
  • 23,311
  • 18
  • 141
  • 164
0
votes
1 answer

Efficient bookkeeping in heap

I'm trying to implement a heap using list data structure. I'd also like to keep track of the position of elements in the list in order to enable easy deletion. My implementation involves looping through the entire list to update the positions after…
Effective_cellist
  • 1,098
  • 1
  • 19
  • 39
0
votes
1 answer

Optimizing heap structure for heapsort

I'm implementing heapsort using a heap. To do this each value to be sorted is inserted. The insertion method calls heapifyUp() (aka siftUp) so this means each time another value is inserted heapifyUp is called. Is this the most efficient…
Celeritas
  • 14,489
  • 36
  • 113
  • 194
0
votes
1 answer

Reversing the order of a popped priority queue in c

I have this code which pops the highest priority node from the queue. Currently the highest priority is defined as 1. I would like to change this so the highest priority is from 10 -> 1. To clarify the problem is in the pop function, others are…
Josh G
  • 1
  • 1
0
votes
2 answers

c++ map into min heap

I want to know an easy way to obtain a (Map + min-Heap) data structure in C++ I tried this: struct Comp { bool operator()(const pair& y , const pair& z) { return (y.second < z.second); } }; priority_queue<…
Ryuzaki
  • 95
  • 1
  • 9