主题:二叉树关于表达式的问题
大哥大姐们:
为什么我的遍历程序输不出来啊 ???
谁知道俺的错误在哪啊 ??俺是菜鸟哦!!
谢谢了!!
这是我的程序:
#include<iostream>
using namespace std;
typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;
}bitnode;
//static
bitnode *root;
void crtree(char s[],int i,int j,bitnode *p)
{// int l;
// cout<<i<<j<<endl;
// cin>>l;
//bitnode *p;
if(p)
{
int k,n=0,m;
if(i==j)
{
p=new bitnode;
p->data=s[i];
p->lchild=NULL;
p->rchild=NULL;
//return p;
}
for(k=i;k<=j;k++)
{
if(s[k]=='+'||s[k]=='-')
{
n++;
m=k;
}
}
if(n==0)
for(k=i;k<=j;k++)
if(s[k]=='*'||s[k]=='/')
{
n++;
m=k;
}
else
{
p=new bitnode;
p->data=s[n];
crtree(s,i,n-1,p->lchild);
crtree(s,n+1,j,p->rchild);
//return p;
}
}
}
/*bitnode *creatroot()
{
bitnode *root;
//n=new bitnode;
// root=new bitnode;
cout<<"输入根结点"<<endl;
char s[111]="5+3*6-5";
// cin>>s;
root=crtree(s,0,strlen((s)-1));
//cout<<root->data;
return root;
}*/
void preorder(bitnode *p)
{ // char j; cout<<"sdkja"<<endl;
if(p)
{
cout<<p->data<<" ";
preorder(p->lchild);
preorder(p->rchild);
}
}
void inorder(bitnode *p)
{//char j;
if(p)
{
inorder(p->lchild);
cout<<p->data<<" ";
inorder(p->rchild);
}
}
void postorder(bitnode *p)
{//char j;
if(p)
{
postorder(p->lchild);
postorder(p->rchild);
cout<<p->data<<" ";
}
}
void main()
{
int i,h;char j;
//bitnode *root;
//n=new bitnode;
// root=new bitnode;
char s[111]="5+3*6-5";
// cin>>s;
crtree(s,0,strlen((s)-1),root);
//cout<<root->data;
// return root;
do{
system("cls");
cout<<" "<<"********************"<<" "<<endl;
cout<<" "<<" 二叉树功能演示 "<<" "<<endl;
cout<<" "<<"【1】:创建二叉树。 "<<" "<<endl;
cout<<" "<<"【2】:前序递归遍历。"<<" "<<endl;
cout<<" "<<"【3】:中序递归遍历。"<<" "<<endl;
cout<<" "<<"【4】:后序递归遍历。"<<" "<<endl;
cout<<" "<<"【0】:退出程序!!!"<<" "<<endl;
cin>>i;
switch(i)
{
//case 1: creatroot();break;
case 2: preorder(root); cout<<endl;
cout<<"按任意键继续。"<<endl;cin>>j;break;
case 3: inorder(root); cout<<endl;
cout<<"按任意键继续。"<<endl;cin>>j;break;
case 4: postorder(root);cout<<endl;
cout<<"按任意键继续。"<<endl;cin>>j;break;
}
}while(i!=0);
}
为什么我的遍历程序输不出来啊 ???
谁知道俺的错误在哪啊 ??俺是菜鸟哦!!
谢谢了!!
这是我的程序:
#include<iostream>
using namespace std;
typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;
}bitnode;
//static
bitnode *root;
void crtree(char s[],int i,int j,bitnode *p)
{// int l;
// cout<<i<<j<<endl;
// cin>>l;
//bitnode *p;
if(p)
{
int k,n=0,m;
if(i==j)
{
p=new bitnode;
p->data=s[i];
p->lchild=NULL;
p->rchild=NULL;
//return p;
}
for(k=i;k<=j;k++)
{
if(s[k]=='+'||s[k]=='-')
{
n++;
m=k;
}
}
if(n==0)
for(k=i;k<=j;k++)
if(s[k]=='*'||s[k]=='/')
{
n++;
m=k;
}
else
{
p=new bitnode;
p->data=s[n];
crtree(s,i,n-1,p->lchild);
crtree(s,n+1,j,p->rchild);
//return p;
}
}
}
/*bitnode *creatroot()
{
bitnode *root;
//n=new bitnode;
// root=new bitnode;
cout<<"输入根结点"<<endl;
char s[111]="5+3*6-5";
// cin>>s;
root=crtree(s,0,strlen((s)-1));
//cout<<root->data;
return root;
}*/
void preorder(bitnode *p)
{ // char j; cout<<"sdkja"<<endl;
if(p)
{
cout<<p->data<<" ";
preorder(p->lchild);
preorder(p->rchild);
}
}
void inorder(bitnode *p)
{//char j;
if(p)
{
inorder(p->lchild);
cout<<p->data<<" ";
inorder(p->rchild);
}
}
void postorder(bitnode *p)
{//char j;
if(p)
{
postorder(p->lchild);
postorder(p->rchild);
cout<<p->data<<" ";
}
}
void main()
{
int i,h;char j;
//bitnode *root;
//n=new bitnode;
// root=new bitnode;
char s[111]="5+3*6-5";
// cin>>s;
crtree(s,0,strlen((s)-1),root);
//cout<<root->data;
// return root;
do{
system("cls");
cout<<" "<<"********************"<<" "<<endl;
cout<<" "<<" 二叉树功能演示 "<<" "<<endl;
cout<<" "<<"【1】:创建二叉树。 "<<" "<<endl;
cout<<" "<<"【2】:前序递归遍历。"<<" "<<endl;
cout<<" "<<"【3】:中序递归遍历。"<<" "<<endl;
cout<<" "<<"【4】:后序递归遍历。"<<" "<<endl;
cout<<" "<<"【0】:退出程序!!!"<<" "<<endl;
cin>>i;
switch(i)
{
//case 1: creatroot();break;
case 2: preorder(root); cout<<endl;
cout<<"按任意键继续。"<<endl;cin>>j;break;
case 3: inorder(root); cout<<endl;
cout<<"按任意键继续。"<<endl;cin>>j;break;
case 4: postorder(root);cout<<endl;
cout<<"按任意键继续。"<<endl;cin>>j;break;
}
}while(i!=0);
}