版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、11/23/2021数据结构与程序设计 1第五章 递归nWhat is recursion?nThe method in which a problem is solved by reducing it to smaller cases of the same problem.11/23/2021数据结构与程序设计 2Stack frames for subprogramsnP158 Figure 5.111/23/2021数据结构与程序设计 3Tree of subprogram callsnP159 Figure 5.211/23/2021数据结构与程序设计 4Tree-Diagram D
2、efinitionsnThe circles in a tree diagram are called vertices or nodes.(节点)nThe top of the tree is called its root.(根)nThe vertices immediately below a given vertex are called the children of that vertex.(孩子)nThe (unique) vertex immediately above a given vertex is called its parent. (The root is the
3、only vertex in the tree that has no parent.)(父亲)11/23/2021数据结构与程序设计 5Tree-Diagram DefinitionsnA vertex with no children is called a leaf or an external vertex.(叶子)nThe line connecting a vertex with one immediately above or below is called a branch.(分支)nSiblings are vertices with the same parent.(兄弟)
4、11/23/2021数据结构与程序设计 6Tree-Diagram DefinitionsnTwo branches of a tree are adjacent if the lower vertex of the first branch is the upper vertex of the second. A sequence of branches in which each is adjacent to its successor is called a path.nThe height of a tree is the number of vertices on a longest
5、 possible path from the root to a leaf. (Hence a tree containing only one vertex has height 1.)nThe depth or level of a vertex is the number of branches on a path from the root to the vertex.11/23/2021数据结构与程序设计 7Two parts of recursionA recursive definition has two parts:nthe base case - a stopping c
6、onditionnthe recursive step - an expression of the computation or definition in terms of itself11/23/2021数据结构与程序设计 8General format forMany Recursive Functions if (some easily-solved condition) / base case solution statement else / general case recursive function call 11/23/2021数据结构与程序设计 911/23/2021数
7、据结构与程序设计 10nP161n目录Factorial下例程The value of Factorial(n) can be written as n * the product of the numbers from (n - 1) to 1, that is, n * (n - 1) * . . . * 1 or, n * Factorial(n - 1) And notice that the recursive call Factorial(n - 1) gets us “closer” to the base case of Factorial(0). 11/23/2021数据结构
8、与程序设计 11A recursive definitionnint factorial(int n)n/* nPre: The parameter n is a nonnegative integer.nPost: The function returns the nth Fibonacci number.n*/nn if (n = 0) n return 1;n else n return n*factorial(n-1);n11/23/2021数据结构与程序设计 12A recursive definitionnvoid main()nint n=0;ncoutendlPlease in
9、put an integer:n;ncoutn! is: factorial(n)endl;n11/23/2021数据结构与程序设计 13求解阶乘 4! 的过程11/23/2021数据结构与程序设计 14斐波那契数列斐波那契数列 P1781),2() 1(0,1,)(nnFibnFibnnnFib11/23/2021数据结构与程序设计 15斐波那契数列斐波那契数列int fibonacci(int n)/* fibonacci: recursive versionPre: The parameter n is a nonnegative integer.Post: The function r
10、eturns the nth Fibonacci number.*/ if (n = 0) return 0; else if (n = 1) return 1; else return fibonacci(n - 1) + fibonacci(n - 2);11/23/2021数据结构与程序设计 16斐波那契数列的递归调用树斐波那契数列的递归调用树11/23/2021数据结构与程序设计 17斐波那契数列斐波那契数列n目录Fibonacci下例程11/23/2021数据结构与程序设计 18搜索链表最后一个结点并打印其数值搜索链表最后一个结点并打印其数值template void Find (
11、ListNode *f ) if ( f link = NULL ) cout f data endl; else Find ( f link );11/23/2021数据结构与程序设计 19在链表中寻找等于给定值的结点并打印其数值template void Print ( ListNode *f ) if ( f != NULL) if ( f data = x ) cout fdata 0) n move(count - 1, start, temp, finish);n cout Move disk count from startn to finish . 0) nmove2(coun
12、t - 1, start, temp, finish); ncout Move disk count from startn to finish . 0) n move(count - 1, start, temp, finish);n cout Move disk count from startn to finish . 0) nmove(count - 1, start, temp, finish); ncout Move disk count from startn to finish . endl;ncount-; nswap = start;nstart = temp;ntemp
13、= swap;nn11/23/2021数据结构与程序设计 33Hanoi Without Tail Recursionn目录Hanoi2下例程11/23/2021数据结构与程序设计 34IterativenBy reading the recursion tree from bottom to top instead of top to bottom, we immediately obtain the iterative program from the recursive one.11/23/2021数据结构与程序设计 35Factorial Without Tail Recursionn
14、int factorial(int n)nn int count, product = 1;n for (count = 1; count = n; count+)n product *= count;n return product;n11/23/2021数据结构与程序设计 36Factorial Without Tail Recursion 目录Factorial2下例程11/23/2021数据结构与程序设计 37Fibonacci Without Tail Recursionnint fibonacci(int n)n/* fibonacci : iterative versionnPr
15、e: The parametern is a nonnegative integer.nPost: The function returns then th Fibonacci number. */nn int last_but_one; / second previous Fibonacci number,Fi-2n int last_value; / previous Fibonacci number,Fi-1n int current; / current Fibonacci numberFin if (n = 0) return 0;n else if (n = 1) return 1
16、;n else n last_but_one = 0;n last_value = 1;n for (int i = 2; i = n; i+) n current = last_but_one + last_value;n last_but_one = last_value;n last_value = current;n n return current;n n11/23/2021数据结构与程序设计 38Fibonacci Without Tail Recursionn目录Fibonacci2下例程11/23/2021数据结构与程序设计 39Recursion(递归) or Iteration(循环)?nrecursion occurs when
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年大学(护理学)精神科护理学阶段测试题及答案
- 2025年高职建筑工程运营(运营技术)试题及答案
- 2025年大学大一(化学工程)无机化学基础阶段测试题及答案
- 2025年高职物流服务与管理(物流成本控制)试题及答案
- 2025年大学航空技术(航空概论基础)试题及答案
- 2025年高职(生物质能应用技术)生物质发电技术阶段测试试题及答案
- 2025年大学建筑结构(建筑结构基础)试题及答案
- 2025年大学二年级(金融学)货币银行学基础试题及答案
- 2026年贵阳职业技术学院高职单招职业适应性考试模拟试题带答案解析
- 2026年黑龙江冰雪体育职业学院高职单招职业适应性测试备考题库带答案解析
- (高清版)WST 442-2024 临床实验室生物安全指南
- 2019译林版高中英语全七册单词总表
- 黄河知识考试题库300题(含答案)
- 医院院内交流与协作制度
- 华住会酒店员工手册
- 正畸保持阶段知情同意书
- 国开计算机应用基础(本)形考学习过程表现
- 部编版九年级道德与法治上册《维护祖国统一》教案及教学反思
- 线路金具出厂检验报告
- 行政组织学简答题论述题
- GB/T 7354-2018高电压试验技术局部放电测量
评论
0/150
提交评论