Data structures - stack, recursion, recursive call stack

Find the box problem:

Solution 1


I. Create a heap of boxes to look for.
II. Take a box from the box pile and look inside.
III. If a box is found, it is added to the box pile so that it can be searched later.
IV. If you find the key, you're done!
V. Go back to step II.

 The first method uses a while loop: as long as the stack of boxes is not empty, take a box from it and search it carefully.

 Solution 2


I. Check everything in the box.
II. If it is a box, go back to the first step.
III. If it's a key, you're done!

 Small example of recursion
 The second method uses recursion - the function calls itself, the pseudo code of this method is as follows.

 

 The connotation of recursive thinking
There is going (passing) and returning (returning).
 "You go" means: the recursive problem must be decomposed into several smaller-scale sub-problems with the same form as the original problem, and these sub-problems can be solved with the same problem-solving ideas  "You go" means:
these The evolution process of the problem is a process from big to small, from near to far, and there will be a clear end point (critical point). Once this critical point is reached, there is no need to go to a smaller and farther place . Finally, starting from this critical point, the original path returns to the origin, and the original problem is solved.


More directly, the basic idea of ​​recursion is to convert large-scale problems into small-scale similar sub-problems to solve.

In particular, when the function is implemented, because the method to solve the big problem and the method to solve the small problem are often the same method, so the function calls itself, which is the definition of recursion.
It is extremely important that the problem-solving function must have a clear end condition, otherwise it will lead to infinite recursion. In particular, when the function is implemented, because the method to solve the big problem and the method to solve the small problem are often the same method, so the function calls itself, which is the definition of recursion.
It is extremely important that the problem-solving function must have a clear end condition, otherwise it will lead to infinite recursion.

Baseline Conditions and Recursion Conditions
 Since recursive functions call themselves, it is easy to make mistakes while writing such functions, which in turn leads to infinite recursion.
 For example, to write a function that counts down like below.


 This can be written recursively:

 

 If you run the above code, you will find a problem: this function runs endlessly! (To stop the script from running, press Ctrl+C.)

 

 Baseline Conditions and Recursion Conditions
 When writing a recursive function, you must tell it when to stop recursing.
 Because of this, every recursive function has two parts:
 Base case and recursive case.
 A recursive condition refers to the function calling itself.
Baseline condition means that the function no longer calls itself, thus avoiding forming an infinite loop.

Let's add a baseline condition and a recursive condition to the function countdown.

  Now, the function will behave as expected.

1. Clarify the recursion termination condition;
2. Give the processing method when the recursion terminates;
3. Extract the repeated logic and reduce the scale of the problem.

the stack


This section will introduce an important programming concept - the call stack (callstack).
The call stack is very important to programming, and this concept must be understood when using recursion.

 


Let's say you go to a barbecue and you create a to-do list for it - a stack of sticky notes.
A stack of sticky notes is much simpler: Inserted to-dos go to the top of the list; when reading to-dos, you just read the top one, and delete it. So this todo list has only two operations: push (insert) and pop (delete and read).

 How to use this to-do list?

 This data structure is called a stack. A stack is a simple data structure.
 We have been using it just now.

 

call stack

Call Stack
 The stack used inside the computer is called the call stack.


How to call the stack?

  is the stack in the computer that stores messages about running subroutines.
How does the computer call the stack?
 Here is a simple function:

 This function greets the user and then calls two other functions. The code for these two functions is as follows.

Explanation: In Python, print is a function, but for simplicity, it is assumed here that it is not a function.

The following details what happens when the function is called.
Suppose you call greet("maggie"), the computer will first allocate a block of memory for the function call.

 

The variable name is set to maggie and stored in memory.

 

Whenever you call a function, the computer stores the values ​​of all the variables involved in the function call into memory like this.
Next, print hello, maggie! .
Call greet2("maggie") again. Likewise, the computer allocates a block of memory for this function call.

 Computers use a stack to represent these blocks of memory.
Wherein the second memory block is located above the first memory block. (the second called exists above the first)
prints how are you, maggie?, then returns from the function call. At this point, the memory block at the top of the stack is popped .

 


Now, the block of memory at the top of the stack belongs to the function greet, which means you returned to the function greet.
When the function greet2 is called, the function greet is only partially executed. That is, when another function is called, the current function is suspended and left in an outstanding state. The values ​​of all variables of the function are still in memory. After executing the function greet2, you return to the function greet, and continue to execute from where you left off:
first print getting ready to say bye..., and then call the function bye.

The memory block of the function bye is added at the top of the stack. Then, print ok bye! and return from this function.

 

Now you are back to the function greet. Since there is nothing else to do, we return from the function greet.

This stack is used to store variables of multiple functions and is called the call stack.

recursive call stack


Recursive functions also use the call stack.
Let's take a look at factorial: a call stack for a recursive function.
Recursive function to compute factorial:

 

 

 

 

 Recursive call stack
Note that each fact call has its own x variable. You cannot access the x variable from one function call to another.
Stack plays an important role in recursion.

Summarize


 Recursion refers to calling your own function.
 Each recursive function has two conditions: the baseline condition and the recursive condition.
The stack has two operations: push and pop.
 All function calls go into the call stack.
 The call stack can be very long, which will take up a lot of memory.

 

Guess you like

Origin blog.csdn.net/jcandzero/article/details/126895956