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

Most Efficient Way to find the logn th Maximum in a Max Heap

The title says it all; What is the most efficient way to find the log nth maximum number in a max heap with n elements? ps: The text book I'm reading vaguely explained a solution with time complexity of O(lognloglogn) by using another heap that I…
Amen
  • 1,524
  • 5
  • 22
  • 41
0
votes
3 answers

Search function for a Heap

I have made a generic Binary Heap (MaxHeap) in which I have to search for a particular node based upon the value present in the node. I have made it such that the search function using Pre-OrderTraversal and it should give a run time of Order n…
Avi
  • 1
  • 1
0
votes
1 answer

Downheap on array giving wrong output

I am writing a program to execute a heap sort. When I try to execute the removeMin function, and a downheap, it seems that I am always getting incorrect output. For example if I input 10 integers in this order: 3, 6, 8, 3, 89, 35, 7, 9, 1, 4 I…
Stephen O'C
  • 77
  • 1
  • 5
0
votes
0 answers

How do I modify a k-ary heap's C code so that the layout of a 4-heap fit on a cache line and the array is padded to cache-align the heap

In Anthony LaMarca and Richard Ladner's January 6 1997 paper titled "The Influence of Caches on the Performance of Heaps", published in the ACM Journal of Experimental Algorithmics they write that: In our heapsort executions, all three of the…
0
votes
0 answers

Finding Median of a Min Heap

If I already have a min heap built what is the fastest way I can find the median? Seems like it is redundant to use the 2 heap (max and min) solution.
jsho
  • 73
  • 2
  • 5
0
votes
0 answers

Prolog error when adding to heaps

I am getting an error when adding elements to a heap in prolog. When I add a priority, key pair to the heap it adds it as t(Priority,Key,[]) which causes my program to crash further down the line. Here's the code in…
lumpofiron
  • 154
  • 1
  • 8
0
votes
1 answer

Java Arraylist Heap Implementation

I am trying to implement an insert() method for an arraylist-based heap implementation, but whenever I test the method inserting more than one integer where at least one of them is greater than ~4 the program takes forever to run. For example,…
Mason Walton
  • 131
  • 4
  • 11
0
votes
1 answer

Why does it require no more than 1 + logN compares when inserting a new node into a heap?

I think when inserting a new node into a heap, the amount of nodes it might passes by is logN, why is it (1 + logN), where is 1 from?
user1888955
  • 626
  • 1
  • 9
  • 27
0
votes
1 answer

Time complexity of Kth smallest using Heap

Following is a code for finding the kth smallest element in an array using heap. The time complexity is O(n log(k)) where k is the size of the heap. As per my understanding, you first go through the entire array i.e. O(n) to populate your heap.…
Pha3drus
  • 169
  • 1
  • 1
  • 10
0
votes
0 answers

Computional complexity of searching lowest element in Max-Heap and AVL tree

I try to define Omega and O complexity of searching the lowest element in Maximum oriented heap and AVL tree. I don't know if the word "lowest" plays here big role, what it think is that Omega complexity for AVL and Max-Heap could be Omega(1) if I…
BlackHawk
  • 1
  • 3
0
votes
1 answer

Remove element from heapq

I am trying to implement the AStar search algorithm and I'm having trouble removing elements from the fringe (open list). I push my Cells in like this: heapq.heappush(myHeap, (priority, cell) My cell class looks like this: class Cell(object): …
o.o
  • 3,563
  • 9
  • 42
  • 71
0
votes
1 answer

Max Heap segmentation fault

Hello dear computer lover. I have once again a problem. I am supposed to program a Max-Heap. The heap.h and main.c are pre-set and should be correct. I will now implement 5 functions: Insert_heap Heapify Heap_create Free_heap Extract_max_heap So…
CL89
  • 13
  • 3
0
votes
1 answer

Building MaxHeap Largest Not Being Sent To Top

I am trying to build a max heap out of a vector of size 10. It contains numbers 1-10. Yet my program will not set the largest value 10 to the largest variable because when comparing it exceeds my vector's range. if ((l <= v.size()) && (v.at(l) >…
0
votes
2 answers

Comparator doesn't remove number duplicates in TreeSet

I used a self-defined comparator to initialize treeset, making it a min-heap. It works fine to remove duplicates of small numbers such as 1, 2, 3. But when the numbers are large, duplicates remain in the treeset. Here's my code: public class Test {…
0
votes
1 answer

At any point we need to tell the top k products based on quantity

There is a stream of data coming. Data contains Product Id and Quantity. At any point we need to tell the top k products based on quantity. My Approach : Maintain one minHeap of size k Maintain one hashmap which stores Product id as key and Product…