Question

In: Computer Science

One way to remove an object from a binary min heap is to decrease its priority...

One way to remove an object from a binary min heap is to decrease its priority value by 1 and then call deleteMin. An alternative is to remove it from the heap, thus creating a hole, and then repair the heap.

Write pseudocode for an algorithm that performs the remove operation using the alternative approach described above. Your pseudocode should implement the method call remove (int index), where index is the index into the heap array for the object to be removed. Your pseudocode can call the following methods described in lecture: insert, deleteMin, percolateUp, and percolateDown. Like in lecture, you may assume that objects are just priority integers (no other data).

What is the worst-case complexity of the algorithm you wrote?

Solutions

Expert Solution

Pseudocode for Remove operation in heap (min-heap) :

  • 1. Find the index i of the object in the heap array that we want to delete. This i is given in the argument of the Remove(int index) method.
  • 2. Swap this object at index i with the last object of the heap array at the nth index. [ Swapping will fill in the hole created on deleting the required object in the heap, and make it a complete binary tree again but not necessarily a heap now ]
  • 3. Call percolateUp() or percolateDown() method to fix the heap property and make this complete binary tree back to a heap
  • if( object (i) < parent(i) ) call percolateUp()
  • else call percolateDown()

Time Analysis of Remove operation in heap :

  • Running time of Remove(int index) in a heap of n objects = height of the heap tree
  • = O(log n) in the worst-case
  • The worst-case time complexity of the above algorithm depends upon the height of the heap tree because we are using percolate up/down methods which is O(logn) and the rest of the operations like swaps and comparisons will take constant time only.

Feel free to ask any doubt you may have. Happy to help.


Related Solutions

Language C++ Implement a Priority Queue with a Binary HEAP. Use a Max Heap. Create a...
Language C++ Implement a Priority Queue with a Binary HEAP. Use a Max Heap. Create a class called Node: Have a Name and Priority.Data set - 10 is the highest priority, 1 is lowest priority. Enqueue and dequeue in the following order. Function  Name, Priority Enqueue  Joe, 3 Enqueue  Fred,1 Enqueue Tuyet,9 Enqueue  Jose, 6 Dequeue Enqueue  Jing, 2 Enqueue  Xi, 5 Enqueue  Moe, 3 DequeueEnqueue  Miko, 7 Enqueue Vlady, 8 Enqueue Frank, 9 Enqueue  Anny, 3 DequeueEnqueue  Xi, 2 Enqueue  Wali, 2 Enqueue  xChe, 6 Enqueue  xVerra, 8 Dequeue Dequeue Dequeue Dequeue...
Building a Priority Queue in C++ using a min-heap. Not using C++ Standard Template Library. Trying...
Building a Priority Queue in C++ using a min-heap. Not using C++ Standard Template Library. Trying to understand how this works. Thank you
Suppose H is a binary min-heap with n nodes, Prove the following facts: Any algorithm that...
Suppose H is a binary min-heap with n nodes, Prove the following facts: Any algorithm that must nd the maximum key of the heap must search all of the leaf nodes.
Let's suppose that you have a binary min-heap with S elements. Please write an an algorithm...
Let's suppose that you have a binary min-heap with S elements. Please write an an algorithm that will find all the elements that are in the heap that are smaller than a given value X. Please make sure that the time complexity of the algorithm that you propose has to be O(K), where is the number of elements smaller than X that is in the heap. Please type answer if you can
One way to remove magnesium (Mg2+) from water is to precipitate it with a strong base...
One way to remove magnesium (Mg2+) from water is to precipitate it with a strong base such as sodium hydroxide (NaOH). Assuming the following unbalanced reaction describes the process. (a) Calculate the concentration of NaOH (in mg/L) needed to completely remove 20 mg/L of Mg2+. (b) Calculate the mass (in lb) of solid Mg(OH)2 that will be produced (and must be disposed of) if this reaction is performed in 250,000 gallons of water. MgSO4 + NaOH = Mg(OH)2¯ + Na2SO4
One way to remove harmful “NOx” (NO and NO2) atmospheric pollutants from flue gas is the...
One way to remove harmful “NOx” (NO and NO2) atmospheric pollutants from flue gas is the “thermal deNOx” process, in which NH3 is used to reduce the NO to N2: __4__ NO (g) + __1__ O2 (g) + ____ NH3 (g) → ____ N2 (g) + ____ H2O (g) The research lab has several gas cylinders available for studying this reaction, including one cylinder containing a mixture of 3.0 wt% NO and 15 wt% O2 in an N2 diluent, and...
This all has to be in javascript A common way to remove duplicates from an array...
This all has to be in javascript A common way to remove duplicates from an array is to convert it to a set and then convert it back to an array. We will do that here. Please use these variables and data structures: var input = [3, 4, 2, 2, 4, 5, 3, 1, 3, 6]; var set = new Set(); 1. Get each element from the input array and add it to the set. 2. Get the elements from...
Implement a function to remove a leaf node from a binary search tree. Using the following...
Implement a function to remove a leaf node from a binary search tree. Using the following class and function definition: class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode * remove_leaf(int item, bool &removed) { // implement } The remove function will return the node with the item if it's present in the tree. If the node is a leaf node and is removed by the function,...
Draw each binary tree that is the maximum heap that results from inserting one by one in the order given the values 20, 15, 25, 30, 45, 18, 10, 12, 16.
In Java-Draw each binary tree that is the maximum heap that results from inserting one by one in the order given the values 20, 15, 25, 30, 45, 18, 10, 12, 16. Note that your tree diagram should show the heap after the maximum heap property has been restored. Submit nine diagrams.Next, draw each binary tree that is the maximum heap as each maximum value is deleted from the above tree and after the maximum heap property has been restored....
Choose an object and for that object list one qualitative variable and its subclass (Nominal, Ordinal)...
Choose an object and for that object list one qualitative variable and its subclass (Nominal, Ordinal) and from that very same object share one quantitative and its subclass (Discrete, Continuous). For each one of the two answers give an explanation for each one your examples. Do not use the following items: Computer Clothes Car Bear Cell phone users Finger nail file Pens Weights
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT