64,680
社区成员
发帖
与我相关
我的任务
分享
void level_travel(LinkTree *s) //层次遍历一棵头节点为s的树
{
int sta,top,sin[100],cen; //标记树的层次
sta=0;
top=0;
cen=1;
ListQueue *q;
LinkTree *p;
DataQueue *test;
q=InitQueue();
p=s;
InQueue(q,p);
sin[top++]=cen;
while(q->front!=q->rear)
{
p=DeQueue(q);
if(sin[sta]>sin[sta-1]||sta==0)
{
printf("\n第%d层节点:",sin[sta]);
}
printf(" %d ",p->data);
sta++;
if(p->lchild)
{
InQueue(q,p->lchild);
sin[top++]=sin[sta-1]+1;
}
if(p->rchild)
{
InQueue(q,p->rchild);
sin[top++]=sin[sta-1]+1;
}
test=q->front->next;
}
printf("\n");
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
typedef struct Lnode
{
int data;
struct Lnode *lchild;
struct Lnode *rchild;
}LinkTree; //树的节点
typedef struct Dnode
{
LinkTree *data;
struct Dnode *next;
}DataQueue; //队列的节点
typedef struct LQ
{
DataQueue *front, *rear;
}ListQueue; //链式队列
void insert_tree(LinkTree *s,int data) //在s为头节点的二叉树上按lchild<parent<rchild原则添加节点,节点数据为data
{
LinkTree *temp,*b;
while(s!=NULL)
{
b=s;
if(data<s->data)
s=s->lchild;
else if(data>s->data)
s=s->rchild;
}
temp=(LinkTree *)malloc(sizeof(LinkTree));
temp->data=data;
temp->lchild=temp->rchild=NULL;
if(data<b->data)
b->lchild=temp;
else if(data>b->data)
b->rchild=temp;
}
LinkTree * Create_tree(int n,int *pdata) //创建一棵树,pdata为连续存储的将要写入树中的data的地址,n为节点个数
{
int i;
LinkTree *temp,*s;
i=1;
s=(LinkTree *)malloc(sizeof(LinkTree));
s->data=pdata[0];
s->lchild=s->rchild=NULL;
// printf("%d.\n",pdata[0]);
temp=s;
printf("OK,create C.\n");
while(i<n)
{
insert_tree(temp,pdata[i]);
i++;
}
printf("OK,create Tree.\n");
return s;
}
void dis_pre_tree(LinkTree *s) //先序遍历头节点为s的树
{
if(s==NULL)
return;
printf("%d ",s->data);
if(s->lchild!=NULL)
dis_pre_tree(s->lchild);
if(s->rchild!=NULL)
dis_pre_tree(s->rchild);
}
void dis_post_tree(LinkTree *s) //后序遍历头节点为s的树
{
if(s==NULL)
return;
if(s->lchild!=NULL)
dis_post_tree(s->lchild);
if(s->rchild!=NULL)
dis_post_tree(s->rchild);
printf("%d ",s->data);
}
ListQueue *InitQueue() //初始化一个链式队列,返回
{
DataQueue *d;
ListQueue *q;
d=(DataQueue *)malloc(sizeof(DataQueue));
q=(ListQueue *)malloc(sizeof(ListQueue));
q->front=d;
q->front->next=NULL;
q->rear=q->front;
printf("init queue successful.\n");
return q;
}
void InQueue(ListQueue *q,LinkTree *p) //把二叉树节点的指针p写入队列的data中
{
DataQueue *a;
a=(DataQueue *)malloc(sizeof(DataQueue));
a->data=p;
a->next=NULL;
q->rear->next=a;
q->rear=a; //rear对应最后进对的节点
}
LinkTree *DeQueue(ListQueue *q) //出队
{
DataQueue *a;
LinkTree *p;
if(q->front!=q->rear)
{
a=q->front->next; //q->front->next对应先入队的节点
p=a->data;
if(q->front->next!=q->rear)
q->front->next=a->next;
else if(q->front->next==q->rear) //front和rear重合要初始化一次
{
q->rear=q->front;
q->front->next=NULL;
}
free(a);
return p;
}
else
return NULL;
}
void level_travel(LinkTree *s) //层次遍历一棵头节点为s的树
{
int sta,top,sin[100],cen; //标记树的层次
sta=0;
top=0;
cen=1;
ListQueue *q;
LinkTree *p;
DataQueue *test;
q=InitQueue();
p=s;
InQueue(q,p);
sin[top++]=cen;
while(q->front!=q->rear)
{
p=DeQueue(q);
if(sin[sta]>sin[sta-1]||sta==0)
{
printf("\n第%d层节点:",sin[sta]);
}
printf(" %d ",p->data);
sta++;
if(p->lchild)
{
InQueue(q,p->lchild);
sin[top++]=sin[sta-1]+1;
}
if(p->rchild)
{
InQueue(q,p->rchild);
sin[top++]=sin[sta-1]+1;
}
test=q->front->next;
}
printf("\n");
}
void main()
{
int n=11;
int a[]={5,13,6,9,12,4,8,3,0,100,56};
LinkTree *s;
s=Create_tree(n,a);
dis_pre_tree(s);
printf("\n");
dis_post_tree(s);
printf("\n");
level_travel(s);
printf("\n");
}
void LevelOrder()
{
if (root == NULL)
return;
queue<BTNode<T>*> travQueue;
travQueue.push(root);
while (!travQueue.empty())
{
Visit(travQueue.front()->element);
if (travQueue.front()->lChild != NULL)
travQueue.push(travQueue.front()->lChild);
if (travQueue.front()->rChild != NULL)
travQueue.push(travQueue.front()->rChild);
travQueue.pop();
}
}