Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C BST中通向最大叶的元素之和_C_Binary Search Tree - Fatal编程技术网

C BST中通向最大叶的元素之和

C BST中通向最大叶的元素之和,c,binary-search-tree,C,Binary Search Tree,我试图求二叉搜索树中最大叶的所有节点的和。节点仅包含正数 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <time.h> typedef int ElType; typedef struct Tree { ElType key; struct Tree *left; struct Tree *right; } Tree; Tree

我试图求二叉搜索树中最大叶的所有节点的和。节点仅包含正数

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>

typedef int ElType;

typedef struct Tree {
    ElType key;
    struct Tree *left;
    struct Tree *right;
} Tree;

Tree* InsertBST(Tree* t, int k)
{
    if (t == NULL) {
        Tree* w = (Tree*) malloc(sizeof(Tree));
        w->key = k;
        w->left = NULL;
        w->right = NULL;
        return w;
    }

    if (k <= t->key)
        t->left = InsertBST(t->left, k);
    else
        t->right = InsertBST(t->right, k);

    return t;
}

int SumMaxOfBST(Tree* t, int *sum_max)
{
    if (t == NULL) {
        *sum_max = -1;
        return *sum_max;
    }

    if (t->right == NULL) {
        *sum_max += t->key;
        return *sum_max;
    }

    *sum_max += t->key;

    *sum_max += SumMaxOfBST(t->right, sum_max);
    return *sum_max;
}

int main()
{
    int i;
    srand (time(NULL));

    Tree* t = NULL;
    for (i = 0; i < 20; i++)
        t = InsertBST(t, rand() % 1000);

    int sum_way = 0;
    int a = SumMaxOfBST(t, sum_way);

      printf("Sum on the way to the largest leaf %d:\n", a);

    return 0;
}
#包括
#包括
#包括
#包括
typedef int-ElType;
类型定义结构树{
ElType键;
结构树*左;
结构树*右;
}树木;
树*InsertBST(树*t,intk)
{
如果(t==NULL){
Tree*w=(Tree*)malloc(sizeof(Tree));
w->key=k;
w->left=NULL;
w->right=NULL;
返回w;
}
如果(k键)
t->left=InsertBST(t->left,k);
其他的
t->right=InsertBST(t->right,k);
返回t;
}
int SumMaxOfBST(树*t,int*sum_max)
{
如果(t==NULL){
*sum_max=-1;
返回*sum_max;
}
如果(t->right==NULL){
*sum_max+=t->key;
返回*sum_max;
}
*sum_max+=t->key;
*sum_max+=SumMaxOfBST(t->右,sum_max);
返回*sum_max;
}
int main()
{
int i;
srand(时间(空));
Tree*t=NULL;
对于(i=0;i<20;i++)
t=InsertBST(t,rand()%1000);
int sum_way=0;
int a=总和fbst(t,总和);
printf(“向最大叶%d:\n”求和,a);
返回0;
}

这将以非零状态退出。我强烈的怀疑是,我把指针的使用搞砸了,然而,在对指针的使用进行了几次重写和视频之后,我似乎仍然不明白发生了什么。如果我理解正确,
*sum_max+=x
应该将
sum_max
的值增加
x
。我在哪一点上禁用了指针?

我不明白为什么要将int指针作为
SumMaxOfBST
的参数,我认为这样编写的函数更简单

int SumMaxOfBST(Tree* t)
{
    if (t == NULL) {
        return 0;
    }
    if (t->right == NULL) {
        return t->key;
    }
    return t->+key + SumMaxOfBST(t->right);
}

此外,在您的
main
中,您正在通过
sum\u-way
传递一个
int
,而
SumMaxOfBST
需要一个
int*
。您应该改为传递
&sum\u way

我不明白为什么要将int指针作为
SumMaxOfBST
的参数,我认为这样编写的函数更简单

int SumMaxOfBST(Tree* t)
{
    if (t == NULL) {
        return 0;
    }
    if (t->right == NULL) {
        return t->key;
    }
    return t->+key + SumMaxOfBST(t->right);
}

此外,在您的
main
中,您正在通过
sum\u-way
传递一个
int
,而
SumMaxOfBST
需要一个
int*
。你应该通过
&sum\u-way

你的编译器应该抱怨调用
sum\u-way
之前没有
&sum\u-way
int a=SumMaxOfBST(t,sum\u-way)。请注意编译器的警告-编译器是对的,而您是错的,至少在您的C代码职业生涯的这个阶段是这样。另外,
声明了
malloc()
等。除非您使用了头的扩展功能,否则没有必要包含
。而且,您的代码无法验证您得到的答案是否正确;您没有打印树,因此无法知道您的计算是否正确。您的编译器应该抱怨调用
sum\u way
之前缺少
&
,调用
int a=SumMaxOfBST(t,sum\u way)。请注意编译器的警告-编译器是对的,而您是错的,至少在您的C代码职业生涯的这个阶段是这样。另外,
声明了
malloc()
等。除非您使用了头的扩展功能,否则没有必要包含
。而且,您的代码无法验证您得到的答案是否正确;你没有打印树,所以你无法知道你的计算是否正确。谢谢,这确实比我的尝试干净得多。谢谢,这确实比我的尝试干净得多。