递归(Recurve)的概念汉诺塔(TowerofHanoi)问题递归过
n n递归递归( (RecurveRecurve) )的概念的概念n n汉诺塔汉诺塔(Tower of Hanoi)(Tower of Hanoi)问题问题n n递归过程与递归工作栈递归过程与递归工作栈n n广义表广义表 (General Lists )(General Lists )第四章第四章 递递 归归Date1递归的概念递归的概念n n递归的定义递归的定义 若一个对象部分地包含它自己若一个对象部分地包含它自己, , 或用它自己给自己定义或用它自己给自己定义, , 则称这个对象是递归则称这个对象是递归 的;若一个过程直接地或间接地调用自己的;若一个过程直接地或间接地调用自己, , 则则 称这个过程是递归的过程。称这个过程是递归的过程。n n在以下三种情况下,常常用到递归方法在以下三种情况下,常常用到递归方法。uu 定义是递归的定义是递归的uu 数据结构是递归的数据结构是递归的uu 问题的解法是递归的问题的解法是递归的Date2定义是递归的定义是递归的? ?求解阶乘函数的递归算法long Factorial ( long n ) if ( n = 0 ) return 1;else return n*Factorial (n-1); 例如,阶乘函数例如,阶乘函数Date3求解阶乘 n! 的过程Date4计算斐波那契数列的函数Fib(n)的定义求解斐波那契数列的递归算法 long Fib ( long n ) if ( n void Hanoi (int n, String A, String B, String C ) /解决汉诺塔问题的算法解决汉诺塔问题的算法if ( n = 1 ) cout >ch;if (ch!=.)reverse;cout = 0 ) cout = 0 ) cout 0 > 0时,时,表的表的第一个表元素第一个表元素称为广义表称为广义表的的表头表头( (headhead) ),除此之外,除此之外,其它表元素组其它表元素组成的成的表表称为广义表的称为广义表的表尾表尾( (tailtail) )。 Date18广义表的特性广义表的特性n n有次序性有次序性n n有长度有长度n n有深度有深度n n可递归可递归n n可共享可共享A = ( ) B = ( 6, 2 ) C = ( a, ( 5, 3, x ) ) D = ( B, C, A ) E = ( B, D ) F = ( 4, F ) Date19各种广义表的示意图各种广义表的示意图Date20广义表的表示广义表的表示只包括整数和字符型数据的广义表链表表示只包括整数和字符型数据的广义表链表表示 表中套表情形下的广义表链表表示表中套表情形下的广义表链表表示Date21广义表结点定义广义表结点定义n n标志域标志域 utypeutype, , 表明结点类型。表明结点类型。0 0为表头结点,为表头结点,1 1 为为 整型原子结点,整型原子结点,2 2为字符型原子结点,为字符型原子结点,3 3为子表结为子表结 点。点。n n值域值域 valuevalue。当当 utypeutype = = 0 0 时为时为表引用计数表引用计数,= = 1 1时时 为为整数值整数值,= = 2 2 时为时为字符值字符值, = = 3 3 时为时为指向子表的指向子表的 表头结点的指针表头结点的指针。n n尾指针域尾指针域 tlinktlink。当当 utypeutype = = 0 0 时为指向该表表头元时为指向该表表头元 素的指针;当素的指针;当 utypeutype 0 0 时为指向同一层下一个表时为指向同一层下一个表 结点的指针。结点的指针。utype = 0/1/2/3value = ref /intgrinfo /charinfo / hlink tlinkDate22广义表的带表头结点的存储表示Date23广义表的类定义广义表的类定义#define HEAD 0 #define INTGR 1 #define CH 2 #define LST 3 class GenList;class GenListNode friend class Genlist; private:int utype;GenListNode * tlink;Date24union int ref; /utype = 0,表头结点int intgrinfo; /utype = 1, 整型 char charinfo; /utype = 2, 字符型GenListNode *hlink; /utype = 3,子表结点 value; public:GenListNode int nodetype ( GenListNode *elem ) return elemutype; void setInfo ( GenListNode *elem,GenListNode ;Date25class GenList private:GenListNode *first;GenListNode* GenList:Copy ( GenListNode*ls );int depth ( GenListNode *ls );int equal ( GenListNode *s, GenListNode *t ); void GenList:Remove (GenListNode *ls ); public:Genlist ( ); GenList ( );GenListNode GenListNode *Tail ( );Date26GenListNode *First ( );GenListNode * Next ( GenListNode *elem );void Push ( GenListNode GenList void setHead ( GenListNode viod setNext ( GenListNode *elem1,GenListNode *elem2 );void setTail ( GenList void Copy ( const GenList int depth ( );int Createlist ( GenListNode *ls, char * s ); Date27广义表的访问算法广义表的访问算法 广义表结点类的存取成员函数 GenListNode pitemutype = elemutype;pitemvalue = elemvalue;return Date28void GenListNode:setInfo(GenListNode *elem,GenListNode elemvalue = xvalue; 广义表类的构造和访问成员函数Genlist:GenList ( ) GenListNode *first = new GenListNode;firstutype = 0; firstref = 0; firsttlink = NULL; /仅建立表头结点 Date29GenListNode exit (1);else GenListNode * temp = new GenListNode;temputype = fristtlinkutype;tempvalue = fristtlinkvalue;return Date30void GenList:Push ( GenListNode else xtlink = firsttlink; firsttlink = x; Date31GenList newlist.firsttlink = Copy ( list.first );xtlink = newlist.fristtlink;newlist.fristtlink = x;return Date32广义表的递归算法广义表的递归算法广义表的复制算法void GenList:Copy ( const GenList GenListNode* GenList:Copy(GenListNode *ls) GenListNode *q = NULL;if ( ls != NULL ) q = new GenListNode; /创建表结点qutype = lsutype; /复制 utypeDate33switch ( lsutype ) case HEAD : qref = lsref; break; case INTGR : qintgrinfo = lsintgrinfo;break; case CH : qcharinfo = lscharinfo;break;case LST : qhlink = Copy ( lshlink );break;qtlink = Copy ( lstlink );return q; 图5.16 广义表ls的链表结构Date34递归顺序递归顺序 s0tlink s1hlink t0tlink t1tlink t2tlink NULL 回退 t2 回退t1回退 t0回退 s1tlink s2hlinku0tlink u1hlink v0tlink v1tlinkv2tlink NULL 回退 v2 回退 v1 回退v0 回退 u1tlink u2tlink NULL 回退u2 回退 u1 回退 u0 回退 s2tlinkNULL 回退 s2 回退 s1回退 s0 回退Date35求广义表的深度例如,对于广义表例如,对于广义表E E ( ( B B ( (a a, , b b), ), D D ( ( B B ( (a a, , b b), ), C C ( (u u, (, (x x, , y y, , z z), ), A A ( ) ) ) ( ) ) ) 按递归算法分析:按递归算法分析:DepthDepth ( (E E) = 1+) = 1+MaxMax DepthDepth ( (B B), ), DepthDepth ( (D D) ) Depth Depth ( (B B) = 1+) = 1+MaxMax DepthDepth ( (a a), ), Depth Depth ( (b b) = 1) = 1Depth Depth ( (D D) = 1+) = 1+MaxMax DepthDepth ( (B B), ), DepthDepth ( (C C), ),DepthDepth ( (A A) )Date36Depth Depth ( (C C) = 1+) = 1+MaxMax DepthDepth ( (u u), ), DepthDepth ( (x x, , y y, , z z) ) Depth Depth ( (A A) = 1) = 1Depth Depth ( (u u) = 0) = 0DepthDepth ( (x x, , y y, , z z) = 1+) = 1+Max Max DepthDepth ( (x x), ), DepthDepth ( (y y), ), DepthDe