Almost every article about recursion includes examples with factorial or Fibonacci numbers which are a) math b) useless in real life. Are there some interesting non-math code examples to teach recursion? I'm thinking divide-and-conquer algorithms but they usually involve complex data structures.
|
|
Directory / File structures are the best example of a use for recursion, because everyone understands them before you start, but anything involving tree-like structures will do.
|
|||||||||||||||||||||
|
|
QuickSort would be the first one that jumps to mind. Binary search also is a recursive problem. Beyond that there are whole classes of problems that solutions fall out almost for free when you start working with recursion. |
|||||||||
|
|
Towers of Hanoi is a good one to help learn recursion. There are many solutions to it on the web in many different languages. |
|||||
|
|
A Palindrome Detector: Start with a string : "ABCDEEDCBA" If starting & ending characters are equal, then recurse and check "BCDEEDCB", etc... |
|||||||||||||
|
|
Look for things that involve tree structures. A tree is relatively easy to grasp, and the beauty of a recursive solution becomes apparent far sooner than with linear data structures such as lists. Things to think about:
These are all related to actual real-world scenarios, and they can all be used in applications of real-world significance. |
||||
|
|
|
A binary search algorithm sounds like what you want. |
||||
|
|
|
Here are some more practical problems that come to my mind:
|
||||
|
|
|
http://stackoverflow.com/questions/105838/real-world-examples-of-recursion
http://stackoverflow.com/questions/2085834/how-did-you-practically-use-recursion
http://stackoverflow.com/questions/126756/examples-of-recursive-functions
|
|||||||||||||||||
|
|
Sort, defined recursively in Python.
Merge, defined recursively.
Linear search, defined recursively.
Binary search, defined recursively.
|
||||
|
|
|
In functional programming languages, when no higher-order functions are available, recursion is used instead of imperative loops in order to avoid mutable state. F# is an impure functional language which allows both styles so I will compare both here. The following sum all the numbers in a list. Imperative Loop with Mutable Variable
Recursive Loop with No Mutable State
Note that this kind of aggregation is captured in the higher order function |
||||
|
|
|
I've used recursion heavily in game playing AI. Writing in C++, I made use of a series of about 7 functions that call each other in order (with the first function having an option to bypass all of those and call instead a chain of 2 more functions). The final function in either chain called the first function again until the remaining depth I wanted to search went to 0, in which case the final function would call my evaluation function and return the score of the position. The multiple functions allowed me to easily branch based on either player decisions or random events in the game. I'd make use of pass-by-reference whenever I could, because I was passing around very large data structures, but because of how the game was structured, it was very difficult to have an "undo move" in my search, so I'd use pass-by-value in some functions to keep my original data unchanged. Because of this, switching to a loop-based approach instead of a recursive approach proved too difficult. You can see a very basic outline of this sort of program, see https://secure.wikimedia.org/wikipedia/en/wiki/Minimax#Pseudocode |
||||
|
|
|
Another real world recursion problem that students may relate to is to build their own web crawler that pulls information from a website and follows all the links within that site (and all the links from those links, etc). |
|||||
|
|
A really good real life example in business is something called a "Bill of Materials". This is the data that represents all of the components that make up a finished product. For example, let's use a Bicycle. A Bicycle has handlebars, wheels, a frame, etc. And each of those components can have sub-components. for example the Wheel can have Spokes, a valve stem, etc. So typically these are represented in a tree structure. Now to query any aggregate information about the BOM or to change elements in a BOM often times you resort to recursion.
And a sample recursive call...
Obviously the BomPart Class would have many many more fields. You may need to figure out how many plastic components you have, how much labor it takes to build a complete part, etc. All this comes back to the usefulness of Recursion on a tree structure though. |
||||
|
|
|
Family relations make for good examples because everybody understands them intuitively:
|
||||
|
|
|
I solved a problem with a knight pattern (on a chessboard) using a recursive program. You were supposed to move the knight around so that it touched every square except a few marked squares. You simply:
Many kinds of "think-ahead" scenarios can be worked by testing future possibilities in a tree like this. |
||||
|
|
|
In a sense, recursion is all about divide and conquer solutions, that is breking the problem space into a smaller one to help find the solution for a simple problem, and then usualy going back reconstructing the original problem to compose the right answer. Some examples not involving math to teach recursion (at least those problems I remember from my university years): These are examples of using Backtracking to solve the problem. Other problems are classics of Artificial Intelligence domain: Using Depth First Search, pathfinding, planning. All those problems involve some kind of "complex" data structure, but if you don't want to teach it with math (numbers) then your choices may be more limited. Yoy may want to start teaching with a basic data structure, like a linked List. For example representing the natural numbers using a List: 0 = empty list 1 = list with one node. 2 = list with 2 nodes. ... then define the sum of two numbers in terms of this data structure like this: Empty + N = N Node(X) + N = Node(X + N) |
||||
|
|
|
A rather useless yet showing recursion inner working well is recursive
No math - a very simple function. Of course you don't implement it recursively in real life, but it's a good demo of recursion. |
||||
|
|