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

Reverse Sort for a Templated Heap class

I have created a Templated Heap Class and have managed to sort the list in ascending order through the use of a custom iterator class, i was just wondering how i would sort it in reverse order. template inline void…
0
votes
1 answer

std::vector out of range for min-heap: C++

I've neglected to work on this code (or any other coding projects) for a while, so while I know what is basically wrong with the code, I've been having a hard time finding exactly where the vector is going out of range. I've been running gdb on it…
Anonymous
  • 43
  • 6
0
votes
0 answers

Make Heap using Sift Up

I have a few questions regarding binary heaps. I'm trying to understand the "sift up" method to create the heap, and it's time complexity of Theta (n log n). void MakeHeap(T[1..n]) { for (i=2; i<=n; i++) sift_up(T[1..n], i); …
yoad w
  • 315
  • 3
  • 10
0
votes
1 answer

Access Violation error when I debug my priority queue program

I am getting a access violation error in my code when I try run it. The program is a priority queue that inserts a value and prints the heap after each insertion and min extract. Header File: #pragma once /* Header file for the priority queue…
0
votes
2 answers

Is it possible to implement a min-priority queue with a heap, or is binary heap needed?

For my assignment, I have been working on a satnav system and I am using an adjacency list to store all of the mapping data. I therefor want to implement dijkstras algorithm for my path planning functions but I need to first implement a min-priority…
0
votes
0 answers

infinite loop in siftDown method for a heap using linked nodes

In my siftDown method for my heap, I'm getting an infinite loop I believe that is being caused by my if(current.isLeaf()) line. Can anyone help me so that I can get Node n down to where it becomes a leaf or when it stops being smaller than its…
asdf
  • 37
  • 5
0
votes
1 answer

What is the logic behind inserting values into binary heaps (when build one)?

I have a midterm exam next week and am having difficulty drawing out a binary heap. The invariant for a minimum binary heap is: the value of the item stored at a parent node is always less than (greater than) the values of the items stored at its…
thatsnifty
  • 191
  • 4
  • 14
0
votes
1 answer

Insert the keys 2 4 5 1 3 6 in that order into a heap. Draw the result

I am working with heaps and I am unsure if this is the right category to post this question in, but the question is: Insert the keys 2 4 5 1 3 6 in that order into a heap. Draw the result. I have draw the heap (I have attached the image), but I am…
Piqka
  • 43
  • 7
0
votes
2 answers

Heapsort Within a Given Range

I am trying to write a Heapsort method that only performs the sort within a given range passed into the method. The ranges low and high are passed in and these values correspond to values inside the heap, not indices of the heap. For example, the…
user5910724
0
votes
0 answers

How would you ,using a heap of lists, find the shortest heap

That probably was not the best description for it. My question is using a heap of lists how would you find the shortest out of the multiple heaps and then add the next element to it? Is this easy? I am not looking for the code for it. Really looking…
programmer 20
  • 59
  • 1
  • 6
0
votes
1 answer

Implementation of Heap (ADT) using Array Vs. LinkedList

In my data-structure course, I need to implement a binary heap with the following time - complexity requirements: Find Max - O(1) Insert - O(lg n) Delete Max - O(lg n) Now I thought to implement this using an array in the following way: The root…
Noam
  • 1,640
  • 4
  • 26
  • 55
0
votes
1 answer

Min-Max heap delete max element

I'm confused about the final image after a delete-max operation. When 87 is deleted does 48 get brought to the spot that 87 once held? Does the rest of the tree not change after? Min-max heap original
user5476593
0
votes
1 answer

recursive function for upheap method(how can i write this non-recursive method into recursive)

protected void up-heap(int j) { while (j > 1) { // continue until reaching root (or break) int p = parent(j); if (compare(heap.get(j), heap.get(p)) >= 1) break; // heap property verified swap(j, p); j = p; …
saif
  • 11
  • 1
  • 3
0
votes
1 answer

Program throwing an exception after I check for the exception

I must be missing something very simple because this is blowing my mind! I am trying to implement a Heap using a CompleteBinaryTree (which is implemented using an array). This CompleteBinaryTree is an array of Position where each Position holds…
KOB
  • 4,084
  • 9
  • 44
  • 88
0
votes
1 answer

Updating Heap in both direction (Prim's Algorithm)

In Prim's algorithm, it is recommended to maintain the invariant in the following way : When a vertice v is added to the MST: For each edge (v,w) in the unexplored tree: 1. Delete w from the min heap. 2. Recompute the key[w] (i.e.…
Mayur Kulkarni
  • 1,306
  • 10
  • 28