Thoughts on key points of understanding and designing Recursion

Source: Internet
Author: User

I think there are several key points in understanding recursion and writing the correct recursive program:

1. grasp the problem as a whole

The difficulty of recursion is that the human brain is not suitable for tracking the process of self-calling itself in recursion, because the human brain does not have a stack that can be remembered like a computer,

However, the difference is that people will generalize, while computers only know the call-return method.

Therefore, to understand and design recursion, we must grasp it from the whole. mathematical induction and Recursion are a symmetric relationship. mathematical induction constantly extends itself, while recursion is constantly decomposed.

Yourself. In recursion, the delivery is to break down the main problem into sub-problems. Instead, it is the process of gradually solving the sub-problems.

The key point is to make good use of the assumptions in mathematical induction.That is, if the subproblem has been solved, what is the result of the subproblem? Once you think so, you need to grasp the problem as a whole without having to tangle with details. When designing a recursive function, first design an algorithm framework like a common function to control the program logic. When processing recursive calls, assume that you are calling another function, this function has been designed. You only need to know what he is doing, and then return something. There seems to be some contradiction in how to do it without thinking about it, because this function is itself, why don't you need to think about it? In fact, this is why recursion is hard to understand. Therefore, you must first put the details aside, first determine the framework, and then process the details.

2. Pay attention to what is the return value of the function, and how to use the return value of the subfunction call to obtain the return value of the call.

After the framework is designed in the first article, we need to pay attention to the details of function processing, including the process Branch and the return value of the function.

The return value of a function is directly related to whether the function is correctly executed, because the return value of a function is what your recursive subcall returns, and the return value of a recursive subcall affects the final result,

Therefore, you must pay attention to the return of the function. The result returned by the subroutine is used by the caller, And the caller returns the result. Therefore, there is a problem: the functionReturns the consistency.

Generally, a complex recursive function design involves many logical branches. The return of these logical branches must be consistent, that is, the solution of the functions under different circumstances.

This is prone to errors. The principle of recursion is based on subproblems. subproblems are just a small parent problem, because we assume that subproblems can be solved, the solution of the parent problem is composed of the solution of the Child problem, so the parent problem and the child problem should solve the same problem, and their results should be consistent.

Use examples to describe:

This is a python-written BST deletion function:

Main framework of the bstremove function:

1 If the subtree is none, return the subtree

2. If the value is smaller than the key, execute bstremove in the left subtree and return the subtree

3. If the value is greater than the key, execute bstremove in the right subtree and return the subtree

4. If it is equal to the key, execute the deletion algorithm and return the subtree.

The main framework has a total of four branches, and determines the specific branch based on the value of the subtree.

This function has two parameters: subtree and target. The function starts searching for and deleting a node with a value equal to the target from the subtree node, and returns the root node.

The key point is what is the root node returned by the function:

This root node is the root node of A subtree. This subtree has already executed bstremove, and its structure may have changed. The root node may have been replaced.

Subtree. Left = self. _ bstremove (subtree. Left, target) re-pays the root node to subtree. Left. It is easy to understand this.

The purpose of the function is to delete the node of a tree and return the root of the tree.

The consistency of the returned values here is that you should always return the root of the entire tree.

Then let's take a closer look at each branch. Their return values all follow this point. Assuming that the sub-problem has been solved, the root node is obtained, and then how can we solve the master problem, then return the root node of the current tree.

After paying attention to this point, we can understand how recursion gets N from n-1 like mathematical induction.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.