Question

In: Computer Science

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

Solutions

Expert Solution

Solution

current_index denotes the index of the element in the min heap we are currently working with.

Start the algorithm from the root node, and initialize current_index = 0

if(current_index != NULL)

{

if(element(current_index)>X)

{

Return,since the element at current_index and all its descendants are skipped as these are greater or equal to X

}

else

{

Print the element at the current_index

Follow the process again with current_index = current_index.leftChildIndex

Follow the process again with current_index = current_index.rightChildIndex

}

}

Logic:

A Min-Heap is a complete binary tree in which the value in each internal node is smaller than or equal to the values in the children of that node. We utilize this property to search for the required elements as follows:

Start testing the elements from the root node of the heap. If root element is >X it means none of the elements of the heap is smaller than X

current_index is 0 , since we are testing the root node. If element at current_index<X, the element is printed and we check the root elements in left and right subtree of current element.

We stop searching descendants of a node as soon as the value of the element is greater than or equal to X.

Since we stop searching descends of the node if the element in node is > or equal to X so the time complexity of the given algorithm is O(k), where k is the number of element smaller than X.


Related Solutions

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.
How to write Prim's Algorithm with min-Heap and adjacency Lists?
How to write Prim's Algorithm with min-Heap and adjacency Lists?
Suppose you have a min-heap in an array a[] where: ● The root isstored at...
Suppose you have a min-heap in an array a[] where: ● The root is stored at index 1 ● There are 15 elements (in a[1]...a[15]) ● There are no duplicates (this is not true in general for heaps; but assume it is true for this problem). Fill in the blanks in the statements below; the resulting statement must be true for any heap obeying the above properties.There are/is at least ____6_______ elements in theheap that are/is larger than a[3] ""There...
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...
Provide an algorithm that solves the following problem: input: a binary heap B, an integer value...
Provide an algorithm that solves the following problem: input: a binary heap B, an integer value x output: all keys in B that are smaller than x Constraint: your algorithm must run in time O(K), where K is the number of keys output. Explain why your algorithm has the required runtime behaviour. (Use pseudocode or C++, or any informal (but clear) description. You are free to choose any kind of representation of binary heaps, as long as it was mentioned...
Can you please explain loop invariant for Heapsort, in which Min-Heapify and Build-Min-Heap are correct
Can you please explain loop invariant for Heapsort, in which Min-Heapify and Build-Min-Heap are correct
write a function that determines if a given vector of integersis a min–heap. By default,...
write a function that determines if a given vector of integers is a min–heap. By default, a vector is asemi–heap satisfying the heap structure propertybut not the heap order property.InputThere is an unknown number of integer data that will be provided from standard input. Each line of input represents the contents of a vector. We do not know how many lines of input there are.1 2 3 4 55 4 3 2 11 9 2 13 10 5 3 15...
C++ Write the code to implement a complete binary heap using an array ( Not a...
C++ Write the code to implement a complete binary heap using an array ( Not a vector ). Code for Max heap. Implement: AddElement, GetMax, HeapSort, ShuffleUp, ShuffleDown, etc Set array size to 31 possible integers. Add 15 elements 1,3,27,22,18,4,11,26,42,19,6,2,15,16,13 Have a default constructor that initializes the array to zeros.. The data in the heap will be double datatype. PART 2 Convert to the program to a template, test with integers, double and char please provide screenshots thank you so...
In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm,...
In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …, n; (4) 50 instances of n random numbers generated in the range of [1..n]. Note:...
Write the binary tree representation for the Binary Search for also 17 elements and give the...
Write the binary tree representation for the Binary Search for also 17 elements and give the worst-case
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT