二叉树的创建和递归遍历和非递归遍历

// 二叉树789.cpp : 定义控制台应用程序的入口点。
//



// 极限二叉树.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#define maxsize 100


//树的结构体
typedef struct tree{
char data;
struct tree *left;
struct tree *right;
}*ptree, tnode;


//栈的数据结构
typedef struct stack{
ptree num[100];
int top;
}*pstack, snode;


//队列结构体
typedef struct queue{
ptree num1[100];
int front;
int rear;
}*pqueue, qnode;


//初始化队列
pqueue init1()
{
pqueue q;
q = (pqueue)malloc(sizeof qnode);
q->front = q->rear = 0;
return q;
}


//判断队列是否为空
int isempty1(pqueue q)
{
if (q->front == q->rear)
return 1;
else
return 0;
}


//进队操作
int enqueue(pqueue q, ptree t)
{
if (q->rear == maxsize - 1)
{
printf("队列已经满了!\n");
return 0;
}
else
{
++q->rear;
q->num1[q->rear] = t;
}
return 1;
}


//出队操作
ptree outqueue(pqueue q)
{
ptree temp;
if (q->front == q->rear)
{
printf("队列为空!\n");
return 0;
}
else
{
++q->front;
temp = q->num1[q->front];
return temp;
}
}


//初始化栈
pstack init()
{
pstack p;
p = (pstack)malloc(sizeof snode);
p->top = -1;
return p;
}


//判断栈空
int isempty(pstack p)
{
if (p->top == -1)
return 1;
else
return 0;
}


//入栈操作
int pushstack(pstack p, ptree t)
{
if (p->top + 1 == maxsize)
{
printf("栈已经满了!\n");
return 0;
}
else
{
p->top = p->top + 1;
p->num[p->top] = t;
}
return 1;
}


//出栈操作
ptree popstack(pstack p)
{
ptree itme;
if (p->top == -1)
{
printf("栈为空!\n");
return 0;
}
else
{
itme = p->num[p->top];
p->top = p->top - 1;
}
return itme;
}


//返回栈顶元素
ptree gettop(pstack p)
{
return p->num[p->top];
}


//树的先序创建
ptree create(ptree t)
{
char ch;
scanf("%c", &ch);
if (ch == ',')
{
t = NULL;
return NULL;
}
else
{
t = (ptree)malloc(sizeof tnode);
t->data = ch;
t->left = create(t->left);
t->right = create(t->right);
}
return t;
}


//层序生成二叉树
ptree create1()
{
char ch;
ptree t;
pqueue q;
q = init1();
scanf("%d", &ch);
if (ch == ',')
{
t = NULL;
return t;
}
else
{
t = (ptree)malloc(sizeof tnode);
t->data = ch;
enqueue(q, t);
}
while (!isempty1(q))
{
t = outqueue(q);
scanf("%c", &ch);
if (ch == ',')
t->left = NULL;


else{
t->left = (ptree)malloc(sizeof tnode);
t->left->data = ch;
enqueue(q, t->left);
}
scanf("%c", &ch);
if (ch == ',')
t->right = NULL;
else
{
t->right = (ptree)malloc(sizeof tnode);
t->right->data = ch;
enqueue(q, t->right);
}
}
return t;
}


//递归先序遍历二叉树
void preorder(ptree t)
{
if (t){
printf("%c", t->data);
preorder(t->left);
preorder(t->right);
}
}


//非递归先序遍历二叉树
void preorder1(ptree t)
{
pstack p;
p = init();
while (t || !isempty(p))
{
while (t)
{
printf("%c", t->data);
pushstack(p, t);
t = t->left;
}
if (!isempty(p))
{
t = popstack(p);
t = t->right;
}
}
}


//树的递归中序遍历
void midorder(ptree t)
{
if (t){
midorder(t->left);
printf("%c", t->data);
midorder(t->right);
}
}


//树的非递归中序遍历
void midorder1(ptree t)
{
pstack p;
p = init();
while (t || !isempty(p))
{
while (t)
{
pushstack(p, t);
t = t->left;
}
if (!isempty(p))
{
t = popstack(p);
printf("%c", t->data);
t = t->right;
}
}
}


//树的递归后序遍历
void aftorder(ptree t)
{
if (t)
{
aftorder(t->left);
aftorder(t->right);
printf("%c", t->data);
}
}


//非递归后序遍历
void aftorder1(ptree t){
ptree at;
pstack p;
p = init();
at = t;
int tag[100];
while (at || !isempty(p))
{
while (at)
{
pushstack(p, at);
tag[p->top] = 0;
at = at->left;


}
while (!isempty(p) && tag[p->top])
{
at = popstack(p);
printf("%c", at->data);
}
if (!isempty(p))
{
tag[p->top] = 1;
at = gettop(p);
at = at->right;
}
else
break;
}
}


//层序遍历
void printfloor(ptree t)
{
pqueue q;
q = init1();
ptree at;
at = t;
if (at)
enqueue(q, at);
while (!isempty1(q))
{
at = outqueue(q);
printf("%c", at->data);
if (at->left)
enqueue(q, at->left);
if (at->right)
enqueue(q, at->right);
}
}
//主函数
int main()
{
ptree t;
t = NULL;
printf("请你输入你要创建二叉树输入的数据:\n");
t = create(t);
// printf("层序创建二叉树:\n");
// t = create1();
printf("递归前序遍历是:\n");
preorder(t);
printf("\n");
printf("非递归先序遍历二叉树是:\n");
preorder1(t);
printf("\n");
printf("递归中序遍历二叉树:\n");
midorder(t);
printf("\n");
printf("非递归中序遍历二叉树:\n");
midorder1(t);
printf("\n");
printf("树的递归后序遍历:\n");
aftorder(t);
printf("\n");
printf("树的非递归后序遍历:\n");
aftorder1(t);
printf("\n");
printf("树的层序遍历是:\n");
printfloor(t);
printf("\n");




return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值