二叉树链式存储的,提供了两种的建树方式,一。没有返回值的,二。有返回值的,遍历的方法,主要是运用了递归的思想,不是很难可以理解
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
#define max 100
typedef struct Bitee{
char data;
struct Bitee *lchild,*rchild;
}Bitree;
Bitree *creatree()
{
char c; Bitree *T;
cin>>c;
if(c=='*') T=NULL;
else
{
T=(Bitree *)malloc(sizeof(Bitree));
T->data=c;
T->lchild=creatree();
T->rchild=creatree();
}
return T;
}
void creatree1(Bitree *&T)
{
char c;
cin>>c;
if(c=='*') T=NULL;
else
{
T=(Bitree *)malloc(sizeof(Bitree));
T->data=c;
creatree1(T->lchild);
creatree1(T->rchild);
}
}
void xianxu(Bitree *T)
{
if(T!=NULL)
{
cout<<T->data<<" ";
xianxu(T->lchild);
xianxu(T->rchild);
}
}
void zhongxu(Bitree *T)
{
if(T!=NULL)
{
zhongxu(T->lchild);
cout<<T->data<<" ";
zhongxu(T->rchild);
}
}
void houxu(Bitree *T)
{
if(T!=NULL)
{
houxu(T->lchild);
houxu(T->rchild);
cout<<T->data<<" ";
}
}
int main()
{
Bitree *T;
cout<<"请按照先序的顺序输入二叉树的存储数据,如果值为空用“*”代替\n" ;
T=creatree();
//creatree1(T);//用第二种方法来建树不需要返回值
cout<<"树的前序\n";
xianxu(T);
cout<<"\n树的中序\n";
zhongxu(T);
cout<<"\n树的后序\n";
houxu(T);
}