Recursion
View Code
See also Tracing Recursion.
Recursion
- Recursion
- the process of defining something with iteself as the definition
- This is a bit tough to grasp, but it is like defining a word with
the word in it.
- Here is an example of a recursive definition. A class is A) a student
or B) a student and another class.
- See with this definition you can have another amount of students in a
class. You can have a studnet and a class, but a class can be a student,
and another class, etc.
- Recursive Function
- a function defined recursively by making a call to iteself
- This is the use of recursion. Some function can be defined more
easily recursively. This can be easily seen with searching, sorting
functions, and data strucures.
- Recursive functions are called in a stack. The function makes a call
to itself many times, but will evaluate the calls backwards when the
last one is called. See the example code.
- It is better to return values directly than hold variables for them
since more memory is taken up.
- Recursive functions can easily be defined iteratively, and they are
often tail end recursive.
- Delimiter
- the condition that haults recursive calls of a function
- Tail End Recursion
- recursion has only lines of code created in the calls
- Another tricky concept of recursion better seen in
code.
- In general tail end recursion is better than non tail end, since
compilers translate them iteratively, which has less memory overhang.
- Divide and Conquer
- recursion in which the calls are spreading to multiple calls
- Maze Searching
- a problem in which a person has to find the exit of a maze which is
easily solved with recursion
- Fractals
- a picture defined recursively
- These are pictures often when you you take a close up then they look
the same. Think of a picture within a picture within a picture...
- Fractals often a used to simulate natural terrains.
- Towers of Hanoi
- a common recursive example
- Let me elaborate, ahem... One day some monks at Hanoi asked God when
the world will end. Then God gave them a puzzle. It was three poles
with 64 concentric disks on one pole. The monks were to move all the
disks to the oposite pole, with only moving one disk at a time, and
never having a larger disk on a smaller one. When the puzzle is solved
then the world will end. Don't worry. Moving one disk to a pole in one
second, it will take about a few million years to completely solved.
- Now this puzzle can easily be solved recursivly. You just move N disks
to the middle pole. Move the Nth disk to the third pole, then move all
the other disks to the third pole. Simple.
See also Tracing Recursion.
Prev --
Back to Portal --
Next