读书人

关于二叉树的创建小疑点一枚【新手求教

发布时间: 2012-09-27 11:11:17 作者: rapoo

关于二叉树的创建小问题一枚【新手求教】

typedef struct BiNode{

char data;

struct BiNode *lch;

struct BiNode *rch;

}BiNode,*BiTree;



//先序拓展序列建立二叉树

void Create(BiTree &T) //重点他妈在这里!!!!!!!!!!!!!!!!!!小弟的疑点在于为什么函数的参数是&T,如果是T的话将不能正常出来遍历结果!

{

T =(BiNode*) malloc (sizeof(BiNode));



printf("Enter the data \n");

cin>>T->data;

if(T->data=='#') T = NULL;

if(T){

printf("");

Create(T->lch);

Create(T->rch);

}

}
void Preorder (BiTree T)

{

if (T) {

cout<<T->data<<' '; // 访问根结点



Preorder(T->lch); // 遍历左子树

Preorder(T->rch);// 遍历右子树

}

}

int main()

{

//建树

printf("The fuction Create() is called.\n");

BiTree T;

Create(T);



//三种遍历递归算法

printf("\n");

printf("The fuction Preorder() is called.\n");

Preorder(T);


printf("\n");

printf("The fuction Inorder() is called.\n");

Inorder(T);



printf("\n");

printf("The fuction Postorder() is called.\n");

Postorder(T);





printf("\n");

system("pause");

return 0;

}

[解决办法]
因为,LCH.RCH都是指针,而你的对象也是用的指针,所以是指针对象指向它的成员指针,参数是指针引用,你不喜欢&的话,就用*吧。

读书人网 >C语言

热点推荐