1.基本思路
层序遍历的基本思路就是,
1.根节点入队列。
2.根节点出队,同时将根节点左儿子和右儿子入队
3.结点出队,同时将该节点的左儿子和右儿子入队
4.重复3直到队列为空
2.算法实现
void layerprint(struct TreeNode* r)
{
struct queue q1; //创建队列
q1.s=&q1.node[0]; //初始化队列
q1.e=&q1.node[0];
struct TreeNode* p; //临时结点指针
in(&q1,r); //根节点入队
while(q1.e!=q1.s) //判断队列是否为空
{
p = out(&q1); //出队
printf("%d ",p->data); //访问结点
if(p->left !=NULL)
in(&q1,p->left); //左儿子入队
if(p->right!=NULL)
in(&q1,p->right); //右儿子入队
}
}
3.完整代码
//main.c
#include<stdio.h>
#include "fun.c"
struct queue
{
struct TreeNode node[100];
struct TreeNode* s;
struct TreeNode* e;
};
struct stack
{
struct TreeNode node[100];
struct TreeNode *top;
};
struct TreeNode* pop(struct stack* a)
{
struct TreeNode* tmp =a->top;//printf("%d",*a->top);
a->top--;
return tmp;
}
int isEmpty(struct stack* a)
{
if(a->top==&a->node[0])
return 1;
else
return 0;
}
void push(struct stack* a,struct TreeNode *node)
{
a->top++;
a->top->data = node->data;
a->top->left = node->left;
a->top->right = node->right;
}
void PrePrint(struct TreeNode *r)
{
struct stack stack1;
stack1.top = &stack1.node[0];
struct TreeNode *p =r;
while(p||!isEmpty(&stack1))
{
if(p!=NULL)
{
printf("%d ",p->data);
push(&stack1,p);
p = p->left;
}
else
{
p=pop(&stack1);
p=p->right;
}
}
}
/*while(p!=NULL)
{
//printf("%d ",p->data);
if(p->left!=NULL)
{
printf("%d ",p->data);
push(&stack1,p);
p=p->left;
}
else
{
if(p->right!=NULL)
p=p->right;
printf("%d ",p->data);
struct TreeNode* tmp;
tmp = pop(&stack1);
while(tmp->right==NULL)
{tmp = pop(&stack1);}
p = tmp->right;
}
} //push(&stack1,r);
//pop()*/
/*void layerprint(struct TreeNode* r)
{
queue q1;
q1.s=&q1.node[0];
q1.e=&q1.node[0];
}*/
void in(struct queue* a,struct TreeNode* b)
{
*a->e=*b;
//*a->e->left = *b->left;
//*a->e->right =*b->right;
a->e++;
}
struct TreeNode* out(struct queue* a)
{
struct TreeNode *tmp=(struct TreeNode*)malloc(sizeof(struct TreeNode));
*tmp = *a->s;
a->s++;
return tmp;
}
void layerprint(struct TreeNode* r)
{
struct queue q1;
q1.s=&q1.node[0];
q1.e=&q1.node[0];
struct TreeNode* p;
in(&q1,r);
while(q1.e!=q1.s)
{
p = out(&q1);
printf("%d ",p->data);
if(p->left !=NULL)
in(&q1,p->left);
if(p->right!=NULL)
in(&q1,p->right);
}
}
int main()
{
//struct stack stack1;
//stack1.top = &stack1.node[0];
int max;
printf("input the num:");
scanf("%d",&max);
//printf("please input 6 num
");
struct TreeNode *r;
r=initTree();
int i=0;
for(;i<max-1;i++)
createTree(r);
//PrePrint(r);
layerprint(r);
return 0;
}
\fun.文件内容
#include<stdio.h>
#include<stdlib.h>
struct TreeNode
{
int data;
struct TreeNode* left;
struct TreeNode* right;
}node;
struct TreeNode* initTree()
{
struct TreeNode* r = (struct TreeNode* )malloc(sizeof(struct TreeNode));
r->left = NULL;
r->right =NULL;
int num;
scanf("%d",&num);
r->data = num;
return r;
}
void createTree(struct TreeNode* r)
{
struct TreeNode *tmp = r;
struct TreeNode *p ;
int num;
scanf("%d",&num);
struct TreeNode *a = (struct TreeNode*)malloc(sizeof(struct TreeNode));
a->left = NULL;
a->right = NULL;
a->data = num;
while(tmp!=NULL)
{
if(num >= tmp->data)
{
p=tmp;
tmp = tmp->right;
}
else
{
p=tmp;
tmp = tmp->left;
}
}
if(a->data>=p->data)
{
p->right = a;
}
else
{
p->left = a;
}
}
struct TreeNode* insert(struct TreeNode* r,int num)
{
/*struct TreeNode* a = (struct TreeNode*)malloc(sizeof(struct TreeNode));
int num;
scanf("%d",&num);
a->data = num;
a->left = NULL;
a->right = NULL;*/
if(r==NULL)
{
struct TreeNode* a = (struct TreeNode*)malloc(sizeof(struct TreeNode));
a->data = num;
a->left = NULL;
a->right = NULL;
return a;
}
else
{
if(num >= r->data)
{
r->right = insert(r->right,num);
}
else
{
r->left = insert(r->left,num);
}
return r;
}
}
void preOrderSearch(struct TreeNode* r)
{
if(r==NULL)
return ;
else
{
preOrderSearch(r->left);
printf("%d ",r->data);
preOrderSearch(r->right);
}
}