首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

二叉树求改错,该如何处理

2012-12-31 
二叉树求改错小弟只是菜鸟!!!写了个二叉树,报错了!!不知道怎么改#include iostreamusing namespace std

二叉树求改错
小弟只是菜鸟!!!写了个二叉树,报错了!!不知道怎么改

#include <iostream>
using namespace std;
struct Node
{
char data;
Node *lchild,*rchild;
};
class BinTree
{
protected:
Node *root;
public:

void BinTree::Creat(Node *root)  //创建二叉树
{
char ch;
root=NULL;
cin>>ch;
if(ch=='#')
root=NULL;
else
{
root=new Node;
root->data=ch;
Creat(root->lchild);
Creat(root->rchild);
}
}
void BinTree::PreOrder(Node *root)  //先序递归历遍
{
if(root!=NULL)
{
cout<<root->data<<" ";
PreOrder(root->lchild);
PreOrder(root->rchild);
}
}
void BinTree::InOredr(Node *root)  //中序递归历遍
{
    
if(root!=NULL)
{
PreOrder(root->lchild);
cout<<root->data<<" ";
PreOrder(root->rchild);
}
}  
};
void main()
{
Node *Root;
Root=new Node;
BinTree BT;
cout<<"请输入结点的值,#表示结束: "<<endl;
BT.Creat(Root);
cout<<"树的前序访问遍序为:"<<endl;
BT.PreOrder(Root);
cout<<endl;
cout<<"树的中序访问遍序为:"<<endl;
BT.InOredr(Root);
cout<<endl;
}

[解决办法]

//OK了,拿去玩吧
#include <iostream>
using namespace std;
struct Node
{
char data;
Node *lchild,*rchild;
};
class BinTree
{
protected:
Node *root;
public:

void BinTree::Creat(Node **root)  //创建二叉树,修改为指针的指针,,
{
char ch;
*root=NULL;
cin>>ch;
if(ch=='#')
*root=NULL;
else
{
*root=new Node;
(*root)->data=ch;
Creat(&((*root)->lchild));
Creat(&((*root)->rchild));
}
}
void BinTree::PreOrder(Node *root)  //先序递归历遍
{
if(root!=NULL)
{
cout<<root->data<<" ";
PreOrder(root->lchild);
PreOrder(root->rchild);
}
}
void BinTree::InOredr(Node *root)  //中序递归历遍
{
    
if(root!=NULL)
{
InOredr(root->lchild);//中序遍历修改
cout<<root->data<<" ";
InOredr(root->rchild);
}
}  
};
void main()
{
Node *Root;
Root=new Node;
BinTree BT;
cout<<"请输入结点的值,#表示结束: "<<endl;
BT.Creat(&Root);
cout<<"树的前序访问遍序为:"<<endl;
BT.PreOrder(Root);
cout<<endl;
cout<<"树的中序访问遍序为:"<<endl;
BT.InOredr(Root);
cout<<endl;
}


热点排行