// 二叉树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;
}
//
// 极限二叉树.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;
}