二叉树打印

#include<stdio.h>

#include<malloc.h>

typedef struct Node

{

         char data;

         struct Node *Lchild;

         struct Node *Rchild;

}BiTNode,*BiTree;

typedef struct Node1{

         BiTNode *element[5];

         int front;

         int rear;

}SeqQueue;

 

int IsEmpty(SeqQueue Q){

         if(Q.front==Q.rear)  return 1;

         else return 0;

}

 

 

void InitQueue(SeqQueue *Q){

         Q->front=Q->rear=0;

}

int EnterQueue(SeqQueue *Q,BiTNode *x){

         if((Q->rear+1)%5==Q->front) return 0;

         Q->element[Q->rear]=x;

         Q->rear=(Q->rear+1)%5;

         return 1;

}

int DeleteQueue(SeqQueue *Q,BiTNode *x){

         if(Q->front==Q->rear) return 0;

         x=Q->element[Q->front];

         Q->front=(Q->front+1)%5;

         return 1;

}

 

void CreateBiTree(BiTree *bt){

         char ch;

         ch=getchar();

         if(ch=='.') *bt=NULL;

         else{

                   *bt=(BiTree)malloc(sizeof(BiTNode));

                   (*bt)->data=ch;

                   CreateBiTree(&((*bt)->Lchild ));

                   CreateBiTree(&((*bt)->Rchild));

        

        

         }

}

int LayerOrder(BiTNode *bt){

         SeqQueue Q1;

         BiTNode *p=bt;

         InitQueue(&Q1);

         if(bt==NULL) return 0;

         EnterQueue(&Q1,p);

         while(!IsEmpty(Q1)){

                   p=Q1.element[Q1.front];

                   DeleteQueue(&Q1,p);

                   printf("%c",p->data);

                   if(p->Lchild) {EnterQueue(&Q1,p->Lchild);}

                   if(p->Rchild) {EnterQueue(&Q1,p->Rchild);}

        

         }

         return 1;

}

 

void main(){

         BiTree L;

         L=(BiTNode*)malloc(sizeof(BiTNode));

         printf("请输入二叉链表\n");

         CreateBiTree(&L);

         printf("请输出打印后的二叉树\n");

         LayerOrder(L);

 

}

 

 

                                                                                                                                                                    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值