c语言实现二叉树的先序,中序,后序以及层序遍历
初学数据结构,如若发现代码有错误,或有需要改进的地方,欢迎发送邮件到1149065115@qq.com
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct treenode *bintree;
typedef struct node *Stack;
typedef struct Queue *que;
struct treenode
{
char data;
int tag;
bintree left;
bintree right;
};
struct node
{
bintree data;
Stack next;
};
struct Queue
{
Stack front;
Stack rear;
};
void Createtree(bintree *t); //先序遍历创建一个二叉树
void PreorderTravelsal0(bintree t); //递归实现先序遍历
void PreorderTravelsal(bintree BT); //非递归实现中序遍历
void LevelorderTravelsal(bintree BT); //非递归实现层序遍历
void PostorderTravelsal(bintree BT); //非递归实现后序遍历
Stack Createstack(void); //创建一个有头结点的堆栈
void Push(Stack s, bintree t); //入栈
bintree Pop(Stack s); //出栈
int Isempty(Stack s); //判断堆栈是否为空
que Createque(void); //创建一个带头结点的队列
void addque(que q, bintree t); //入队
bintree deleteque(que q); //出队
int IsEmpty(que q); //判断队列是否为空
int main(void)
{
bintree t;
printf("先序遍历输入二叉树数\n");
Createtree(&t); //测试序列abd##e##cf###
printf("递归实现先序遍历\n");
PreorderTravelsal0(t);
printf("\n非递归实现中序遍历\n");
PreorderTravelsal(t);
printf("非递归实现层序遍历\n");
LevelorderTravelsal(t);
printf("非递归实现后序遍历\n");
PostorderTravelsal(t);
printf("over\n");
return 0;
}
void Createtree(bintree *t) //先序遍历创建一个二叉树
{//读入#表示空
char c;
scanf(" %c",&c);
if(c == '#')
*t = NULL;
else
{
*t = (bintree)malloc(sizeof(struct treenode));
if(!*t)
{
printf("error\n");
exit(-1);
}
(*t)->data = c;
Createtree(&(*t)->left);
Createtree(&(*t)->right);
}
}
void PreorderTravelsal0(bintree t) //递归实现先序遍历
{
if(t)
{
printf("%c ",t->data);
PreorderTravelsal0(t->left);
PreorderTravelsal0(t->right);
}
}
Stack Createstack(void) //创建一个有头结点的堆栈
{
Stack t = (Stack)malloc(sizeof(struct node));
t->next = NULL;
return t;
}
void Push(Stack s, bintree t) //入栈
{
Stack temp = (Stack)malloc(sizeof(struct node));
temp->data = t;
temp->next = s->next;
s->next = temp;
}
bintree Pop(Stack s) //出栈
{
Stack temp;
bintree y;
temp = s->next;
s->next = temp->next;
y = temp->data;
free(temp);
return y;
}
int Isempty(Stack s) //判断堆栈是否为空
{
if(s->next == NULL)
return 1;
else
return 0;
}
void PreorderTravelsal(bintree BT) //非递归实现中序遍历
{
bintree t = BT;
Stack s;
if(t == NULL)
{
printf("the tree is empty\n");
return;
}
s = Createstack();
while(t || !Isempty(s))
{
while(t)
{
/*printf("%c ",t->data); 先序遍历*/
Push(s, t);
t = t->left;
}
if(!Isempty(s))
{
t = Pop(s);
printf("%c ",t->data);
t = t->right;
}
}
putchar('\n');
}
que Createque(void) //创建一个头结点队列
{
que y = (que)malloc(sizeof(struct Queue));
y->front = (Stack)malloc(sizeof(struct node));
y->rear = y->front;
return y;
}
void addque(que q, bintree t) //入队
{
Stack temp = (Stack)malloc(sizeof(struct node));
temp->data = t;
temp->next = NULL;
q->rear->next = temp;
q->rear = temp;
}
bintree deleteque(que q) //出队 注意考虑队列只有一个节点的情况
{
Stack temp;
bintree y;
if(q->front->next == q->rear)
{
temp = q->front->next;
y = temp->data;
q->front->next = NULL;
q->rear = q->front;
free(temp);
return y;
}
else
{
temp = q->front->next;
y = temp->data;
q->front->next = temp->next;
free(temp);
return y;
}
}
int IsEmpty(que q) //判断队列是否为空
{
if(q->front == q->rear)
return 1;
else
return 0;
}
void LevelorderTravelsal(bintree BT) //非递归实现层序遍历
{
que q;
bintree t = BT;
if(!BT)
{
printf("the tree is empty\n");
return;
}
q = Createque();
addque(q, t);
while(!IsEmpty(q))
{
t = deleteque(q);
printf("%c ",t->data);
if(t->left)
{
addque(q, t->left);
}
if(t->right)
{
addque(q, t->right);
}
}
putchar('\n');
}
void PostorderTravelsal(bintree BT) //非递归实现后序遍历
{
bintree t = BT;
Stack s = Createstack();
while(t || !Isempty(s))
{
while(t)
{
t->tag = 0;
Push(s,t);
t = t->left;
}
if(!Isempty(s))
{
t = Pop(s);
if(t->tag == 0)
{
t->tag = 1;
Push(s,t);
t = t->right;
}
else if(t->tag == 1)
{
printf("%c ",t->data);
t = NULL;
}
else
{
printf("error\n");
return;
}
}
}
putchar('\n');
}