Question

In: Computer Science

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 in class.)

Solutions

Expert Solution

Algorithm to get all keys less than x:

The binary heap representation I have used to explain is an array based min binary heap,

For an index i,  

a. The left child is given by index 2*i + 1,

b. The right child is given by index 2*i + 2

For a min-Heap each node has lesser key than both its child, unless its a leaf node.

The min-heap array is represented by heapArr

We use a queue, which is initially empty.

Then index 0 is pushed in the queue.

While queue is not empty

i = front of the queue

pop the queue

if i >= heapSize ( the number of elements in heap )

continue;

if heapArr[ i ] is less than x ( the integer input ) then,

cout << heapArr[ i ]; ( print it or store it in a new vector )

push the left and right child indexes in queue ( push 2*i + 1 and 2*i + 2 in queue ).

Finally after the queue becomes empty, all keys less than x have been printed.

Time Complexity of the algorithm is O( k ), where k is number of keys in the heap less than x.


Related Solutions

Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
USE Coral Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order.Ex: If the input is 6, the output is:011(6 in binary is 110; the algorithm outputs the bits in reverse).
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
In Java  Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order.Ex: If the input is:6the output is:0116 in binary is 110; the algorithm outputs the bits in reverse.
[C] Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
6.19 LAB: Convert to binary - functionsWrite a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string.Ex: If the input is:6the output is:110Your program must define and call the following two functions. The IntegerToReverseBinary function...
(Python) Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
In Python8.21 Program: Convert to binary. Sections 2.7, 3.8, 5.2. Functions.Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x // 2Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string.Your program must define and call the following two functions. The function integer_to_reverse_binary() should return...
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.
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i ← 1 to n do S ← S + i * i return S a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm? e. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency class. If you cannot do it, try...
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
C programming problem I have to design an efficient algorithm which solves this problem. Also, it...
C programming problem I have to design an efficient algorithm which solves this problem. Also, it needs to include time and space complexities of this algorithm. There is a matrix (N times N) of integers which rows and columns are sorted in non-decreasing order. A sorted matrix and integer M will be given and we need to find the position(row,column) of M in this matrix. it's okay to report only one position if there are more than one answers. when...
An algorithm is classically defined as a finite series of steps which solves a problem. What...
An algorithm is classically defined as a finite series of steps which solves a problem. What are some types of instructions which occur in everyday life which would qualify as an algorithm? What are some everyday types of instruction which would NOT qualify as an algorithm
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT