Questions tagged [max-heap]

148 questions
0
votes
0 answers

Writing Pseudocode to find smallest element in an array. [verification]

I need a solution verification. I could not find the tag so if anyone could assist me on that. Question is below. Let A [1..n] be a max-heap with n elements. Assuming there are no duplicate keys. Write pseudocode for finding the smallest element…
GiBiT
  • 311
  • 1
  • 8
  • 20
0
votes
1 answer

Insert into heap in at most one swap?

There's this question in the famous book "Introduction to Algorithms" by Udi Manber, which states that Algorithm *Insert_To_Heap* may swap many times up the heap. Modify the algorithm so that at most one swap will be performed. O(log n)…
0
votes
1 answer

Insertions into a max heap with atmost one swap

Is there a algorithm to perform insertions into a heap with atmost one swap (O(log n) comparisons are allowed)
Jay
  • 1
  • 1
0
votes
1 answer

Heap, trickle down method

I am currently doing a max-heap. When I use remove() method, I understand that I would swap with the larger children. What if both of the children have the same priority? for instance Case 1: heap = [5,7,7,16,15] if I remove 5 and replace it with…
Sugihara
  • 1,091
  • 2
  • 20
  • 35
0
votes
1 answer

Is this a valid MaxHeap structure?

I am trying to convert a minHeap class to a maxHeap class. I was given the code for the minHeap class, one method of which was add. It looks like this: while (index > 1 && getParent(index).compareTo(newElement) > 0) The first node is automatically…
Andrew Martin
  • 5,619
  • 10
  • 54
  • 92
0
votes
1 answer

How can I lower the cost of Heapsorts' Maxheap function from 2lg(n) to ~lg(n)? (Java)

Here is the heapsort program so far. public class HeapSort{ private static int[] a; private static int n; private static int left_child; private static int right_child; private static int largest; public static void main(String[] args) { …
JessieM
  • 11
  • 3
-1
votes
1 answer

Why does my recursive code result in a stack overflow error?

This code is given in the book "Introduction to Algorithms". For this I used 1 indexed array #include #include using namespace std; int n=6; int x[1000]; void max_heapify(int a[],int i) { int left=2*i; int…
user466534
-1
votes
1 answer

what are the inputs given to the lambda expression(a,b) while creating a max priority queue in java

Started learning dsa and came across priority queues but I am stuck at lambda expressions in priority queue in java. Map map = new HashMap<>(); PriorityQueue q = new PriorityQueue<>((a,b)->map.get(b)-map.get(a)); In the…
-1
votes
1 answer

Is finding the min/max of BST considered to be O(1) time?

I was building a queue using a BST, but I realized that a min/max heap might be better. However, a BST might work, since if we store a reference to head/tail in the BST, then lookup is very close to O(1)..for example: (5) / \ …
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
-1
votes
1 answer

Heapify method is not working properly for max heap

I am preparing for a data structures and algorithms final exam. I am trying to work through all of the data structures we have learnt this semester and program them by my self to help prepare me for the final. I am working on the max heap right now…
osty2001
  • 1
  • 2
-1
votes
1 answer

how to implement string priority queue with checking message priority

Messenger is used to send or receive text messages. When someone is offline a messenger maintains a buffer of messages which is delivered to the receiver when he gets online. The phenomena take place on simple timestamp phenomena, the message…
-1
votes
1 answer

Is golang container/heap effectively doing heap sort?

Is golang container/heap effectively doing heap sort on an underlying slice? It doesn't look like underlying array is always sorted. type IntHeap []int // ... other interface implementation func main() { a := []int{} h := IntHeap(a) …
Steve
  • 863
  • 3
  • 9
  • 21
-1
votes
1 answer

Max Heap Insert Function Implementation C++

I'm trying to insert key values into heap. I'm using TestUnit.cpp for errors. I got these errors: Assert…
NOBUD
  • 27
  • 7
-1
votes
1 answer

Maxheap giving wrong result

I wrote the following code to build a maxheap from a already existing array the downadjust function makes the array a max heap but it is not producing results as desired Please check the code and tell me where am I going wrong also it would be very…
user16393473
-1
votes
1 answer

max_heapify iterative way to go down the heaps to get the next level nodes

I am trying to write a iterative control loop instead of recursion because it is more efficient can someone tell me if my code makes sense: Recursive version: def max_heapify(array, i): left = 2 * i right = 2 * i + 1 length = len(array) - 1 # for…
Poala
  • 5
  • 2
1 2 3
9
10