Question

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

Homework Answers

Answer #1

Do a preorder traversal of the heap.

At each step,

1.if the element looked at has value ≥ m, do not explore that subtree any further;

2.otherwise print the element and continue the preorder traversal in that subtree

See the code snippet for only printing small elements than x (Assuming Min Heap and it is already created),

Complexity is O(k) 
--> doing a preorder traversal so that we will be avoiding elements greater than required element

void MinHeap::printSmallerNodes(int x, int pos = 0){
   if (pos >= heap_size) // if position is > heap size its invalid so return
      return; 
   if (arr[pos] >= x) { // if value arr[pos] > x that means we are traversing element that is greater than our required so neglect it do return
      return;
   }
   cout<<arr[pos]<<" "; // print current element (it prints is ascending order as we are starting from 0 position that is top node of min heap that is the least of array given
   printSmallerNodes(x, left(pos)); // span left
   printSmallerNodes(x, right(pos)); // span right
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Design a recursive divide-and-conquer algorithm A(n) that takes an integer input n ≥ 0, and returns...
Design a recursive divide-and-conquer algorithm A(n) that takes an integer input n ≥ 0, and returns the total number of 1’s in n’s binary representation. Note that the input is n, not its binary representation. For example, A(9) should return 2 as 9’s binary representation is 1001, while A(7) should return 3 since 7 is 111 in binary. Note that your algorithm should have a running time of O(log n). Justify your answer. You need to do the following: (1)...
In this problem your task is to find a missing number. The input will always consist...
In this problem your task is to find a missing number. The input will always consist of an array of n positive integers such that the difference between every two consecutive numbers is a fixed constant but one integer is missing. See below for two example inputs/outputs: Input sequence: [0, 2, 4, 6, 10] Output: missing number is 8 Input sequence: [1, 4, 7, 13, 16] Output: missing number is 10 Note that in the first example the constant c...
Design a recursive algorithm with proofs of the following: Richest Heritage: Input: A binary tree T...
Design a recursive algorithm with proofs of the following: Richest Heritage: Input: A binary tree T in which each node x contains a field worth[x], which is a (positive, zero, or negative) monetary value expressed as a real number. Define (monetary) heritageof a node x to be the total worth of ancestors of x minus the total worth of proper descendants of x. Output: A node x in T with maximum heritage Note: other than the field worthand left/right child...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT