//中序遍歷的遞歸與非遞歸算法 #include<iostream> using namespace std; #define MAXQSIZE 100 #define OK 1 #define ERROR 0 #define OVERFLOW -2 typedef int Status; typedef struct BiNode{ //二叉鏈表定義 char data; struct BiNode *lchild,*rchild; }BiTNode,*BiTree; /************************************* 隊列 ***************************************/ typedef BiTree QElemType; typedef struct{ QElemType *base;//初始化時動態分配存儲空間 int front;//頭指針 int rear;//尾指針 int last; }SqQueue; //算法3.13 循環隊列的初始化 Status InitQueue(SqQueue &Q) { // 構造一個空隊列Q Q.base = new QElemType[MAXQSIZE]; if(!Q.base) { return OVERFLOW; // 存儲分配失敗 } Q.front = 0; Q.rear = 0; return OK; } //算法3.14 求循環隊列的長度 int QueueLength(SqQueue Q) {// 返回Q的元素個數,即隊列的長度 return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE; } int QueueEmpty(SqQueue &Q) { if (Q.front==Q.rear) return OK; else return ERROR; } //算法3.15 循環隊列的入隊 Status EnQueue(SqQueue &Q,QElemType e) {// 插入元素e爲Q的新的隊尾元素 if((Q.rear+1)%MAXQSIZE == Q.front) { return ERROR;//尾指針在循環意義上加1後等於頭指針,代表隊滿 } Q.base[Q.rear] = e; Q.rear = (Q.rear+1)%MAXQSIZE; return OK; } //算法3.16 循環隊列的出隊 Status DeQueue(SqQueue &Q,QElemType &e) { if(Q.rear == Q.front) { return ERROR; } e = Q.base[Q.front]; Q.front = (Q.front+1)%MAXQSIZE; return OK; } BiTree GetHead(SqQueue Q) {//返回Q的隊列元素,不修改隊頭指針 if(Q.front!=Q.rear) //隊列非空 return Q.base[Q.front]; //返回隊頭元素的值,隊頭指針不變 } /************************************************************************************/ //用算法5.3 先序遍歷的順序創建二叉鏈表 void CreateBiTree(BiTree &T){ //按先序次序輸入二叉樹中結點的值(一個字符),建立二叉鏈表表示的二叉樹T char ch; cin >> ch; if(ch=='#') T=NULL; //遞歸結束,建空樹 else{ T=new BiTNode; T->data=ch; //生成根結點 CreateBiTree(T->lchild); //遞歸建立左子樹 CreateBiTree(T->rchild); //遞歸建立右子樹 } //else } //CreateBiTree void InOrderTraverse(BiTree T){ //中序遍歷二叉樹T的遞歸算法 if(T){ InOrderTraverse(T->lchild); cout << T->data; InOrderTraverse(T->rchild); } } //實現按層遍歷二叉樹的非遞歸算法(隊列) void HierarchyTraverse(BiTree T) { BiTree bt = T; SqQueue Q; InitQueue(Q); if(!bt){ return; } EnQueue(Q,bt); while(Q.rear!=Q.front){ DeQueue(Q,bt); cout<<bt->data; if(bt->lchild!=NULL){ EnQueue(Q,bt->lchild); } if(bt->rchild!=NULL){ EnQueue(Q,bt->rchild); } } } //統計二叉樹中的葉子結點個數 int LeafNodeCount(BiTree T){ if(T==NULL) return 0; else if(T->lchild==NULL&&T->rchild==NULL) return 1; else return LeafNodeCount(T->lchild)+LeafNodeCount(T->rchild); } //------------------- int Depth(BiTree T) { int m,n; if(T == NULL ) return 0; //若是是空樹,深度爲0,遞歸結束 else { m=Depth(T->lchild); //遞歸計算左子樹的深度記爲m n=Depth(T->rchild); //遞歸計算右子樹的深度記爲n if(m>n) return(m+1); //二叉樹的深度爲m 與n的較大者加1 else return (n+1); } } int LevelWidth(BiTree root,int level)//find the width of a level(amounts of nodes in the level). { if(!root)return 0; else { if(level==1)return 1; level=LevelWidth(root->lchild,level-1)+LevelWidth(root->rchild,level-1); } return level; } int Width(BiTree root)//find the maximum width of the btree. { int width,i; int w[20]; for(i=0;i<20;i++)w[i]=0; if(!root)width=0; else { for(i=0;i<=Depth(root);i++)w[i]=LevelWidth(root,i+1); } i=0; while(w[i]) { if(w[i]>width)width=w[i]; i++; } return width; } // // ------------------- nt main(){ BiTree tree; cout<<"請輸入創建二叉鏈表的序列:\n"; CreateBiTree(tree); cout<<"中序遍歷的結果爲:\n"; InOrderTraverse(tree); cout<<endl; //按層遍歷二叉樹 cout<<"按層遍歷的結果爲:\n"; HierarchyTraverse(tree); cout<<endl; cout<<"二叉樹的寬度爲"<<Width(tree)<<endl; //統計葉子結點樹 cout<<"葉子結點數爲"<<LeafNodeCount(tree); cout<<"\n"; return 0; }