Recursion: Difference between revisions

From Computer Science Wiki
(Created page with "right|frame|Programming basics<ref>http://www.flaticon.com/</ref> Recursion in computer science is a method where the solution to a problem depends on sol...")
 
(6 intermediate revisions by the same user not shown)
Line 5: Line 5:
A simple definition: Recursion is the process of a subroutine calling itself.
A simple definition: Recursion is the process of a subroutine calling itself.


== Example ==


== Image of a tree ==  
<syntaxhighlight lang=python>
def factorial(n):
    if n == 1:
        return 1
    else:
        return n * factorial(n-1)
</syntaxhighlight>


== Recursion ==


[[File:Binary tree.svg.png]]


== tree vocabulary ==
<html>
<iframe width="560" height="315" src="https://www.youtube.com/embed/t4MSwiqfLaY" frameborder="0" allowfullscreen></iframe>
</html>


In addition to NORMAL tree vocabulary:
== Another look at recursion ==


* root node
<html>
* parent node
<iframe width="560" height="315" src="https://www.youtube.com/embed/VrrnjYgDBEk" frameborder="0" allowfullscreen></iframe>
* child node
</html>
* leaf node


Binary Trees have special vocabulary:
* left-child
* right-child
* subtree
== Practical applications of a tree ==
* Trees can be used to store data that has an inherent hierarchical structure. For example, an operating system may use a tree for directories, files and folders in its file management system.
* They are dynamic, which means that it is easy to add and delete nodes.
* They are easy to search and sort using standard traversal algorithms.
* They can be used to process the syntax of statements in natural and programming languages so are commonly used when compiling programming code.
== Binary Tree - video example ==
[https://www.youtube.com/watch?v=H5JubkIy_p8  This video provides a basic introduction to binary trees.]
== Traversal ==
Traversal describes the order in which nodes are visited.  I used this image with great gratitude from the guys at Dartford Grammar School<ref>http://ib.compscihub.net/wp-content/uploads/2015/04/5.1.16.pdf</ref>
<br />
<br />
[[File:Binary tree traversal.png]]
== Applications of different methods of traversals ==
I used these definition from wikipedia <ref>https://en.wikipedia.org/wiki/Tree_traversal#Applications</ref>
* Pre-order traversal while duplicating nodes and edges can make a complete duplicate of a binary tree. It can also be used to make a prefix expression (Polish notation) from expression trees: traverse the expression tree pre-orderly.
* In-order traversal is very commonly used on binary search trees because it returns values from the underlying set in order, according to the comparator that set up the binary search tree (hence the name).
* Post-order traversal while deleting or freeing nodes and values can delete or free an entire binary tree. It can also generate a postfix representation of a binary tree.


== Standards ==  
== Standards ==  
Line 61: Line 38:


* [[Abstract data structures]]
* [[Abstract data structures]]
== External Links ==
[http://cslibrary.stanford.edu/110/BinaryTrees.html high level discussion of binary trees]


== References ==
== References ==

Revision as of 15:24, 6 March 2018

Programming basics[1]

Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem (as opposed to iteration). The approach can be applied to many types of problems, and recursion is one of the central ideas of computer science [2]

A simple definition: Recursion is the process of a subroutine calling itself.

Example[edit]

def factorial(n):
    if n == 1:
        return 1
    else:
        return n * factorial(n-1)

Recursion[edit]

Another look at recursion[edit]


Standards[edit]

  • Identify a situation that requires the use of recursive thinking.
  • Identify recursive thinking in a specified problem solution.
  • Trace a recursive algorithm to express a solution to a problem.

See Also[edit]

References[edit]