二叉树的建立
struct TreeNode{
char val;
TreeNode* left;
TreeNode* right;
};
void CreateBiTree(TreeNode *&T)
{
cout<<"&T = "<<&T<<" *&T= "<<*&T<<endl;
char ch;
cin>>ch;
if(ch=='#')
T=NULL;
else
{
T = new TreeNode;
if(!T)
return;
(T)->val = ch;
CreateBiTree(T->left);
CreateBiTree(T->right);
}
}
void PreTraverse(TreeNode* &T){
if(T==NULL)
return;
cout<<"&T = "<<&T<<" *&T= "<<*&T<<endl;
cout<<T->val<<endl;
PreTraverse(T->left);
PreTraverse(T->right);
}
int main()
{
TreeNode *T;
// cout<<"T = "<<T<<endl;
cout<<"&T = "<<&T;
cout<<" *&T= "<<*&T<<endl;
CreateBiTree(T);
cout<<endl;PreTraverse(T);
结果如下:
&T = 003DFCDC *&T= CCCCCCCC
&T = 003DFCDC *&T= CCCCCCCC
ab#c###
&T = 002A4FF4 *&T= CDCDCDCD //b
&T = 002A503C *&T= CDCDCDCD //#
&T = 002A5040 *&T= CDCDCDCD //c
&T = 002A5084 *&T= CDCDCDCD //#
&T = 002A5088 *&T= CDCDCDCD //#
&T = 002A4FF8 *&T= CDCDCDCD //#
&T = 003DFCDC *&T= 002A4FF0
a
&T = 002A4FF4 *&T= 002A5038
b
&T = 002A5040 *&T= 002A5080
c
如果函数参数为int*& a,在函数里面对a的值进行改变,那么在调用函数时传入的参数的指针值也会改变