主题:[讨论]关于二叉树
#include<iostream.h>
//树的存储结构
struct tree
{
char data;
struct tree*left;
struct tree*right;
};
char ch;
//创建二叉树
int Create( tree * T)
{
cin>>ch;
if(ch=='#')
{
delete T;
T=NULL; //设定为空
}
else
{
T->data=ch;
T->left=new tree;
Create(T->left);
T->right=new tree;
Create(T->right);
}
return 1;
}
main()
{
tree * root=new tree;
Create(root);
cout<<root->data<<" "<<root->left->data<<" "<<root->right->data<<endl;
cout<<root->left->left<<" "<<root->left->right<<" "<<root->right->left<<" "<<root->right->right<<endl;
return 0;
}
我输入的是 a b # # c # #
我想问下为什么树的最后节点的left和right指针为什么不是空呢?
//树的存储结构
struct tree
{
char data;
struct tree*left;
struct tree*right;
};
char ch;
//创建二叉树
int Create( tree * T)
{
cin>>ch;
if(ch=='#')
{
delete T;
T=NULL; //设定为空
}
else
{
T->data=ch;
T->left=new tree;
Create(T->left);
T->right=new tree;
Create(T->right);
}
return 1;
}
main()
{
tree * root=new tree;
Create(root);
cout<<root->data<<" "<<root->left->data<<" "<<root->right->data<<endl;
cout<<root->left->left<<" "<<root->left->right<<" "<<root->right->left<<" "<<root->right->right<<endl;
return 0;
}
我输入的是 a b # # c # #
我想问下为什么树的最后节点的left和right指针为什么不是空呢?