Question: Convert reheapUp (bubbleUp) Heap algorithm from iterative to recursive I need a working recursive version of this reheapUp algorithm, which is used when something is

Convert reheapUp (bubbleUp) Heap algorithm from iterative to recursive

I need a working recursive version of this reheapUp algorithm, which is used when something is enqueued into the heap. Here is the iterative version of the algorithm:

private void reheapUp(T element) // Current lastIndex position is empty. // Inserts element into the tree and ensures shape and order properties. { int 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 } 

Every attempt i've made has resulted in stack overflow. thanks in advance!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!