如果有的话,麻烦告诉我一下。
谢谢!
用队列来做
void bianli4 (struct btnode *bt) /* 按层次遍历 */
{
struct btnode *V[SIZE],*p;
int front=0,area=0;
if (bt!=NULL)
{area++;
V[area]=bt;
while (front<area)
{front++;
p=V[front];
printf("%d ",p->data);
if(p->lchild!=NULL) {area++;V[area]=p->lchild;}
if(p->rchild!=NULL) {area++;V[area]=p->rchild;}
}
}
return;
}
// example2.cpp : 定义控制台应用程序的入口点。
//
// example1.cpp : 定义控制台应用程序的入口点。
//
#include"stdio.h"
#include"malloc.h"
#define MAX 100
typedef char TDataType;
typedef struct TreeNode
{TDataType data;
struct TreeNode*lchild,*rchild;
}TreeNode,*Tree;
typedef Tree QDataType;
typedef struct
{QDataType data[MAX+1];
int front,rear;
}Queue;
//(1)初始化队列q
void InitQueue(Queue &q)
{
q.front=0;
q.rear=0;
}
//(2)判断队列q是否非空;
int EmptyQueue(Queue &q)
{
if(q.rear==q.front) return 1;
else return 0;
}
//(3)依次进队元素a,b,c
void InsqQueue(Queue &q,QDataType x)
{
if(q.rear==MAX){printf("overflow"); }
q.rear++;
q.data[q.rear]=x;
}
//(4)出队一个元素,输出该元素;
OutsqQueue(Queue &q,Tree &p)
{
if(EmptyQueue(q))
{
printf("underflow");
return 0;
}
else
{
p=q.data[q.front+1];
// printf("%c",x);
q.front++;
}
if(EmptyQueue(q)){q.front=0;q.rear=0;}
return 1;
}
//(5)输出队列的元素个数;
int NumberQueue(Queue &q)
{
if(EmptyQueue(q)) return 0;
else return(q.rear-q.front);
}
//(8)输出出队序列; begin
int DisplaySqQueue(Queue &q)
{
int i;
if(EmptyQueue(q))return 1;
for(i=1;i<=q.rear;i++)
{
printf("%c",q.data[q.front+1]);
q.front++;
}
printf("\n");
return 1;
}
//n(9)释放队列。
void DestroyList_SqQueue(Queue &q)
{
if(EmptyQueue(q)==0)
q.front=0;q.rear=0;
}
Tree CreateTree(Tree &r)
{
char c;
scanf("%c",&c);
if(c==' ')r=NULL;
else
{
r=(Tree)malloc(sizeof(TreeNode));
if(!r)return(NULL);
r->data=c;
r->lchild = CreateTree(r->lchild);
r->rchild = CreateTree(r->rchild);
}
return(r);
}
//先序遍历二叉树
void Preorder(Tree r)
{
if(r)
{
printf("%c",r->data);
Preorder(r->lchild);
Preorder(r->rchild);
}
}
//中序遍历二叉树
void midorder(Tree r)
{
if(r)
{
midorder(r->lchild);
printf("%c",r->data);
midorder(r->rchild);
}
}
//后序遍历二叉树
void postorder(Tree r)
{
if(r)
{
postorder(r->lchild);
postorder(r->rchild);
printf("%c",r->data);
}
}
//层次遍历二叉树
void TravelTree(Tree r)
{
Queue q;
InitQueue(q);
Tree p,p1,p2;
p=r;//(Tree)malloc(sizeof(TreeNode));
if(p)InsqQueue(q,p);
while(!EmptyQueue(q))
{
OutsqQueue(q,p);
printf("%c",p->data);
p1=p->lchild;p2=p->rchild;
if(p1) InsqQueue(q,p1);
if(p2) InsqQueue(q,p2);
}
}
void main()
{
Tree r;
printf("初始化树\n");
CreateTree(r);
printf("\n先序遍历二叉树\n");
Preorder(r);
printf("\n中序遍历二叉树\n");
midorder(r);
printf("\n后序遍历二叉树\n");
postorder(r);
printf("\n层序遍历二叉树\n");
TravelTree(r);
}
#define QUEUESIZE 100
#define TRUE 1
#define FALSE !TRUE
#define Statu int
#define BitTreeElementType int
#define BTELEMIOFORMAT "%d"
#define ENDFLAG -1
typedef struct Bittreenode
{
BitTreeElementType data;
struct Bittreenode *lchild;
struct Bittreenode *rchild;
}BTnode,*BitTree;
typedef BitTree QueueElemtype;
typedef struct
{
QueueElemtype data[QUEUESIZE];
int front;
int rear;
}queue;
int CreateQueue(queue *q);
int Entry(queue *q, QueueElemtype c);
Statu Exit(queue *q, QueueElemtype *reb);
Statu CreateBitTree(BitTree *T);
Statu LayerTravelBitTree(BitTree T);
main()
{
BitTree t;
system("cls");
printf("Input data to create bittree and '");
printf(BTELEMIOFORMAT,ENDFLAG);
printf("' will make null tree.\n");
CreateBitTree(&t);
printf("Layer order travel the tree:\n");
LayerTravelBitTree(t);
system("pause");
}
Statu CreateBitTree(BitTree *T)
{
BitTreeElementType inputdata;
*T = (BitTree )malloc(sizeof(BTnode));
printf("Enter Data:");
fflush(stdin);
scanf(BTELEMIOFORMAT,&inputdata);
if ( inputdata == ENDFLAG )
{
*T = NULL;
return FALSE;
}
else
{
(*T)->data = inputdata;
printf("Create left sub tree...<");
CreateBitTree(&((*T)->lchild));
printf("Create right sub tree...<");
CreateBitTree(&((*T)->rchild));
return TRUE;
}
}
Statu LayerTravelBitTree(BitTree T)
{
queue tq;
QueueElemtype res;
CreateQueue(&tq);
Entry(&tq,T);
while ( Exit(&tq,&res) == TRUE)
{
if (res)
{
VisitData(res->data);
Entry(&tq,res->lchild);
Entry(&tq,res->rchild);
}
}
}
int CreateQueue(queue *q)
{
q->front = -1;
q->rear = -1;
}
int Entry(queue *q, QueueElemtype c)
{
q->rear++;
if (q->rear >= QUEUESIZE)
{
printf("Queue overflow!\n");
exit(0);
}
q->data[q->rear] = c;
return 1;
}
Statu Exit(queue *q, QueueElemtype *returnback)
{
if (q->front == q->rear)
{
return FALSE;
}
q->front++;
*returnback = q->data[q->front];
return TRUE;
}
Statu VisitData(BitTreeElementType data)
{
printf(BTELEMIOFORMAT,data);
printf("\n");
return TRUE;
}