二叉树(Binary tree)是树形结构的一个重要类型。许多实际问题抽象出来的数据结构往往是二叉树形式,即使是一般的树也能简单地转换为二叉树,而且二叉树的存储结构及其算法都较为简单,因此二叉树显得特别重要。二叉树特点是每个结点最多只能有两棵子树,且有左右之分 。
本程序可实现二叉树的构建,先中后序遍历,层次遍历以及左右交换功能。
#include <bits/stdc++.h> //万能头文件
//FCA##DB###EH##GM###
using namespace std;//分配空间
typedef struct TNode//定义二叉树的结构体
{
char data;
int quan;
int LTag,RTag;
struct TNode *lchild,*rchild;
}DogEgg,*DogSon;
DogSon pre;//线索时要用到的全局变量
DogSon Stack[100];
DogSon Stack2[100];//存放二叉树结点的栈
int high(DogSon T)//二叉树深度
{
int m , n;
if (T==0) return 0;
else
{
m=high(T->lchild);
n=high(T->rchild);
if(m>n) return (m+1);
else return (n+1);
}
}
DogSon CreateTree()//创建二叉树
{
DogSon T;
char c;
cin >> c;
if('#'==c){T=NULL;}
else
{
T=(DogSon)malloc(sizeof(TNode));
T->data=c;
T->lchild=CreateTree();
T->rchild=CreateTree();
}
return T;
}
int printfT0(DogSon T)//中序
{
if(!T) return -1;
if(T)
{
printfT0(T->lchild);
cout << T->data;
printfT0(T->rchild);
}
return 1;
}
void printfT1(DogSon T)//后序
{
if(T)
{
printfT1(T->lchild);
printfT1(T->rchild);
cout << T->data;
}
}
void printfT2(DogSon T)//先序
{
if(T)
{
cout << T->data;
printfT2(T->lchild);
printfT2(T->rchild);
}
}
void iprintfT1(DogSon T)//后序非递归
{
DogSon p = T, S[100], pre;
int top = 0, flag = 1;
if (p)
do {
while (p)
{
S[top++] = p;
p = p->lchild;
}
// p所有左节点入栈
flag = 1;
pre = new DogEgg;
while (top != 0 && flag == 1)
{
p = S[top - 1];
if ( p->rchild == NULL|| p->rchild == pre)
{
//右孩子不存在或右孩子已访问
top--;
cout<<p->data;
pre = p;
//指向被访问节点
}
else
{
//继续遍历右子树
p = p->rchild;
flag = 0;
}
}
}
while (top != 0);
{
cout <<endl;
}
}
void iprintfT0(DogSon T)//中序非递归
{
DogSon p;
p=T;
int top=-1;
while (p||top>-1)
{
if(p)
{
Stack[++top]=p;
p=p->lchild;
}
else
{
p=Stack[top];
cout << Stack[top]->data ;
top--;
p=p->rchild;
}
}
}
void iprintfT2(DogSon T)//前序非递归
{
DogSon p;
p=T;
int top=-1;
while (p||top>-1)
{
if(p)
{
Stack[++top]=p;
cout << Stack[top]->data ;
p=p->lchild;
}
else
{
p=Stack[top];
top--;
p=p->rchild;
}
}
}
int cengciprintf(DogSon T,int level) //层次遍历
{
if (!T || level < 0)
return 0;
if (0 == level) {
cout << T->data << " ";
return 1;
}
return cengciprintf(T->lchild, level - 1) + cengciprintf(T->rchild, level - 1);
}
void cengciprintf1(DogSon T)
{
int i = 0;
for (i = 0; ; i++) {
if (!cengciprintf(T, i))
break;
}
cout << endl;
}
void InThreading(DogSon T) //线索二叉树
{
if(!T)
{
InThreading(T->lchild);
if (!T->lchild)
{
T->LTag=1;
T->lchild=pre;
}
else T->LTag=0;
if(!pre->lchild)
{
pre->RTag=1;
pre->rchild=T;
}
else pre->RTag = 0;
pre=T;
InThreading(T->rchild);
}
}
void PRINT(DogSon T)
{
DogSon p;
p=T->lchild;
while(p!=T)
{
while(p->LTag==0) p=p->lchild;
cout << p->data;
while(p->RTag==1&&p->rchild!=T)
{
p=p->rchild;cout<<p->data;
}
p=p->rchild;
}
}
void change(DogSon T,DogSon &NewT)//左右交换
{
if(T==NULL)
{
return;
}
else
{
NewT=new DogEgg;
NewT->data=T->data;
change(T->rchild,NewT->lchild);
change(T->lchild,NewT->rchild);
}
}
void menu(DogSon T)
{
DogSon NewT;
int level=0;
while(1)
{
int a;
printf(" 1.=====中序 2.=====后序\n");
printf(" 3.=====先序 4.=====深度\n");
printf(" 5.=====线索 6.\n");
printf(" 7.=====中序非递归 8.=====后序非递归\n");
printf(" 9.=====先序非递归 10.=====层次遍历\n");
printf("11.=====左右交换\n");
printf("——————请输入操作序号——————\n");
cin >> a;
switch(a)
{
case 1:printfT0(T); cout << "完毕" <<endl;break;
case 2:printfT1(T); cout << "完毕" <<endl;break;
case 3:printfT2(T); cout << "完毕" <<endl;break;
case 4:cout << "该二叉树深度为:" ;
cout <<high(T)<<endl; cout << "完毕" <<endl;break;
case 5:InThreading(T); cout << "完毕" <<endl;break;
case 6:PRINT(T); cout << "完毕" <<endl;break;
case 7:iprintfT0(T); cout << "完毕" <<endl;break;
case 8:iprintfT1(T); cout << "完毕" <<endl;break;
case 9:iprintfT2(T); cout << "完毕" <<endl;break;
case 10:cengciprintf1(T); cout << "完毕" <<endl;break;
case 11:change(T,NewT);T=NewT;cout << "完毕" <<endl;break;
}
}
}
int main()
{
cout << "输入你要创建的二叉树" << endl;
DogSon T;
T=CreateTree();
menu(T);
return 0;
}