您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > 层序遍历二叉树(队列),已调试,C语言
#includestdlib.h#includestdio.h#defineTRUE1#defineFALSE0#defineOK1#defineERROR0#defineOVERFLOW-2typedefintStatus;typedefcharTElemType;typedefstructBiTNode{TElemTypedata;structBiTNode*lchild,*rchild;}BiTNode,*BiTree;#defineMAXQSIZE100typedefBiTreeQElemType;typedefstruct{QElemTypebase[MAXQSIZE];intfront;intrear;}SqQueue;StatusCreateBiTree(BiTree*T){charch;scanf(%c,&ch);if(ch=='#')*T=NULL;else{if(!(*T=(BiTNode*)malloc(sizeof(BiTNode))))exit(OVERFLOW);(*T)-data=ch;CreateBiTree(&((*T)-lchild));CreateBiTree(&((*T)-rchild));}returnOK;}voidInitQueue(SqQueue*Q){Q-front=Q-rear=0;}StatusEnQueue(SqQueue*Q,QElemTypee){if((Q-rear+1)%MAXQSIZE==Q-front)returnERROR;Q-base[Q-rear]=e;Q-rear=(Q-rear+1)%MAXQSIZE;returnOK;}StatusDeQueue(SqQueue*Q,QElemType*e){if(Q-front==Q-rear)returnERROR;*e=Q-base[Q-front];Q-front=(Q-front+1)%MAXQSIZE;returnOK;}StatusQueueEmpty(SqQueueQ){if(Q.rear==Q.front)returnTRUE;elsereturnFALSE;}voidTraverse(BiTreeT){SqQueueQ;BiTreep;p=T;InitQueue(&Q);if(p)EnQueue(&Q,p);while(!QueueEmpty(Q)){DeQueue(&Q,&p);printf(%c,p-data);if(p-lchild)EnQueue(&Q,p-lchild);if(p-rchild)EnQueue(&Q,p-rchild);}printf(\n);}voidmain(){BiTreeT1;CreateBiTree(&T1);Traverse(T1);}
本文标题:层序遍历二叉树(队列),已调试,C语言
链接地址:https://www.777doc.com/doc-3264643 .html