层序输出结点数据

#include<iostream.h>    
#include<stdlib.h>


typedef struct BiNode
{
 char data;
 BiNode *lchild,*rchild;
} *BiTree;


BiTree queue[100];    //队列当中存的是指向结构体的指针,而不是通常的整形或字符型的数据。
int front,rear;


BiTree CreateTree()   //建立二叉树
{
 BiTree root;
 char ch;
cin>>ch;
 if(ch=='#') root=NULL;
 else{
  root=new BiNode;
  root->data=ch;
  root->lchild=CreateTree();
  root->rchild=CreateTree();
 }
 return root;
}


void LeverOrder(BiTree T)   //层序遍历二叉树(进出队列元素都依赖于front)
{
 front=rear=0;
 queue[++rear]=T;
 while(front!=rear)
 {
  cout<<queue[++front]->data;


  if(queue[front]->lchild) 
  queue[++rear]=queue[front]->lchild;
  if(queue[front]->rchild) 
  queue[++rear]=queue[front]->rchild;
 }
   cout<<endl;
}


void main()
{
 BiTree root;
 root=CreateTree();
 LeverOrder(root);

}

猜你喜欢

转载自blog.csdn.net/weixin_41939516/article/details/80811132
今日推荐