Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Present an algorithm that returns the largest k elements in a binary max-heap with n elements in 0(k lg k) time Note that, the complexity
Present an algorithm that returns the largest k elements in a binary max-heap with n elements in 0(k lg k) time Note that, the complexity of your algorithm should not depend on n. Hint 1: Let's consider who are the candidates for the ith largest element. It is easy to see that the root contains the only candidate for the 1st largest element, then who are the candidates for the 2nd largest element after the 1st largest element is determined? Who are the candidates for the 3rd largest element after the 2nd largest element is determined? And so on. Eventually, you will find that there are i candidates for the ith largest element after the (i1)th largest element is determined. Hint 2: How to use some data structure to maintain these candidates so that you can find the maximum among
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started