Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Thinking recursively :(

Name: Anonymous 2011-12-12 20:55

I need some help thinking recursively. I've never had to use this board before, but it's finals week and I'm desperate to get this project done so I can start studying. I had to modify several parts of a java heap program, but one part that's stumping me is converting one function with a while loop into a recursive function. Here's the code:

<code>
   public void trickleUp(int index)
   {
      int parent = (index-1) / 2;
      Node bottom = heapArray[index];

      while( index > 0 && heapArray[parent].getKey() > bottom.getKey() )
         {
         heapArray[index] = heapArray[parent];  // move it down
         index = parent;
         parent = (parent-1) / 2;
         }  // end while
      heapArray[index] = bottom;
   }  // end trickleUp()
</code>

How the hell do I convert this into a recursive function with a void function? My professor said the interface of the method should remain the same.

Name: Anonymous 2011-12-13 6:23

Download the book titled The Little Schemer and finish the first two chapters (at least). The stated goal of the book is to teach you to think recursively, and it does a damn fine job, provided you don't just try to simply read through the book - you need to think about the answer to each question it asks, and try coding the answers0. The fact the book uses the Scheme language might be unappealing if your project is in Java, but the concepts you'll learn can be used in any language.


0: Use http://repl.it, click languages button, select "Scheme" to test it out.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List