因为我用的cin,不能读入空格,所以树空的地方用-代替
一种是自己写的栈的数据结构,没有用到栈的库函数
//给一个二叉树,要求前序中序后序递归非递归输出
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<fstream>
#include<queue>
using namespace std;
typedef char datatype;
typedef struct BiNode
{
datatype data;
struct BiNode *lchild, *rchild;
}BiNode, *BiTree;
typedef struct seqstack //栈的操作,为非递归实现做准备
{
BiTree data[100];
int flag[100];
int top; //栈顶元素的位置
}seqstack;
void Push(seqstack *s, BiTree T)
{
s->data[++s->top] = T;
}
BiTree Pop(seqstack *s)
{
if (s->top != -1)
return s->data[s->top--];
else
return NULL;
}
void CreateTree(BiTree *T) //按前序方式输入并建立一棵树
{
datatype ch;
cin >> ch;
if (ch == '-')
{
(*T) = NULL;
return;
}
else
{
*T = new BiNode;
(*T)->data = ch;
CreateTree(&(*T)->lchild);
CreateTree(&(*T)->rchild);
}
}
void PreOrder1(BiTree T) //前序递归
{
if (T)
{
cout << T->data << " ";
PreOrder1(T->lchild);
PreOrder1(T->rchild);
}
}
void PreOrder2(BiTree T)
{
seqstack s;
s.top = -1; //栈初始化,top初始值为-1;
while ((s.top != -1) || T)
{
while (T)
{
cout << T->data << " ";
Push(&s, T);
T = T->lchild;
}
if (s.top != -1)
{
T = Pop(&s);
T = T->rchild;
}
}
}
void InOrder1(BiTree T) //中序递归
{
if (T)
{
InOrder1(T->lchild);
cout << T->data << " ";
InOrder1(T->rchild);
}
}
void InOrder2(BiTree T) //中序非递归
{
seqstack s;
s.top = -1; //栈初始化,top初始值为-1;
while ((s.top != -1) || T)
{
if (T)
{
Push(&s, T);
T = T->lchild;
}
else
{
T = Pop(&s);
cout << T->data << " ";
T = T->rchild;
}
}
}
void PostOrder1(BiTree T) //后序递归
{
if (T)
{
PostOrder1(T->lchild);
PostOrder1(T->rchild);
cout << T->data << " ";
}
}
void PostOrder2(BiTree T)
{
seqstack s;
s.top = -1;
while (T || (s.top != -1))
{
while (T)
{
Push(&s, T);
s.flag[s.top] = 0; //设此根结点标志初始化为0,表示左右孩子都么有访问,当访问完左子树,tag变为1;
T = T->lchild;
}
while ((s.top != -1) && (s.flag[s.top] == 1))
{
//左右子树都访问过了,就可以输出当前节点
T = Pop(&s);
cout << T->data << " ";
}
if (s.top != -1) //不大懂。。。。。。。。。
{
T = s.data[s.top];
s.flag[s.top] = 1; //进入右孩子前,标志tag变为1;
T = T->rchild;
}
else
T = NULL;
}
}
int main()
{
BiTree T;
cout << "输入一棵树,空的地方输入-" << endl;
CreateTree(&T);
cout << "前序递归:" << endl;
PreOrder1(T);
cout << endl << "前序非递归:" << endl;
PreOrder2(T);
cout << endl << "中序递归:" << endl;
InOrder1(T);
cout << endl << "中序非递归:" << endl;
InOrder2(T);
cout << endl << "后序递归:" << endl;
PostOrder1(T);
cout << endl << "后序非递归:" << endl;
PostOrder2(T);
system("pause");
return 0;
}
一种是直接用的栈的库函数,区别在于库函数的Stack.pop()没有返回值,要先T=Stack.top()(队列是front()),再Stack.pop(),我自己写的函数可以直接让pop()返回BiTree型。
//给一个二叉树,要求前序中序后序递归非递归输出
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<fstream>
#include<queue>
#include<stack>
using namespace std;
typedef char datatype;
typedef struct BiNode
{
datatype data;
struct BiNode *lchild, *rchild;
}BiNode, *BiTree;
/*如果用栈的库函数做,就不需要自己写栈相关的函数了
typedef struct seqstack //栈的操作,为非递归实现做准备
{
BiTree data[100];
int flag[100];
int top; //栈顶元素的位置
}seqstack;
void Push(seqstack *s, BiTree T)
{
s->data[++s->top] = T;
}
BiTree Pop(seqstack *s)
{
if (s->top != -1)
return s->data[s->top--];
else
return NULL;
}
*/
void CreateTree(BiTree *T) //按前序方式输入并建立一棵树
{
datatype ch;
cin >> ch;
if (ch == '-')
{
(*T) = NULL;
return;
}
else
{
*T = new BiNode;
(*T)->data = ch;
CreateTree(&(*T)->lchild);
CreateTree(&(*T)->rchild);
}
}
void PreOrder1(BiTree T) //前序递归
{
if (T)
{
cout << T->data << " ";
PreOrder1(T->lchild);
PreOrder1(T->rchild);
}
}
void PreOrder2(BiTree T)
{
/*
seqstack s;
s.top = -1; //栈初始化,top初始值为-1;
*/
stack<BiTree> s;
while (!s.empty() || T)
{
while (T)
{
cout << T->data << " ";
//Push(&s, T);
s.push(T);
T = T->lchild;
}
if (!s.empty())
{
//T = Pop(&s);
T = s.top();
s.pop();
T = T->rchild;
}
}
}
void InOrder1(BiTree T) //中序递归
{
if (T)
{
InOrder1(T->lchild);
cout << T->data << " ";
InOrder1(T->rchild);
}
}
void InOrder2(BiTree T) //中序非递归
{
stack<BiTree> s;
//s.top = -1; //栈初始化,top初始值为-1;
while (!s.empty() || T)
{
if (T)
{
s.push(T);
T = T->lchild;
}
else
{
//T = Pop(&s);
T = s.top();
s.pop();
cout << T->data << " ";
T = T->rchild;
}
}
}
void PostOrder1(BiTree T) //后序递归
{
if (T)
{
PostOrder1(T->lchild);
PostOrder1(T->rchild);
cout << T->data << " ";
}
}
void PostOrder2(BiTree T)
{
stack<BiTree> s;
int flag[100] = { 0 };
//s.top = -1;
while (T)
{
s.push(T);
//Push(&s, T);
//s.flag[s.top] = 0; //设此根结点标志初始化为0,表示左右孩子都么有访问,当访问完左子树,tag变为1;
flag[s.size()] = 0;
T = T->lchild;
}
while (!s.empty())
{
T = s.top();
while (T && flag[s.size()] == 0)
{
//进入右孩子前,标志tag变为1
flag[s.size()] = 1;
//s.flag[s.top] = 1; ;
T = T->rchild;
while (T) //访问右子树
{
s.push(T);
flag[s.size()] = 0;
T = T->lchild;
}
}
T = s.top();
cout << T->data << " ";
s.pop();
}
}
void LeverOrder(BiTree T)
{
queue<BiTree> Queue;
BiTree P;
Queue.push(T); //将根节点入队
while (!Queue.empty())
{
P = Queue.front(); //队头元素出队
Queue.pop();
cout << P->data << " ";
if (P->lchild)
Queue.push(P->lchild); //左子树不为空,则左子树入列
if (P->rchild)
Queue.push(P->rchild); //右子树不为空,则右子树入列
}
}
int main()
{
BiTree T;
cout << "输入一棵树,空的地方输入-" << endl;
CreateTree(&T);
cout << "前序递归:" << endl;
PreOrder1(T);
cout << endl << "前序非递归:" << endl;
PreOrder2(T);
cout << endl << "中序递归:" << endl;
InOrder1(T);
cout << endl << "中序非递归:" << endl;
InOrder2(T);
cout << endl << "后序递归:" << endl;
PostOrder1(T);
cout << endl << "后序非递归:" << endl;
PostOrder2(T);
cout << endl << "层序递归:" << endl;
LeverOrder(T);
system("pause");
return 0;
}