精简代码见新的链接:
https://blog.csdn.net/weixin_45888287/article/details/109727927
//严蔚敏《数据结构》
//二叉链表: 递归、非递归(非递归用栈)先序、中序、后序遍历(非递归后序遍历麻烦点,需要用到flag标志)
//以及层序遍历(层序用队列)
//自学中,加油!!!
//过程很痛,但结果很美
#include<iostream>
using namespace std;
#define TElemType double
typedef struct BiTNode
{
TElemType data;
struct BiTNode* lchild,* rchild;
int flag;//标志变量 标志非递归的后序遍历
}BiTNode,* BiTree;
bool Creat_BiTree(BiTree& T)//递归先序创建并输入
{
TElemType num;
if(cin>>num){//此处应用if而不是while循环 这是在用递归,就不用while循环了
T=new BiTNode;
if(!T)
return false;
T->data=num;
Creat_BiTree(T->lchild);
Creat_BiTree(T->rchild);
}
if(!cin)
T=nullptr;
if(!cin){
cin.clear();
while(cin.get()!='\n')
continue;
}
return true;
}
void PreOrder_Bitree(BiTree T)//递归先序遍历
{
if(T){
cout<<T->data<<endl;
PreOrder_Bitree(T->lchild);
PreOrder_Bitree(T->rchild);
}
}
void InOrder_bitree(BiTree T)//递归中序遍历
{
if(T){
InOrder_bitree(T->lchild);
cout<<T->data<<endl;
InOrder_bitree(T->rchild);
}
}
void PostOrder_BiTree(BiTree T)//递归后序遍历
{
if(T){
PostOrder_BiTree(T->lchild);
PostOrder_BiTree(T->rchild);
cout<<T->data<<endl;
}
}
//构造的