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

Pages: 1-

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-12 21:26


main:
  call main
  leave
  ret



recursion at its best

Name: Anonymous 2011-12-12 21:29

Java + recursion, that's retarded.
btw return type is irrelevant, just make it stop recursing(is that a even a word?) once it reaches bottom
void nigger(int woot)
{
if(woot!=endOfthatShit)nigger();
}

Name: Read SICP 2011-12-12 21:40

>>1
1. Learn how tail call elimination works.
2. Perform it in reverse.

Name: Anonymous 2011-12-12 22:26

Tail call elimination isn't possible in the JVM.

Name: Anonymous 2011-12-12 22:36

try using some python. it's really awesome for learning about recursion

Name: >>4 2011-12-12 22:50

Tail call elimination isn't possible in the JVM.
facepalm.jpg

Name: Anonymous 2011-12-12 23:27

>>7
try for yourself, JVM doesn't optimize tail recursion

Name: Anonymous 2011-12-12 23:39

>>4
Java
No point.

Name: Anonymous 2011-12-13 1:00

more reason to hate JVM.

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.

Name: Anonymous 2011-12-13 6:39

>>6

oh you

Name: Anonymous 2011-12-13 8:58

Download the book titled The Little Autist and finish the first two volumes (at least until Knuth goes senile and invents his own assembler). The stated goal of the book is to teach you to think iteratively, 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 answers. The fact the book uses the C language might be unappealing if your project is in Java, but the concepts you'll learn can be used in any language.
Use http://codepad.org/, select "C" , click RUN button, to test it out.

Name: Anonymous 2011-12-13 9:32

I normally wouldn't do this, but it's finals week here too and I'm feeling generous. Here you go, anon. Good luck with your finals.

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

   // This only works for a maxheap. Use < for a minheap
   if (index > 0 && top.getKey() > bottom.getKey())
   {
      swap(index, parent);
      trickleUp(parent);
   }
}

Name: Anonymous 2011-12-13 9:46

>>14
>Only works for maxheap minheap.

FTFY

Name: Anonymous 2011-12-13 10:07

Dont listen to >>11

The objectively best book on Scheme is Teach Yourself Scheme in Fixnum Days.

Name: Anonymous 2011-12-13 14:44

(define (recur x) (recur x))

Name: Anonymous 2011-12-13 18:53

>>14
THANK YOU SO MUCH! I almost didn't bother checking back but you just blew my fucking mind. Over 9000 internets for you, good sir! I'm back to studying.

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