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.
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 SICP2011-12-12 21:40
>>1
1. Learn how tail call elimination works.
2. Perform it in reverse.
Name:
Anonymous2011-12-12 22:26
Tail call elimination isn't possible in the JVM.
Name:
Anonymous2011-12-12 22:36
try using some python. it's really awesome for learning about recursion
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.
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:
Anonymous2011-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);
}
}
>>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.