代码:
#include <iostream>
#include <stack>
using namespace std;
#include <stack>
using namespace std;
typedef struct node
{
struct node *lchild;
struct node *rchild;
char data;
}BiTreeNode, *BiTree;
{
struct node *lchild;
struct node *rchild;
char data;
}BiTreeNode, *BiTree;
//按照前序顺序建立二叉树(递归)
void createBiTree(BiTree &T)
{
char c;
cin >> c;
if('#' == c)
T = NULL;
else
{
T = new BiTreeNode;
T->data=c;
createBiTree(T->lchild);
createBiTree(T->rchild);
}
}
//前序遍历二叉树并打印(递归)
void preTraverse(BiTree T)
{
if(T)
{
cout<<T->data<<" ";
preTraverse(T->lchild);
preTraverse(T->rchild);
}
}
void preTraverse(BiTree T)
{
if(T)
{
cout<<T->data<<" ";
preTraverse(T->lchild);
preTraverse(T->rchild);
}
}
//非递归前序遍历
void _preTraverse(BiTree T)
{
void _preTraverse(BiTree T)
{
stack<BiTree> node_stack;
while(T!=nullptr||!node_stack.empty()){
while(T!=nullptr||!node_stack.empty()){
if(T!=nullptr){
std::cout << T->data << ' ';
node_stack.push(T);
T = T->lchild;
}
std::cout << T->data << ' ';
node_stack.push(T);
T = T->lchild;
}
else{
T = node_stack.top();
T = T->rchild;
node_stack.pop();
}
T = node_stack.top();
T = T->rchild;
node_stack.pop();
}
}
}
//中序遍历二叉树并打印(递归)
void midTraverse(BiTree T)
{
if(T)
{
midTraverse(T->lchild);
cout<<T->data<<" ";
midTraverse(T->rchild);
}
}
//非递归中序遍历;
void _midTraverse(BiTree T)
{
//中序遍历二叉树并打印(递归)
void midTraverse(BiTree T)
{
if(T)
{
midTraverse(T->lchild);
cout<<T->data<<" ";
midTraverse(T->rchild);
}
}
//非递归中序遍历;
void _midTraverse(BiTree T)
{
stack<BiTree> node_stack;
while(T!=nullptr||!node_stack.empty()){
if(T!=nullptr){
node_stack.push(T);
T = T->lchild;
}
else
{
T = node_stack.top();
std::cout << T->data << ' ';
T = T->rchild;
node_stack.pop();
}
}
while(T!=nullptr||!node_stack.empty()){
if(T!=nullptr){
node_stack.push(T);
T = T->lchild;
}
else
{
T = node_stack.top();
std::cout << T->data << ' ';
T = T->rchild;
node_stack.pop();
}
}
}
//后续遍历二叉树并打印(递归)
void postTraverse(BiTree T)
{
if(T)
{
postTraverse(T->lchild);
postTraverse(T->rchild);
cout<<T->data<<" ";
}
}
//后续遍历二叉树并打印(递归)
void postTraverse(BiTree T)
{
if(T)
{
postTraverse(T->lchild);
postTraverse(T->rchild);
cout<<T->data<<" ";
}
}
//非递归后续遍历;
void _postTraverse(BiTree T)
{
stack<BiTree> node_stack;
BiTree thelastvivst = T;
while (T!=nullptr||!node_stack.empty()) {
void _postTraverse(BiTree T)
{
stack<BiTree> node_stack;
BiTree thelastvivst = T;
while (T!=nullptr||!node_stack.empty()) {
if(T!=nullptr)
{
node_stack.push(T);
T = T->lchild;
}
{
node_stack.push(T);
T = T->lchild;
}
else
{
T = node_stack.top();
if(T->rchild==nullptr||T->rchild==thelastvivst)
{
std::cout << T->data << ' ';
thelastvivst = T;
node_stack.pop();
}
else
{
T = T->rchild;
}
}
}
{
T = node_stack.top();
if(T->rchild==nullptr||T->rchild==thelastvivst)
{
std::cout << T->data << ' ';
thelastvivst = T;
node_stack.pop();
}
else
{
T = T->rchild;
}
}
}
}
int main()
{
BiTree T; //声明一个指向二叉树根节点的指针
int main()
{
BiTree T; //声明一个指向二叉树根节点的指针
createBiTree(T);
cout<<"二叉树创建完成!"<<'
';
cout<<'
'<<"前序遍历二叉树:"<<'
';
preTraverse(T);
std::cout << '
'<<"非递归前序遍历:" << '
';
_preTraverse(T);
cout<<'
'<<"中序遍历二叉树:"<<endl;
midTraverse(T);
std::cout <<'
'<< "非递归中序遍历:" << '
';
_midTraverse(T);
cout<<'
'<<"后序遍历二叉树:"<<endl;
postTraverse(T);
std::cout <<'
'<< "非递归后序遍历:" << '
';
postTraverse(T);
return 0;
}
}
测试结果:
![](https://img2020.cnblogs.com/blog/1765425/202008/1765425-20200808020952756-2005837828.png)