Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 01-18-2002, 11:15 AM   #1
aym
Registered User
 
aym's Avatar
 
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.
aym is offline   Reply With Quote
Old 01-19-2002, 12:02 PM   #2
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
No one
aym is offline   Reply With Quote
Old 01-19-2002, 12:48 PM   #3
Member (13 bit)
 
DrZaius's Avatar
 
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);
}
The important part of this function is in the while loop. What it basically does is it goes through every object in the first directory you called and asks, "Is this a file or another folder?" If it is a file the it prints the name of that file. If the object is another folder, then it calls the same function on that specific folder so that it can print the files in that folder. What it's doing is splitting up the work that needs to be done. Think of it this way, printFolder is a function and the only thing it can do is print the name of a file in a specific directory, or create a clone of itself. Here is a sample directory structure:
Code:
[Main]
hello.txt
+- [Foo]
   foo.txt
   +- [Bar]
      bar.txt
If I call printFiles(Main), the functon will first ask, is "hello.txt" a file or a folder? Since it is a file it will print "hello.txt" Then it will ask "Is Foo a file or a folder?" Since it is a folder, it will stop what it is doing and call a clone of itself to "take of care of" the foo folder, which in turn will repeat the same process. And since the [Foo] directory contains another sub-directory the function will call yet another clone of the printFiles function to print all the contents of that directory.

I hope that makes some sense, if not I'll try to explain it better.
__________________
"A witty saying proves nothing." - Voltaire
DrZaius is offline   Reply With Quote
Old 01-19-2002, 02:10 PM   #4
aym
Registered User
 
aym's Avatar
 
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.
aym is offline   Reply With Quote
Old 01-19-2002, 02:23 PM   #5
Member (13 bit)
 
DrZaius's Avatar
 
Join Date: Jul 2000
Location: Fullerton, CA
Posts: 7,030
Quote:
Thanks DrZ! I understand them very well, I'm not a newbie in programming, but I needed some examples for teaching purposes.
Ohh, sorry then, I though you needed an explanation. Hope it helps anyway.
DrZaius is offline   Reply With Quote
Old 01-19-2002, 08:54 PM   #6
Member (12 bit)
 
Paul Victorey's Avatar
 
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();
}
I needed, due to how I was building tables, to do some processing before any child nodes were processed, and some after all the node's children were done, hence the BeforeChildren() and 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.
Paul Victorey is offline   Reply With Quote
Old 01-21-2002, 07:16 AM   #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:
                ****************
        ********        ********
    ****    ****    ****    ****
  **  **  **  **  **  **  **  **
 * * * * * * * * * * * * * * * *
It's the flag of the recursive states of computania. Unfortunately it's the flag of the iterative states of computania, too... Thus they have all sorts of troubles together because each of them blaims the other to have the flag stolen.

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
Felix is offline   Reply With Quote
Old 01-21-2002, 07:47 AM   #8
SQL nutcase
 
mosquito's Avatar
 
Join Date: Sep 2000
Location: Belgium
Posts: 1,136
Send a message via AIM to mosquito
which code does not cause stack overflows.
mosquito is offline   Reply With Quote
Old 01-21-2002, 08:49 AM   #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.
Felix is offline   Reply With Quote
Old 01-21-2002, 10:23 AM   #10
SQL nutcase
 
mosquito's Avatar
 
Join Date: Sep 2000
Location: Belgium
Posts: 1,136
Send a message via AIM to mosquito
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.
mosquito is offline   Reply With Quote
Old 01-21-2002, 11:57 AM   #11
aym
Registered User
 
aym's Avatar
 
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);
    }
}
aym is offline   Reply With Quote
Old 01-25-2002, 01:54 PM   #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
Demosthenes is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:50 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2