Simple question: Just looking for a good book or article on recursion. I know there are hundreds out there, but just want your opinion on an article you read about recursion and were like, "Wow, that makes a lot of sense" and it cleared up any confusion you had about recursion or helped you think recursively.
I understand what recursion is, but have trouble thinking recursively. Any help would be greatly appreciated.
Node* findNode( Node* node, int target ) // look for 'target' in the given 'node'
{
if(!node) // if this node is null, then we can't find the target, so return null to indicate failure
returnnullptr;
// otherwise... is this the correct node?
if(target == node->value)
return node; // if yes, then we found our target, so return this node.
// At this point... this is not the correct node.. so we need to search other nodes.
// But how can we do that?
// Well... we're ALREADY doing it... that's what this 'findNode' function is. So let's just
// use recursion to call it again!
// The only difference is... rather than searching the top of the tree, we're searching this
// node's children.
// if the target is less than this value, then the target must be in a left node
if(target < node->value)
return findNode(node->left, target); // search left node for the target
elsereturn findNode(node->right, target); // otherwise, search the right node
}
Could you give me clues on how to solve this problem recursively? I really don't want someone to solve it for me, but maybe give me a hint on how to think to solve this problem...
Write a recursive function that finds and returns the minimum element in an array, where the array and its size are given as parameters.
1 2
//return the minimum element in a[]
int findmin(int a[], int n)
#include <iostream>
usingnamespace std;
int findmin(int ar[], int size);
int main()
{
int myArray[5] = { 5, 2, 4, 3, 6 };
cout << findmin(myArray, 5);
return 0;
}
int findmin(int ar[], int size)
{
if(size <=0)
return ar[size]; // I'm assuming this is the only thing you can do at this point.
// Maybe do some sort of relational expression here?
if(findmin(ar, size-1) > something)
// do something.
// I'm really just guessing at this point, intuition is just compelling me to think this way; any tips would be appreciated.
}
- The recursive approach is to have a function which does 1 thing... then calls itself if it needs to do that thing multiple times.
- If you have an array with one value... then that value is the smallest value in the array.
- If you have more than one value, then you compare values with the < operator to see which is smaller.
The recursive mindset for this problem:
Which is smaller? The value I'm currently looking at? Or the smallest value in the rest of the array?
EDIT:
Also... just FYI, this is wrong:
1 2
if(size <=0)
return ar[size]; // wrong wrong
If you have an array with a size of 0... then it has no elements. So ar[0] is going to be out of bounds.
EDIT 2:
I also hate how recursion is taught so early on. It's scarcely useful and is very difficult for beginners to wrap their head around. It should be taught way way later... if at all.
I was trying to think how to guide you to a solution but it basically gives you the code in a more confusing way...so here is some code that does what you want. Hope it helps you get you hear around recession.
______________________________________________________
edit:
this may be of interest:
What on Earth is Recursion? - Computerphile
https://www.youtube.com/watch?v=Mv9NEXX1VHc
But, one of the operands to < operator is a value ar[size-1] and one of them is a recursive function call findmin(ar, size-1). So logically, if the ar[size-1] is less than whatever is returned from the recursive function call, you're going to want to return ar[size-1]. But what if the recursive function call returns something smaller than ar[size-1]? How do I return a recursive function call?
I understand what you're trying to get at, but don't understand how to syntactically do it. Unless I'm storing the return value of the recursive function call in a variable and comparing it to ar[size-1] and then just returning the variable?