第6章树与森林 6-1写出用广义表表示法表示的树的类声明,并给出如下成员函数的实现 (1) operator>()接收用广义表表示的树作为输入,建立广义表的存储表示; (2)复制构造函数用另一棵表示为广义表的树初始化一棵树 (3) operator=()测试用广义表表示的两棵树是否相等 (4) operator<<()用广义表的形式输出一棵树 (5)析构函数清除一棵用广义表表示的树。 【解答】 #include <iostream. h> #define max Sub TreeNum 20 最大子树(子表)个数 class EntRee: ∥ Gen tree类的前视声明 class GenTreeNode i ∥广义树结点类的声明 friend class EntRee: private int uty pe, ∥结点标志:=0,数据;=1,子女 Gen TreeNode nextsibl /dtpe=0,指向第一个子女 /dtpe=0,根结点数据 har Childdata: /type=1,子女结点数据 Gen TreeNode *firstchild type=2,指向第一个子女的指针 Gen TreeNode( int tp, char item ) utype(tp), nextSibling (NULL) d if tp==0)RootData=item; else ChildData=item;) ∥构造函数:构造广义表表示的树的数据结点 GenTreeNode( GenTreeNode *son= NULL ) utype(2), nextSibling(NULL), firstChild( son) ∥构造函数:构造广义表表示的树的子树结点 t nodetype ()( return utype; ∥返回结点的数据类型 ∥返回数据结点的值 Gen TreeNode *Get Child (return firstChild; j 历返回子树结点的地址 Gen TreeNode 'GetNsibling(( return nextsibling 返回下一个兄弟结点的地址 void setInfo( char item )i data=item; I /将结点中的值修改为item void set Child( Gen TreeNode *ptr)( firstChild=ptr; 1 将结点中的子树指针修改为pr void setNsinbilg( Gen TreeNode*ptr )inextsibling= ptr; 3 class EntRee i ∥广义树类定义 private Gen TreeNode "first; ∥根指针 char ret value; 建树时的停止输入标志 Gen TreeNode* Copy( Gen TreeNode* ptr ) ∥复制一个p指示的子树 void Traverse( GenListNode"ptr ) /对p为根的子树遍历并输出
第 6 章 树与森林 43 6-1 写出用广义表表示法表示的树的类声明,并给出如下成员函数的实现: (1) operator >> ( ) 接收用广义表表示的树作为输入,建立广义表的存储表示; (2) 复制构造函数 用另一棵表示为广义表的树初始化一棵树; (3) operator == ( ) 测试用广义表表示的两棵树是否相等; (4) operator << ( ) 用广义表的形式输出一棵树; (5) 析构函数 清除一棵用广义表表示的树。 【解答】 #include <iostream.h> #define maxSubTreeNum 20; //最大子树(子表)个数 class GenTree; //GenTree 类的前视声明 class GenTreeNode { //广义树结点类的声明 friend class GenTree; private: int utype; //结点标志:=0, 数据; =1, 子女 GenTreeNode * nextSibling; //utype=0, 指向第一个子女; //utype=1 或 2, 指向同一层下一兄弟 union { //联合 char RootData; //utype=0, 根结点数据 char Childdata; //utype=1, 子女结点数据 GenTreeNode *firstChild; //utype=2, 指向第一个子女的指针 } public: GenTreeNode ( int tp, char item ) : utype (tp), nextSibling (NULL) { if ( tp == 0 ) RootData = item; else ChildData = item; } //构造函数:构造广义表表示的树的数据结点 GenTreeNode ( GenTreeNode *son = NULL ) : utype (2), nextSibling (NULL), firstChild ( son ) { } //构造函数:构造广义表表示的树的子树结点 int nodetype ( ) { return utype; } //返回结点的数据类型 char GetData ( ) { return data; } //返回数据结点的值 GenTreeNode * GetFchild ( ) { return firstChild; } //返回子树结点的地址 GenTreeNode * GetNsibling ( ) { return nextSibling; } //返回下一个兄弟结点的地址 void setInfo ( char item ) { data = item; } //将结点中的值修改为 item void setFchild ( GenTreeNode * ptr ) { firstChild = ptr; } //将结点中的子树指针修改为 ptr void setNsinbilg ( GenTreeNode * ptr ) { nextSibling = ptr; } }; class GenTree { //广义树类定义 private: GenTreeNode *first; //根指针 char retValue; //建树时的停止输入标志 GenTreeNode *Copy ( GenTreeNode * ptr ); //复制一个 ptr 指示的子树 void Traverse ( GenListNode * ptr ); //对 ptr 为根的子树遍历并输出
第6章树与森林 void Remove( Gen"ptr ) ∥将以p为根的广义树结构释放 friend int Equal( Gen TreeNode*s, GenTreeNodet ) ∥比较以s和t为根的树是否相等 en Tree ( 构造函数 GenTree( GenTree& t ) ∥复制构造函数 EntRee(; ∥析构函数 Gen Tree& tl, Gen Tree&t2); ∥判两棵树tl与口是否相等 friend istream& operator >> istream& in, Gen Tree& t ); friend ostream& operator << ostream& out, Gen Tree& t ); (1) operator>()接收用广义表表示的树作为输入,建立广义表的存储表示 istream& operator >> istream& in, Gen Tree& t)( ∥友元函数,从输入流对象i接受用广义表表示的树,建立广义表的存储表示t。 t. ConstructTree( in, ret Value ) return in: void Gen Tree: ConstructTree( istream& in, char& value )i ∥从输入流对象n接受用广义表表示的非空树,建立广义表的存储表示te Stack <Gen TreeNode*>st(maxSubTreeNum): ∥用于建表时记忆回退地址 Gen TreeNode" p, a ,r cin > value: ∥广义树停止输入标志数据 cin > ch; first=q=new Gen TreeNode(0, ch ) ∥建立整个树的根结点 if( ch=="()st Push (q); ∥接着应是(,进栈 while( ch I= value )i ∥逐个结点加入 switch( ch) caseC: p=new Gen TreeNode <Type>(q); ∥建立子树,p-> firstchild=q r=st GetTop(: st Pop(; ∥从栈中退出前一结点 r->nextSibling=p; 插在前一结点r之后 ush(p ) st Push( q): ∥子树结点及子树根结点进栈 case): q->nextSibling= NULL; st pop(; ∥子树建成,封闭链,退到上层 if( st Is Empty (==0)q=st GetTop(); ∥栈不空,取上层链子树结点 else return 0: ∥栈空,无上层链,算法结東 break break: default p=q; ir( isupper(ch)q= new Gen TreeNode(0,ch);∥大写字母,建根结点 de( 1, ch )i ∥非大写字母,建数据结点 p->nextsibling=q: ∥链接
第 6 章 树与森林 44 void Remove ( GenTreeNode *ptr ); //将以 ptr 为根的广义树结构释放 friend int Equal ( GenTreeNode *s, GenTreeNode *t ); //比较以 s 和 t 为根的树是否相等 public: GenTree ( ); //构造函数 GenTree ( GenTree& t ); //复制构造函数 ~GenTree ( ); //析构函数 friend int operator == ( GenTree& t1, GenTree& t2 ); //判两棵树 t1 与 t2 是否相等 friend istream& operator >> ( istream& in, GenTree& t ); //输入 friend ostream& operator << ( ostream& out, GenTree& t ); //输出 } (1) operator >> ( ) 接收用广义表表示的树作为输入,建立广义表的存储表示 istream& operator >> ( istream& in, GenTree& t ) { //友元函数, 从输入流对象 in 接受用广义表表示的树,建立广义表的存储表示 t。 t.ConstructTree ( in, retValue ); return in; } void GenTree :: ConstructTree ( istream& in, char& value ) { //从输入流对象 in 接受用广义表表示的非空树,建立广义表的存储表示 t。 Stack <GenTreeNode* > st (maxSubTreeNum); //用于建表时记忆回退地址 GenTreeNode * p, q, r; Type ch; cin >> value; //广义树停止输入标志数据 cin >> ch; first = q = new GenTreeNode ( 0, ch ); //建立整个树的根结点 cin >> ch; if ( ch == ‘(’ ) st.Push ( q ); //接着应是‘(’, 进栈 cin >> ch; while ( ch != value ) { //逐个结点加入 switch ( ch ) { case ‘(’ : p = new GenTreeNode <Type> ( q ); //建立子树, p->firstChild = q r = st.GetTop( ); st.Pop( ); //从栈中退出前一结点 r->nextSibling = p; //插在前一结点 r 之后 st.Push( p ); st.Push( q ); //子树结点及子树根结点进栈 break; case ‘)’ : q->nextSibling = NULL; st.pop( ); //子树建成, 封闭链, 退到上层 if ( st.IsEmpty ( ) == 0 ) q = st.GetTop( ); //栈不空, 取上层链子树结点 else return 0; //栈空, 无上层链, 算法结束 break; case ‘,’ : break; default : p = q; if ( isupper (ch) ) q = new GenTreeNode ( 0, ch ); //大写字母, 建根结点 else q = new GenTreeNode ( 1, ch ); //非大写字母, 建数据结点 p->nextSibling = q; //链接 }
第6章树与森林 (2)复制构造函数用另一棵表示为广义表的树初始化一棵树; GenTree: GenTree( const GenTree& t)i ∥共有函数 first=Copy( tfirst ) GenTreeNode* Gen Tree Copy( Gen TreeNode"ptr)t ∥私有函数,复制一个pt指示的用广义表表示的子树 GenTreeNode *q=NULL; f( ptr I=NULL)( q= new Gen TreeNode( ptr->utype, NULL ) switch( ptr->utype )i ∥根据结点类型 utype传送值域 case0: q->RootData= ptr->RootData; break ∥传送根结点数据 case 1: q->ChildData= ptr->ChildData; break; ∥传送子女结点数据 case 2: q->firstChild =Copy( ptr->first Child ) break ∥递归传送子树信息 q->nextsibling= Copy( ptr->nextS ibling ) ∥复制同一层下一结点为头的表 return q; (3) operator=()测试用广义表表示的两棵树是否相等 int operator ==( Gen Tree& tl, Gen Tree& 22) ∥友元函数:判两棵树t与口是否相等,若两表完全相等,函数返回1,否则返回0。 return Equal( tl. first, t2. first ) int Equal( GenTreeNode * tl, Gen TreeNode *2)t ∥是 Gen TreeNode的友元函数 if( tI ==NULL &&t2=NULL)return 1: ∥表t与表t2都是空树,相等 f(t=NUL&&t2l=NULL&&tl->upe=口2->utpe){W两子树都非空且结点类型相同 switch( tl->utype ∥比较对应数据 case0: x=(tl-> RootData==t2-> RootData)?1: 0; ∥根数据结点 case 1: x=(tI->Child Data ChildData)? 1: 0; ∥女数据结点 Ae2: x= Equal( tI->firstChild, t2->first Child ) ∥递归比较其子树 if(x)return Equal(tI-nextsibling, t2->nextSibling
第 6 章 树与森林 45 cin >> ch; } } (2) 复制构造函数 用另一棵表示为广义表的树初始化一棵树; GenTree :: GenTree ( const GenTree& t ) { //共有函数 first = Copy ( t.first ); } GenTreeNode* GenTree :: Copy ( GenTreeNode *ptr ) { //私有函数,复制一个 ptr 指示的用广义表表示的子树 GenTreeNode *q = NULL; if ( ptr != NULL ) { q = new GenTreeNode ( ptr->utype, NULL ); switch ( ptr->utype ) { //根据结点类型 utype 传送值域 case 0 : q->RootData = ptr->RootData; break; //传送根结点数据 case 1 : q->ChildData = ptr->ChildData; break; //传送子女结点数据 case 2 : q->firstChild = Copy ( ptr->firstChild ); break; //递归传送子树信息 } q->nextSibling = Copy ( ptr->nextSibling ); //复制同一层下一结点为头的表 } return q; } (3) operator == ( ) 测试用广义表表示的两棵树是否相等 int operator == ( GenTree& t1, GenTree& t2 ) { //友元函数 : 判两棵树 t1 与 t2 是否相等, 若两表完全相等, 函数返回 1, 否则返回 0。 return Equal ( t1.first, t2.first ); } int Equal ( GenTreeNode *t1, GenTreeNode *t2 ) { //是 GenTreeNode 的友元函数 int x; if ( t1 == NULL && t2 == NULL ) return 1; //表 t1 与表 t2 都是空树, 相等 if ( t1 != NULL && t2 != NULL && t1->utype == t2->utype ) { //两子树都非空且结点类型相同 switch ( t1->utype ) { //比较对应数据 case 0 : x = ( t1->RootData == t2->RootData ) ? 1 : 0; //根数据结点 break; case 1 : x = ( t1->ChildData == t2->ChildData ) ? 1 : 0; //子女数据结点 break; case 2 : x = Equal ( t1->firstChild, t2->firstChild ); //递归比较其子树 } if ( x ) return Equal ( t1->nextSibling, t2->nextSibling );
第6章树与森林 ∥相等,递归比较同一层的下一个结点;不等,不再递归比较 (4) operator<<()用广义表的形式输出一棵树 ∥元函数,将树t输出到输出流对象out t traverse( out, t. first ) ∥有函数,广义树的遍历算法 if( ptr I=NULL)& if ptr->utype ==0)out < ptr->RootData <<'C; ∥根数据结点 ∥子女数据结点 out < ptr->ChildDat ∥子树结点 traverse( ptr->firstChild ) m向子树方向搜索 if( ptr->nextSibling I= NULL )out < traverse( ptr->nextSibling ) 向同一层下一兄弟搜索 (5)析构函数清除一棵用广义表表示的树 GenTree: :-GenTree(i m用广义表表示的树的析构函数,假定frst≠NULL Gen Tree Node p: if( p-> 2)Remove( p->firstChild ) 在子树中删除 ptr->nextsibling=p->nextsibling: delete(p); 释放结点p
第 6 章 树与森林 46 //相等, 递归比较同一层的下一个结点; 不等, 不再递归比较 } return 0; } (4) operator << ( ) 用广义表的形式输出一棵树 ostream& operator << ( ostream& out, GenTree& t ) { //友元函数, 将树 t 输出到输出流对象 out。 t.traverse ( out, t.first ); return out; } void GenTree :: traverse ( ostream& out, GenTreeNode * ptr ) { //私有函数, 广义树的遍历算法 if ( ptr != NULL ) { if ( ptr->utype == 0 ) out << ptr->RootData << ‘(’; //根数据结点 else if ( ptr->utype == 1 ) { //子女数据结点 out << ptr->ChildData; if ( ptr->nextSibling != NULL ) out << ‘,’; } else { //子树结点 traverse ( ptr->firstChild ); //向子树方向搜索 if ( ptr->nextSibling != NULL ) out << ‘,’; } traverse ( ptr->nextSibling ); //向同一层下一兄弟搜索 } else out << ‘)’; } (5) 析构函数 清除一棵用广义表表示的树 GenTree :: ~ GenTree ( ) { //用广义表表示的树的析构函数, 假定 first ≠ NULL Remove ( first ); } void GenTree :: Remove ( GenTreeNode *ptr ) { GenTreeNode * p; while ( ptr != NULL ) { p = ptr->nextSibling; if ( p->utype == 2 ) Remove ( p->firstChild ); //在子树中删除 ptr->nextSibling = p->nextSibling; delete ( p ); //释放结点 p } }
第6章树与森林 6-2列出右图所示二叉树的叶结点、分支结点和每个结点的层次 【解答】 二叉树的叶结点有⑥、⑧、⑨。分支结点有①、②、③、④、⑤、⑦。 结点①的层次为0:结点②、③的层次为1:结点④、⑤、⑥的层次 为2:结点⑦、⑧的层次为3:结点⑨的层次为 6-3使用(1)顺序表示和(2)二叉链表表示法,分别画出右图所示二叉树的存储表示。 ②③④ ⑥ a 101112131415 Al△ E△ 顺序表示 回人 叉链表表示 6-3用嵌套类写出用链表表示的二叉树的类声明 【解答】 #include <iostream. h> template <class Type> class Binary Tree i private plate <class Type> class BinTreeNode i public: BinTreeNode<Type> *leftchild, *right Child; Type RevAlue; BinTreeNode<Type>*root; BinTreeNode<Type>"Parent( BinTreeNode<Type> * start, BinTreeNode<Type>"current )i int Insert( BinTreeNode<Type> current, const Type& item ) int Find( BinTreeNode<Type> *current, const Type& item )const void destroy( BinTreeNode<Type>*current )i void Traverse( Bin TreeNode<Type> *current, ostream& out)const Binary Tree(: root(NULL)0 Binary Tree( Type value ) RefValue( value ) root( NULL Binary Tree()i destroy(root);j BinTreeNode(): leftChild( NULL ) right Child(NULL)J BinTreeNode( Type item ) data( item ) leftChild NULL ) right Child NULL)0 BinTreeNode<Type>* GetLeft (const( return leftChild; j Right const( return right Child; 3 oid SetData( const Type& item )i data=item; 3 id SetLeft( BinTreeNode<Type>"L)leftChild =L;)
第 6 章 树与森林 47 6-2 列出右图所示二叉树的叶结点、分支结点和每个结点的层次。 【解答】 二叉树的叶结点有⑥、⑧、⑨。分支结点有①、②、③、④、⑤、⑦。 结点①的层次为 0;结点②、③的层次为 1;结点④、⑤、⑥的层次 为 2;结点⑦、⑧的层次为 3;结点⑨的层次为 4。 6-3 使用 (1) 顺序表示和 (2) 二叉链表表示法,分别画出右图所示二叉树的存储表示。 6-3 用嵌套类写出用链表表示的二叉树的类声明。 【解答】 #include <iostream.h> template <class Type> class BinaryTree { private: template <class Type> class BinTreeNode { public: BinTreeNode<Type> *leftChild, *rightChild; Type data; } Type RefValue; BinTreeNode<Type> * root; BinTreeNode<Type> *Parent ( BinTreeNode<Type> *start, BinTreeNode<Type> *current ); int Insert ( BinTreeNode<Type> *current, const Type& item ); int Find ( BinTreeNode<Type> *current, const Type& item ) const; void destroy ( BinTreeNode<Type> *current ); void Traverse ( BinTreeNode<Type> *current, ostream& out ) const; public: BinaryTree ( ) : root ( NULL) { } BinaryTree ( Type value ) : RefValue ( value ), root ( NULL ){ } ~BinaryTree ( ) { destroy (root); } BinTreeNode ( ) : leftChild ( NULL), rightChild ( NULL) { } BinTreeNode ( Type item ) : data ( item ), leftChild ( NULL ), rightChild ( NULL ) {} Type& GetData ( ) const { return data; } BinTreeNode<Type>* GetLeft ( ) const { return leftChild; } BinTreeNode<Type>* GetRight ( ) const{ return rightChild; } void SetData ( const Type& item ){ data = item; } void SetLeft ( BinTreeNode<Type> *L ) { leftChild = L; } ① ② ③ ④ ⑤ ⑥ ⑦ ⑨ ⑧ ∧ ∧ ∧ ∧ ∧ ∧ ∧ ∧ ∧ ∧ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ① ② ③ ④ ⑤ ⑥ ⑦ ⑧ ⑨ 顺序表示 二叉链表表示