|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
Recursive Programming
I need non-mathematical examples of recursive programming, I'll program them in java, but I prefer examples that can be programmed in any language.
Thanks in advance. |
|
|
|
|
|
#2 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
No one
|
|
|
|
|
|
#3 |
|
Member (13 bit)
Join Date: Jul 2000
Location: Fullerton, CA
Posts: 7,030
|
Hi aym_7,
Well, the most popular recursion problem is to computer factorials, but since you said non-math examples, here is a different one. Imagine that on your hard drive you can have types of object, folders and files. Each folder can only contain more folder and files. Now, say you want to go through the drive and print out the name of every file. Here is some pseudo-code that shows how recursion works: Code:
function printFiles (name_of_folder)
{
open (name_of_folder);
while (there are objects in the folder)
{
if (object is a file)
print (file's name);
else if (object is a folder)
printFiles (folder's name);
}
close (name_of_folder);
}
Code:
[Main]
hello.txt
+- [Foo]
foo.txt
+- [Bar]
bar.txt
I hope that makes some sense, if not I'll try to explain it better.
__________________
"A witty saying proves nothing." - Voltaire |
|
|
|
|
|
#4 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
Thanks DrZ! I understand them very well, I'm not a newbie in programming, but I needed some examples for teaching purposes.
|
|
|
|
|
|
#5 | |
|
Member (13 bit)
Join Date: Jul 2000
Location: Fullerton, CA
Posts: 7,030
|
Quote:
|
|
|
|
|
|
|
#6 |
|
Member (12 bit)
Join Date: Mar 1999
Location: MN or WI
Posts: 3,017
|
I think, as Dr Zaius's example illustrates, anything which natively has a "tree" structure lends itself to recursion. The windows file system (for that manner, ANY file system that supports directories), the windows registry, etc. are good recursive choices.
I used recursion last to take information which I was gathering in a tree window (which was the most intuitive interface for the task) and build arrays of objects, to save to disk for import into another app. Pseudo-code looked somthing like this: Code:
void TreeRecursion::Recurse(TreeNode * n){
BeforeChildren();
TreeNode * nextNode;
for (nextNode = n.GetFirstChild();nextNode != NULL;nextNode=n.GetNextSibling()){
Recurse(n);
}
AfterChildren();
}
__________________
Paul M. Victorey ------------------ I am not responsible for any problems that may arise as a result of following my advice. This includes, but is not limited to, computer failure, loss of data, nuclear war, famine, boils, no clean laundry, your daughter running off with a biker gang, or armageddon. Take my advice at your own risk. |
|
|
|
|
|
#7 |
|
Member (10 bit)
Join Date: Mar 1999
Location: Zurich, Switzerland
Posts: 797
|
Hi there,
I'll add an example which I read once in a campus booklet my older brother brought home. Look at the picture: Code:
****************
******** ********
**** **** **** ****
** ** ** ** ** ** ** **
* * * * * * * * * * * * * * * *
Finally they agreed to make a contest. Each country builds a team of programmers. Each team works and codes and tries hard to make a short, smooth program that builds up the flag of the recursive states of computania... ummm... or was it the iterative states? Now, go and try to code both of them, the recursive and the iterative version. Which code is smaller? Which looks smoother? Which is easier to understand? What other things do you note? Hope that helps Felix |
|
|
|
|
|
#8 |
|
SQL nutcase
|
which code does not cause stack overflows.
|
|
|
|
|
|
#9 |
|
Member (10 bit)
Join Date: Mar 1999
Location: Zurich, Switzerland
Posts: 797
|
of course, but let me add two comments:
1) stack overflows is not an issue with the 5-lines flag shown above. ![]() 2) stack overflow is always an issue with recursive programming. |
|
|
|
|
|
#10 |
|
SQL nutcase
|
But the checks to prevent stack overflow are usually omitted. That's what I find in code reviews.
Another real life example for recursive programming is Currency rate triangulation over time, but that sucks so much you don't even want to know how it works.
|
|
|
|
|
|
#11 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
Thank you Felix for the example, I found another good one, it's a puzzle from the Far East:
We have 3 sticks and a set of discs (of different sizes), these discs are placed in the form of a tower around the first stick, and we have to move them to the third stick (using the second stick), and there are two rules: 1. We can move only one disc at a time. 2. The discs on each stick should always look like a tower. The solution of this puzzle can be programmed in a recursive method: Code:
//n is the number of discs around the SourceStick
//I will refer to the sticks by numbers (1,2,3)
void moveDiscs(int n, int SourceStick, int TempStick, int TargetStick) {
if (n == 1)
println("Move from " + SourceStick + " to " + TargetStick);
else {
moveDiscs(n - 1, SourceStick, TargetStick, TempStick);
println("Move from " + SourceStick + " to " + TargetStick);
moveDiscs(n - 1, TempStick, SourceStick, TargetStick);
}
}
|
|
|
|
|
|
#12 |
|
Member (10 bit)
Join Date: Mar 1999
Location: Random
Posts: 997
|
The good old Towers of Hanoi. It is the definitive example of stacks, recursion, induction, etc. It is very versatile. There is a good story that goes with it, too. The Monks of some monastery had a whole room with 63 (or whatever) giant gold rings and three pegs. They believed that when someone moved the entire stack from one peg to another, the world would be destroyed. Calculate how many steps this would require.
Respectfully, Demosthenes |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|