rkakar 2016-04-20 08:31 采纳率: 25%
浏览 1367

精通数据结构的帮忙看看,为什么参数是这个?

//是这样的,如果TreeInsert()中的第一个参数是Tree * T就无法遍历整个树,而传入 Tree * &才可以,为什么?是因为引用吗?
//还是因为其他原因?
#include
#include
#include
typedef struct tree {
int number;
struct tree * left;
struct tree * right;
}Tree;
void print(Tree * T);
Tree * TreeInsert(Tree* T, int n) {
if (T == NULL) {
T = (Tree *)malloc(sizeof(Tree));
if (T == NULL) {
printf("Out of space!");
return NULL;
}
else {
T->number = n;
T->left = T->right = NULL;
}
}
else if (n < T->number) {
TreeInsert(T->left, n);
}
else if (n >T->number) {
TreeInsert(T->right, n);
}
print(T);
return T;
}
void print(Tree * T) {
if (T == NULL) {
return;
}
else {
print(T->left);
printf("%4d", T->number);
print(T->right);
}
}
void main() {
int temp, n, i = 0;
printf("please input a number:");
scanf("%d", &n);
Tree * Ntree = NULL;
for (i = 0;i < n;i++) {
printf("Now please input a number:");
scanf("%d", &temp);
TreeInsert(Ntree, temp);
}
print(Ntree);

}

  • 写回答

1条回答

  • threenewbee 2016-04-20 11:53
    关注

    TreeInsert(Ntree, temp);
    ->
    Ntree = TreeInsert(Ntree, temp);
    这样就不一定要引用了。

    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集