Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
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