主题:二叉树帮忙改错
各们大哥帮忙改一下,着急啊
#include<iostream>
using namespace std;
#define null 0
#define ERROR 0
#define OVERFLOW -2
#define OK 1
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;//定义树结点
int CreateBiTree(BiTree &T)//创建一棵树
{
char ch;
ch=getchar();//获取一个字符
if(ch==' ') T=null;
else
{
if(!(T=new BiTNode))
exit(OVERFLOW);
T->data=ch;
CreateBiTree(T->lchild);//建立左子树
CreateBiTree(T->rchild);//建立右子树
}
return OK;
}
int display(char e)
{
cout<<e<<" ";
return OK;
}
int OrderTraverse(BiTree T,char a)//遍历二叉树,三种方式
{
if(T)
{
if(a=='p')//先序遍历
{
if(display(T->data))
if(OrderTraverse(T->lchild,'p'))
if(OrderTraverse(T->rchild,'p'))
return OK;
}
else if(a=='i')//中序遍历
{
if(OrderTraverse(T->lchild,'i'))
if(display(T->data))
if(OrderTraverse(T->rchild,'i'))
return OK;
}
else if(a=='a')//后序遍历
{
if(OrderTraverse(T->lchild,'a'))
if(OrderTraverse(T->rchild,'a'))
if(display(T->data))
return OK;
}
return ERROR;
}
else
return OK;
}
int main()
{
BiTree T;
int t;
cout<<"请输入各结点的值,每输入一个值请按回车键(包括空格):"<<endl;
CreateBiTree(T);
cout<<"1先序遍历"<<" "<<"2中序遍历"<<" "<<"3后序遍历"<<endl;
cout<<"请输入你的选择:";
cin>>t;
cout<<"遍历结果为:"<<endl;
switch(t)
{
case 1:
OrderTraverse(T,'p');cout<<endl;break;//先序
case 2:
OrderTraverse(T,'i');cout<<endl;break;//中序
case 3:
OrderTraverse(T,'a');cout<<endl;break;//后序
default:
cout<<"你这个家伙乱输入."<<endl;//呵呵,输入错误的缺省处理。
main();
}
return 0;
}
#include<iostream>
using namespace std;
#define null 0
#define ERROR 0
#define OVERFLOW -2
#define OK 1
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;//定义树结点
int CreateBiTree(BiTree &T)//创建一棵树
{
char ch;
ch=getchar();//获取一个字符
if(ch==' ') T=null;
else
{
if(!(T=new BiTNode))
exit(OVERFLOW);
T->data=ch;
CreateBiTree(T->lchild);//建立左子树
CreateBiTree(T->rchild);//建立右子树
}
return OK;
}
int display(char e)
{
cout<<e<<" ";
return OK;
}
int OrderTraverse(BiTree T,char a)//遍历二叉树,三种方式
{
if(T)
{
if(a=='p')//先序遍历
{
if(display(T->data))
if(OrderTraverse(T->lchild,'p'))
if(OrderTraverse(T->rchild,'p'))
return OK;
}
else if(a=='i')//中序遍历
{
if(OrderTraverse(T->lchild,'i'))
if(display(T->data))
if(OrderTraverse(T->rchild,'i'))
return OK;
}
else if(a=='a')//后序遍历
{
if(OrderTraverse(T->lchild,'a'))
if(OrderTraverse(T->rchild,'a'))
if(display(T->data))
return OK;
}
return ERROR;
}
else
return OK;
}
int main()
{
BiTree T;
int t;
cout<<"请输入各结点的值,每输入一个值请按回车键(包括空格):"<<endl;
CreateBiTree(T);
cout<<"1先序遍历"<<" "<<"2中序遍历"<<" "<<"3后序遍历"<<endl;
cout<<"请输入你的选择:";
cin>>t;
cout<<"遍历结果为:"<<endl;
switch(t)
{
case 1:
OrderTraverse(T,'p');cout<<endl;break;//先序
case 2:
OrderTraverse(T,'i');cout<<endl;break;//中序
case 3:
OrderTraverse(T,'a');cout<<endl;break;//后序
default:
cout<<"你这个家伙乱输入."<<endl;//呵呵,输入错误的缺省处理。
main();
}
return 0;
}