数据结构与程序设计(王丽苹)11-递归.ppt
《数据结构与程序设计(王丽苹)11-递归.ppt》由会员分享,可在线阅读,更多相关《数据结构与程序设计(王丽苹)11-递归.ppt(40页珍藏版)》请在三一办公上搜索。
1、5/27/2023,数据结构与程序设计,1,数据结构与程序设计(11),王丽苹,5/27/2023,数据结构与程序设计,2,第五章 递归,What is recursion?The method in which a problem is solved by reducing it to smaller cases of the same problem.,5/27/2023,数据结构与程序设计,3,Stack frames for subprograms,P158 Figure 5.1,5/27/2023,数据结构与程序设计,4,Tree of subprogram calls,P159 F
2、igure 5.2,5/27/2023,数据结构与程序设计,5,Tree-Diagram Definitions,The circles in a tree diagram are called vertices or nodes.(节点)The top of the tree is called its root.(根)The vertices immediately below a given vertex are called the children of that vertex.(孩子)The(unique)vertex immediately above a given verte
3、x is called its parent.(The root is the only vertex in the tree that has no parent.)(父亲),5/27/2023,数据结构与程序设计,6,Tree-Diagram Definitions,A vertex with no children is called a leaf or an external vertex.(叶子)The line connecting a vertex with one immediately above or below is called a branch.(分支)Sibling
4、s are vertices with the same parent.(兄弟),5/27/2023,数据结构与程序设计,7,Tree-Diagram Definitions,Two 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.The height of a tre
5、e is the number of vertices on a longest possible path from the root to a leaf.(Hence a tree containing only one vertex has height 1.)The depth or level of a vertex is the number of branches on a path from the root to the vertex.,5/27/2023,数据结构与程序设计,8,Two parts of recursion,A recursive definition ha
6、s two parts:the base case-a stopping conditionthe recursive step-an expression of the computation or definition in terms of itself,5/27/2023,数据结构与程序设计,9,General format forMany Recursive Functions,if(some easily-solved condition)/base case solution statement else/general case recursive function call,
7、5/27/2023,数据结构与程序设计,10,递归的使用,在以下三种情况下,常常用到递归方法。定义是递归的 数据结构是递归的 问题的解法是递归的,5/27/2023,数据结构与程序设计,11,定义是递归的,P161目录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
8、us“closer”to the base case of Factorial(0).,5/27/2023,数据结构与程序设计,12,A recursive definition,int factorial(int n)/*Pre:The parameter n is a nonnegative integer.Post:The function returns the nth Fibonacci number.*/if(n=0)return 1;else return n*factorial(n-1);,5/27/2023,数据结构与程序设计,13,A recursive definitio
9、n,void main()int n=0;coutn;coutn!is:factorial(n)endl;,5/27/2023,数据结构与程序设计,14,求解阶乘 4!的过程,5/27/2023,数据结构与程序设计,15,斐波那契数列 P178,5/27/2023,数据结构与程序设计,16,斐波那契数列,int fibonacci(int n)/*fibonacci:recursive versionPre:The parameter n is a nonnegative integer.Post:The function returns the nth Fibonacci number.*/
10、if(n=0)return 0;else if(n=1)return 1;else return fibonacci(n-1)+fibonacci(n-2);,5/27/2023,数据结构与程序设计,17,斐波那契数列的递归调用树,5/27/2023,数据结构与程序设计,18,斐波那契数列,目录Fibonacci下例程,5/27/2023,数据结构与程序设计,19,数据结构是递归的,搜索链表最后一个结点并打印其数值template void Find(ListNode*f)if(f link=NULL)cout f data endl;else Find(f link);,例如,单链表结构,5
11、/27/2023,数据结构与程序设计,20,在链表中寻找等于给定值的结点并打印其数值template void Print(ListNode*f)if(f!=NULL)if(f data=x)cout fdata endl;else Print(flink);,5/27/2023,数据结构与程序设计,21,问题的解法是递归的 例如,汉诺塔(Tower of Hanoi)问题,5/27/2023,数据结构与程序设计,22,汉诺塔,void move(int count,int start,int finish,int temp)if(count 0)move(count-1,start,temp
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 程序设计 王丽苹 11 递归
链接地址:https://www.31ppt.com/p-4980173.html