|
-
March 7th, 2010, 02:02 AM
#1
How divide and conquer algorithms are implemented
Hi,
I was studying recursion in c++ recently. Although I understood the simple single-recursion as calculating factorial, etc, easily, but was unable to understand double or multiple recursion as in case of binary search, merge sort, binary search tree 's insert,inorder,preorder functions. I just couldn't get how programmer thought of using recursion in first place and how they guaranteed its success. To concentrate on topic I choose only this simple example of printing elements of binary search tree in inorder:
Code:
void inorder(node *temp){
if(temp!=NULL){
inorder(temp->left);
cout << temp->data << " ";
inorder(temp->right);
}
Now precisely I can't understand two things:
1.How programmer thought in which order and what next case he should take for recursion.
2.How he guarantees that it will always print element in correct order.
Please help me in understanding these two issues. Thanks in advance and please forgive me if this post in wrong section( I posted here as I am a newbie in C++).
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|