I need this method written in a recursive version. I will provide algorithm and
ID: 3567439 • Letter: I
Question
I need this method written in a recursive version.
I will provide algorithm and code.use variables from this method only.
reheapUp(element)
Set hole to lastIndex
while (the hole is not the root ) AND (element > the hole's parent)
Swap hole with hole's parent
Set hole to element
here's the code for reheapUp method
private void reheapUp(T element)
//current lastIndex position is "empty"
//Inserts element into the tree and ensures shape and order properties.
{
in t hole = lastIndex;
while ((hole > 0) //hole is not root and element > hole's parent
&& (element.compareTo(elements.get((hole - 1) / 2)) > 0))
{
//move hole's parent down and then move hole up
elements.set(hole,elements.get((hole - 1)/2));
hole = (hole - 1) / 2;
}
elements.set(hole, element) ; //place element into final hole
}
Explanation / Answer
private void reheapUp(T element)
{
int static hole = lastIndex;
if(!((hole > 0) //hole is not root and element > hole's parent
&& (element.compareTo(elements.get((hole - 1) / 2)) > 0)))
{
elements.set(hole, element) ; //place element into final hole
return;
}
//move hole's parent down and then move hole up
elements.set(hole,elements.get((hole - 1)/2));
hole = (hole - 1) / 2;
reheapUp(element);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.