Data Structure - recursive stack

1. The basic idea of ​​recursion

  • The so-called recursive, is to have to go back there.
  • The basic idea of ​​recursion, is the larger question, broken down into multiple smaller sub-problems to solve, and each sub-problem and can continue to be split into multiple smaller sub-problems.
  • The most important thing is to assume that the child has been resolved, now based on sub-problem has been resolved to solve the current problem; or that the child must first solve the problem, and then based on sub-problems to solve current problems.
  • Or it can be understood: the recursive solution is to have more problems rely order relationship.

We assume an abstract question has two time elements: start processing, the process ends. So the question is recursive processing sequence, the first start of the process, and finally to the end of the process.
Assuming that dependency problem as follows:

  • [A] ---- ---- depend> [B] ---- ---- depend> [C]
  • Our ultimate goal is to solve the problem A,
  • The process sequence following three questions:
  • Began to deal with the problem A;
  • Since A depends B, the problems begin processing B;
  • Since B depends C, problems begin processing C;
  • The end of the deal C;
  • End deal with the problem B;
  • A. End deal

2. From the look of generalized recursive function calls

  • For software, the function call relationship is a generalized recursive process, as follows,
func_A()
{
    func_B();
}
func_B()
{
    func_C();
}
func_C()
{
    /////
}
  • Call the function A;
  • Call the function B;
  • Call the function C;
  • Function returns C;
  • B function returns;
  • A function returns;

3. Narrow recursive function

  • There is a special case, the problem is to deal with A / B / C method is the same, which is to produce a "recursive functions" narrow sense, i.e., the function calls function itself.
  • From the above analysis, the recursive processing order of the problem, is to follow the post-first-out (that is, the issue before the start of the final end) of the law.
  • After first-out? Stack!
  • Yes, the treatment of generalized recursive problem, we need to use the stack to solve. The classic example is the function call, it is to rely on the stack to achieve .

4. A non-recursive function recursive

  • Let us now look at a narrow depth analysis of recursive function (ie, function calls itself).
  • We know that there is a recursive function biggest problem is that when recursion is large enough, can cause stack overflow and crash function, a function of the stack size is generally a fixed value, for linux is usually the default is 8M.
  • Therefore, the program will teach us older drivers may not use a recursive function! But the code to achieve recursive function is really simple ah, not to use? Chenqie do ah!
  • So the question is, all non-recursive recursive functions can do? The answer is yes.
  • In essence, for the same problem, if must use the generalized recursive program to deal with, then narrowly recursive function is just one of an implementation, if we give up the narrow recursive function, we had to use an extra data structure : stack .
  • It seems, by all means use the stack, or just let the compiler to maintain a stack ( function stack ), or let the dog to maintain a program stack ( data stack ).
  • The two stacks differences are as follows:

  • For example: non-recursive binary tree traversal

The non-recursive algorithm for recursive

  • Since the data stack recursive algorithm can be used to perform non-recursive, non-recursive algorithm is then achieved by means of data stack, in theory, it can also be recursive. In other words, both are reversible, the bridge is the stack.

6.C language often say "local variables allocate space on the stack," then there is the relationship between this place stacks and stack data structure it?

  • Program "function call stack" is an application stack data structure
  • Function call stack is generally from higher to lower growth in the address of
  • At the bottom of the stack to address high memory
  • At the top of the stack to address low memory
  • Function call stack stored data activity records ( activity log is a serial record relevant information function call ).

7. function call

8. The program stack

  • Stack space program can be seen as a sequence of stack of applications , to access the program stack space is carried out by a function call, the program stack space still comply with the rules of LIFO
  • Stack holds a required maintenance function call information
    • Teach function parameters, function return address
    • Local variables
    • The number of call context painting
  • What is the program stack overflow ?
    • 在不断的压栈过程中造成栈空间耗尽而产生栈溢出
    • 栈溢出常由于函数递归过深或局部数组过大造成

9.以下三种情况常常用到递归方法:

  • 递归定义的数学函数
    • 阶乘函数:                   
    • 二阶Fibonaci数列:   
  • 具有递归特性的数据结构:
    • 树 :                   
    • 广义表:                        A=(a,A)
  • 可递归求解的问题:
    • 迷宫问题 Hanoi塔问题

10.用分治法求解递归问题

  • 分治法:对于一个较为复杂的问题,能够分解成几个相对简单的且解法相同或类似的子问题来求解。
  • 必备的三个条件:
    • 1、能将一个问题转变成一个新问题,而新问题与原问题的解法相同或类同,不同的仅是处理的对象,且这些处理对象是变化有规律的
    • 2、可以通过上述转化而使问题简化
    • 3、必须有一个明确的递归出口,或称递归的边界。
  • 分治法求解递归问题算法的一般形式:      
  • void   p (参数表) {        
    • if   (递归结束条件)可直接求解步骤;-----基本项        
    • else  p(较小的参数);------归纳项        

}

Guess you like

Origin blog.csdn.net/qq_22847457/article/details/94389271