所謂二叉樹層序遍歷,即從二叉樹根結點開始,按從上到下、從左到右的順序訪問每一個結點。每個結點只訪問一次。
#include <stdio.h> #include <stdlib.h> /** * 二叉樹二叉鏈表之非遞歸遍歷:層序遍歷 * 算法思想:借助一個隊列;根樹進隊;隊不為空時循環“從隊列中出一個樹p,訪問該樹根結點; * 若它有左子樹,左子樹進隊;若它有右子樹,右子樹進隊。” * (保證了元素進隊順序是從樹的上層到下層,且同層元素在隊列中從左到右相鄰) * (注:為了節約空間,這里的樹進隊,是指樹的地址進隊,而不是樹結點進隊,隊列中存放的是對各樹地址的引用) */ #define OK 1; #define TURE 1; #define FALSE 0; #define ERROR 0; const int OVERFLOW = -2; const int MAXSIZE = 100; typedef int Status; typedef char TElemType; //二叉鏈表結構定義 typedef struct BiNode{ TElemType data; struct BiNode *lchild, *rchild; } BiNode, *BiTree; //順序循環隊列定義 typedef struct { BiTree *base; //存放樹型指針 int front; //頭指針,若隊列不為空,指向隊列頭元素 int rear; //尾指針,若隊列不為空,指向隊列尾元素的下一個位置 } SqQueue; //由字符序列創建二叉樹 Status CreateBiTree(BiTree *tree,TElemType data[],int *j,int len){ if((*j)<=len-1){ if(data[(*j)]=='#'){ (*tree)=NULL; (*j)++; } else { (*tree)=(BiTree)malloc(sizeof(BiNode)); if(!(*tree)) return OVERFLOW; (*tree)->data=data[(*j)]; //生成根結點 (*j)++; CreateBiTree(&((*tree)->lchild),data,j,len); //構造左子樹 CreateBiTree(&((*tree)->rchild),data,j,len); //構造右子樹 } } return OK; } //訪問二叉樹結點 Status Visit(BiTree tree){ printf("%c",tree->data); return OK; } //借助隊列實現二叉鏈表的層序遍歷 Status LevelOrder_ByQueue(BiTree tree) { BiTree p; SqQueue queue1; InitQueue(&queue1); EnQueue(&queue1,tree); //根結點入隊 while(queue1.front!=queue1.rear){ //隊不為空 DeQueue(&queue1,&p); //根節點出隊 Visit(p); if(p->lchild!=NULL) EnQueue(&queue1,p->lchild); //有左孩子就進隊 if(p->rchild!=NULL) EnQueue(&queue1,p->rchild); //有右孩子也進隊 } return OK; } /***用到隊列的相關函數***/ //循環隊列初始化 Status InitQueue(SqQueue *queue) { queue->base=(BiTree*)malloc(sizeof(BiTree)*MAXSIZE); //分配隊列數組空間 if(!queue->base) return OVERFLOW; //分配失敗 queue->front=0; queue->rear=0; return OK; } //循環隊列入隊操作 Status EnQueue(SqQueue *queue,BiTree elem){ if((queue->rear+1)%MAXSIZE==queue->front){ //隊滿 return ERROR; } else { queue->base[queue->rear]=elem; queue->rear=(queue->rear+1)%MAXSIZE; return OK; } } //循環隊列出隊操作 Status DeQueue(SqQueue *queue,BiTree *elem){ if(queue->front==queue->rear){ //隊空 return ERROR; } else { (*elem)=queue->base[queue->front]; queue->front=(queue->front+1)%MAXSIZE; return OK; } } int main(void){ //示例二叉樹的結構 /* A / B / \ C D / \ E F \ G */ //指向二叉樹的指針 BiTree bitree1; //創建二叉樹 待用數據 TElemType data1[]={'A','B','C','#','#','D','E','#','G','#','#','F','#','#','#',}; //先序遍歷序列 int len1=sizeof(data1)/sizeof(data1[0]); int* j1=(int*)malloc(sizeof(int)); *j1=0; //按先序遍歷序列創建二叉樹 Status createBiTreeResult = CreateBiTree(&bitree1,data1,j1,len1); printf("二叉樹創建結果:%d\n",createBiTreeResult); //層序遍歷二叉樹 Status levelOrder_ByQueue = LevelOrder_ByQueue(bitree1); //注:這里參數是二叉樹根結點,而不是指針 printf("\n層序遍歷二叉樹結果:%d\n",levelOrder_ByQueue); printf("\nEND"); return 0; }