当前位置:网站首页>Recursive and non recursive transformation
Recursive and non recursive transformation
2022-06-12 01:13:00 【4nc414g0n】
Recursive and non recursive conversion
recursive
Three kinds of problems solved by recursion:
- Data is defined recursively .(Fibonacci function ,n The factorial )
- The solution of the problem is realized recursively .( to flash back )
- The structural form of data is defined recursively .( Traversal of binary tree , Figure search )
The problem of recursion versus loop iteration :
- Recursive too deep , Cause stack overflow (
Linear recursion)- Performance issues ( Now compiler optimization is getting better and better , This issue has become relatively less important )
Take the Fibonacci series for example
Linear recursion
long long Fib(size_t N) { if(N < 3) return 1; return Fib(N-1) + Fib(N-2); }
Every time you call, you call first Fib(n-1), Call again Fib(n-2), So the first column in the figure is the first to be called , Because space is not cumulative , The space complexity is calculated by the first column with the largest number O(N)Call recursively before calling Fib(n-1) When ,Fib(n-2) Not implemented yet , You need to save the previous state , Therefore, the cost is large
Tail recursion
int Fib(int n, int ret1, int ret2) { if (n == 0) return ret1; else return Fib(n-1, ret2, ret1 + ret2); }
- The criterion of tail recursion is whether the last step of function operation calls itself , Not whether to call itself on the last line of the function
- In general, tail recursion mainly passes the results of the last run through parameters
- Spatial complexity O(1)
The importance of a tail call is that it does not add a new stack frame to the call stack —— It's about updating it , Like iteration . The tail has two characteristics due to successive attribution : Call its own function (Self-called); Computation takes only constant stack space (Stack Space). And formally, as long as it is the last return Statement returns a complete function , It's tail recursion .
Recursive to non recursive
Loop iteration
Tail recursion
Example: data structure ---- Sort MediumMerge sort non recursive solution
Stack or queue + Loop iteration
Linear recursion
Example: data structure ---- Sort MediumQuick sort non recursive solution
边栏推荐
- Explain asynchronous tasks in detail: the task of function calculation triggers de duplication
- Win jar package setting boot auto start
- Set up NFT blind box mall system | customized development of NFT mall software
- Article 6: Design of multi-functional intelligent trunk following control system | undergraduate graduation design - [Key Technology - positioning technology related data (UWB WiFi Bluetooth)]
- C language structure - learning 27
- Equipment encryption of industrial control security
- Lambda intermediate operation distinct
- Lambda创建流
- [n32g457] remote monitoring of graffiti cloud based on RT thread and n32g457
- In depth description of Weibull distribution (2) meaning of parameters and formulas
猜你喜欢

One article to show you how to understand the harmonyos application on the shelves

websocket服务器实战

The annual salary of testers in large factories ranges from 300000 to 8K a month. Roast complained that the salary was too low, but he was ridiculed by netizens?

Online Fox game server - room configuration wizard - component attribute and basic configuration assignment

Creating a flutter high performance rich text editor - rendering

In depth description of Weibull distribution (2) meaning of parameters and formulas

一看就懂的JMeter操作流程
![2022-06-11:注意本文件中,graph不是邻接矩阵的含义,而是一个二部图。 在长度为N的邻接矩阵matrix中,所有的点有N个,matrix[i][j]表示点i到点j的距离或者权重, 而在二部](/img/ab/b3044472561ac366f2cbd878268132.png)
2022-06-11:注意本文件中,graph不是邻接矩阵的含义,而是一个二部图。 在长度为N的邻接矩阵matrix中,所有的点有N个,matrix[i][j]表示点i到点j的距离或者权重, 而在二部

Set up NFT blind box mall system | customized development of NFT mall software

Low code platform design exploration, how to better empower developers
随机推荐
I worked as a software testing engineer in a large factory and wrote "one day's complete workflow"
Dr. wangxiting: understanding interpretable recommendation from the perspective of knowledge map and natural language generation
Chapter V - Fund professional ethics
Elegant throttling / de buffeting decorator under LAYA
Crawler case 05 - parsing websites using XPath
LabVIEW Arduino electronic weighing system (project Part-1)
Virtual human appears on the stage of the Winter Olympic Games, connecting elements of the meta universe
MS-HGAT: 基于记忆增强序列超图注意力网络的信息扩散预测
Lambda中间操作filter
About MySQL password modification failure
C language preprocessing instructions - learning 21
Lambda中间操作distinct
Global and Chinese lutetium oxide powder industry investigation and analysis and Investment Strategy Research Report 2022-2028
Analysis report on demand status and Prospect Forecast of global and Chinese remote control helicopter industry 2022-2028
市场监管总局、国家网信办:开展数据安全管理认证工作
[path of system analysts] summary of real problems of system analysts over the years
A day when the script boy won't be killed
[answer] in the business sequence diagram of XX shopping mall, is it drawn as a business executor (bank) or a business entity (banking system)
Intel trimbert: tailor Bert for trade-offs
Interpretation of the guiding opinions on the digital transformation of banking and insurance industry by Analysys analysis
