Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Give an implementation of the HeapPriorityQueues downheap method that uses recursion (and no loop). java. protected void downheap(int j) { while (hasLeft(j)) { int leftIndex
Give an implementation of the HeapPriorityQueues downheap method that uses recursion (and no loop). java.
protected void downheap(int j) {
while (hasLeft(j)) {
int leftIndex = left(j);
int smallChildIndex = leftIndex;
if (hasRight(j)) {
int rightIndex = right(j);
if (compare(heap.get(leftIndex), heap.get(rightIndex)) > 0)
smallChildIndex = rightIndex;
}
if (compare(heap.get(smallChildIndex), heap.get(j)) >= 0)
break;
swap(j, smallChildIndex);
j = smallChildIndex;
}
}
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