473,513 Members | 2,563 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Book for recursion.....

AmberJain
884 Recognized Expert Contributor
HELLO,

I studied recursion in C programming. But I still think that I'm unable to grab the concept of recursion. OR better said I cannot get recursion inside my mind from the book I'm referring to. So please tell me a book which will make me feel more comfortable with concept of recursion in programming.

THANKS.......
Jul 12 '08 #1
2 2375
JosAH
11,448 Recognized Expert MVP
Recursion as programming knows it is all about divide and conquer. Briefly stated
if a problem is too complex you chop it up in smaller instances of itself, fiddle
a bit with the results and you're ready. If the program instance is very simple there
is no need to chop it up but you solve it directly. This is the 'sentinel' condition.

A canonical example is the binary tree; you can recursively define a binary tree
as either nothing at all (the sentinel condition) or a node having two binary trees
as its two children.

Given this simple recursive definition a lot of problems are just corollaries thereof.
e.g. the number of nodes in a tree either equals zero (the sentinel condition) or
one plus the number of nodes of both of the children. In C this translates easily to:

Expand|Select|Wrap|Line Numbers
  1. int nof_nodes(tree_t* node) {
  2.    if (node == NULL)
  3.       return 0;
  4.    return 1+nof_nodes(node->left)+nof_nodes(node->right);
  5. }
  6.  
Another example: the depth of a tree is the length of the longest path from the
root of the tree to a leaf node; this translates to C as:

Expand|Select|Wrap|Line Numbers
  1. int depth(tree_t* node) {
  2.    if (node == NULL)
  3.       return 0;
  4.    return 1+max(depth(node->left), depth(node->right));
  5. }
  6.  
Note the similarity of both functions. There is a simple rule of thumb: the depth
of the stack must not exceed O(n) where n is the size of the problem. For nicely
balanced trees this rule is true. Not so with a recursive definition of another
problem: the factorial of a number either equals one if n equals 0 (the sentinel
condition) or the product of n times the factorial of n-1. This translates to C as:

Expand|Select|Wrap|Line Numbers
  1. int fac(int n) {
  2.    if (n == 0)
  3.       return 1;
  4.    return n*fac(n-1);
  5. }
  6.  
The depth of the stack will aways be O(n) and this function is better expressed
in an iterative way:

Expand|Select|Wrap|Line Numbers
  1. int fac(int n) {
  2.    int result= 1;
  3.    for (; n > 0; result*= n--);
  4.    return result;
  5.  
Although this code looks more 'mechanical' it doesn't use any stack at all for its
calculations. This rule of thumb doesn't work for functions with two or more
variables; a famous example is the Ackermann function:

Expand|Select|Wrap|Line Numbers
  1. int act(int n, int m) {
  2.    if (n == 0) 
  3.       return m+1;
  4.    if (m == 0)
  5.       return ack(n-1, 1);
  6.    return ack(n-1, ack(n, m-1));
  7. }
  8.  
This little monster uses a horrible amount of stack space and it can't be written
in an iterative way without using any form of a stack. A closed form of this function
does exist but it needs the 'Knuth operator' or similar to be expressed in this
closed form. A bit of theory on recursive functions and computability as well as
Turing decidability can be found here.

kind regards,

Jos
Jul 12 '08 #2
AmberJain
884 Recognized Expert Contributor
THANK YOU ................................................
Jul 13 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
3402
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
12
2737
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
3
1758
by: dave | last post by:
ISBN 1-861000-88-X I'm confused about these references to the usage of recursion pertaining to example Ex6_08A: On page 236 in the last sentence of the 3rd paragraph "Since any set of...
14
3493
by: __frank__ | last post by:
What do you think about the following book: "C How to Program", 4th edition, ISBN: 0131426443 I'm a novice. Thanks in advance.
4
3747
by: utab | last post by:
Dear all, I have been searching for an Algorithm book which groups common kinds (such as: recursion is most useful in these kinds of problems, sth like that ) of problems and explains them with...
1
1510
by: shahoo | last post by:
Hi, Does anyone know a good book (or tutorial) on Crystal Reports in C# ? Thanks in advance.
1
1177
by: Alan T | last post by:
I want to learn ASP.NET using C# (not VB.NET). Any beginner book worth to buy? I am application developer using Delphi so will stick with that for desktop development.
20
2959
by: athar.mirchi | last post by:
..plz define it.
35
4684
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7380
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.