二进制的分割故障
我正在研究一个程序,该程序通过程序进行搜索,并输出它的数量大于或等于函数参数中指定的值。我已将代码附加在下面,我看到它在发生分割故障之前遍历了两个值。分段故障错误指向返回count + count_values(value,ever) + count_values(value,current);
>
typedef struct BinaryTree {
int val;
struct BinaryTree *left;
struct BinaryTree *right;
} BinaryTree;
BinaryTree *build_tree(int value, BinaryTree *leftnode, BinaryTree *rightnode) {
BinaryTree *out = calloc(1, sizeof(BinaryTree));
out->val = value;
out->leftnode = leftnode;
out->rightnode = rightnode;
return out;
}
int count_values(int value, BinaryTree *tree) {
BinaryTree *current = tree;
BinaryTree *here = current;
int count = 0;
if (current != NULL) {
printf("Value in tree is not NULL\n");
if (current->val < value) {
printf("%d < %d\n", current->val, value);
printf("Count value: %d\n", count);
here = current->leftnode;
count_values(value, here);
current = current->rightnode;
count_values(value, current);
} else if (current->val == value) {
printf("%d = %d\n", current->val, value);
count++;
printf("Count value: %d\n", count);
here = current->leftnode;
count_values(value, here);
current = current->rightnode;
count_values(value, current);
} else {
printf("%d > %d\n", current->val, value);
count++;
printf("Count value: %d\n", count);
here = current->leftnode;
count_values(value, here);
current = current->rightnode;
count_values(value, current);
}
}
return count + count_values(value, here) + count_values(value, current);
}
int main(void) {
BinaryTree *tree =
build_tree(14, build_tree(3, NULL, NULL),
build_tree(15, NULL, build_tree(42, NULL, NULL)));
//path is 14->15->42
int count = count_values(42, tree);
printf("should have a count of 1, got %d\n", count);
count = count_values(14, tree);
printf("should have a count of 3, got %d\n", count);
return 0;
}
I'm working on a program that searches through a program and outputs the number of times it comes across a number greater than or equal to the value specified in the parameter of the function. I have attached my code below, I see it traverses two of the values before a segmentation fault occurs. The segmentation fault error points to return count + count_values(value, here) + count_values(value, current);
typedef struct BinaryTree {
int val;
struct BinaryTree *left;
struct BinaryTree *right;
} BinaryTree;
BinaryTree *build_tree(int value, BinaryTree *leftnode, BinaryTree *rightnode) {
BinaryTree *out = calloc(1, sizeof(BinaryTree));
out->val = value;
out->leftnode = leftnode;
out->rightnode = rightnode;
return out;
}
int count_values(int value, BinaryTree *tree) {
BinaryTree *current = tree;
BinaryTree *here = current;
int count = 0;
if (current != NULL) {
printf("Value in tree is not NULL\n");
if (current->val < value) {
printf("%d < %d\n", current->val, value);
printf("Count value: %d\n", count);
here = current->leftnode;
count_values(value, here);
current = current->rightnode;
count_values(value, current);
} else if (current->val == value) {
printf("%d = %d\n", current->val, value);
count++;
printf("Count value: %d\n", count);
here = current->leftnode;
count_values(value, here);
current = current->rightnode;
count_values(value, current);
} else {
printf("%d > %d\n", current->val, value);
count++;
printf("Count value: %d\n", count);
here = current->leftnode;
count_values(value, here);
current = current->rightnode;
count_values(value, current);
}
}
return count + count_values(value, here) + count_values(value, current);
}
int main(void) {
BinaryTree *tree =
build_tree(14, build_tree(3, NULL, NULL),
build_tree(15, NULL, build_tree(42, NULL, NULL)));
//path is 14->15->42
int count = count_values(42, tree);
printf("should have a count of 1, got %d\n", count);
count = count_values(14, tree);
printf("should have a count of 3, got %d\n", count);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
中,如果(current-&gt; val!= null){
current-&gt; value永远不可能为null指针,因为值不是指针。只是int。您可能应该检查当前
,而不是current-&gt; val
,返回count + count_values(value,there) + count_values(value,current);
lineP.S 中您发布的代码需要进行一些调整才能构建。如果可以的话,请修复
if (current->val != NULL) {
current->value can never be NULL pointer as value isn't pointer. It is just int. You probably should checkcurrent
instead ofcurrent->val
return count + count_values(value, here) + count_values(value, current);
lineP.S. the code you posted needed some adjustments to build. Fix it if you can
对于初学者,这两个函数中都有错别字,例如
,
您必须编写
Count_values 中的函数在此If语句
调用不确定的行为。
首先,这是没有意义的。其次,在访问节点的数据成员之前,您需要检查指针
当前
(更准确地说,指针tree
)是否是空指针。注意这样的陈述
实际上没有影响。
可以按照以下方式定义递归函数,
将树指针应为第一个函数参数,并使用限定器
const
声明,因为该函数不会更改树。并且该函数应返回未签名整数类型的对象,因为计数器不能为负值。代替返回类型size_t
您可以使用例如类型无符号int
。这是一个演示程序。
它的输出是
For starters there are typos in the both functions as for example
and
You have to write
and
Within the function
count_values
already this if statementinvokes undefined behavior.
First of all it does not make a sense. And secondly before accessing data members of a node you need to check whether the pointer
current
(more precisely the pointertree
) is a null pointer or not.Pay attention to that such statements like these
in fact have no effect.
The recursive function could be defined the following way
The pointer to the tree should be the first function parameter and declared with the qualifier
const
because the function does not change the tree. And the function should return an object of an unsigned integer type because the counter can not be a negative value. Instead of the return typesize_t
you may use for example the typeunsigned int
.Here is a demonstration program.
Its output is
您使这种方式比需要更难。
通过测试
tree
是null
,首先测试基本情况。如果不是,请在左和右节点上重复,并将这些计数添加到当前节点的计数中。
You're making this way harder than it needs to be.
First test for the base case by testing whether
tree
isNULL
.If not, recurse on the left and right nodes, and add these counts to the count for the current node.