临时写一个 不知是否和题意
#include<iostream>
using namespace std;
struct node
{
char data;
node *left;
node *right;
};
class tree
{
public:
tree();
~tree()
{
Delete(root);
}
private:
void Create(node*);
void Delete(node*);
node *root;
};
tree::tree()
{
cout<<"输入树的根节点(如果为空输入#):\t";
root=new node;
cin>>root->data;
if(root->data!='#')
Create(root);
else
root=0;
}
void tree::Create(node *p)
{
if(p)
{
char x,y; node *q;
cout<<"输入节点"<<p->data<<"的左孩子和右孩子:\t";
cin>>x>>y;
if(x=='#')
p->left=0;
else
{
q=new node;
q->data=x;
p->left=q;
}
if(y=='#')
p->right=0;
else
{
q=new node;
q->data=y;
p->right=q;
}
Create(p->left);
Create(p->right);
}
}
void tree::Delete(node *p)
{
if(p)
{
Delete(p->left);
Delete(p->right);
delete p;
}
}
void main()
{
tree h;
}
[此贴子已经被作者于2006-5-30 21:56:14编辑过]