我正在開發一個程式,該程式搜索程式并輸出它遇到大于或等于函式引數中指定的值的數字的次數。我在下面附上了我的代碼,我看到它在發生分段錯誤之前遍歷了兩個值。分段錯誤錯誤指向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;
}
uj5u.com熱心網友回復:
- 在
if (current->val != NULL) {current->value 中永遠不能是 NULL 指標,因為 value 不是指標。它只是整數。您可能應該檢查current而不是current->val - 即使您更正了該行,您的程式也會因段錯誤導致堆疊溢位(呵呵),因為您無條件地從其自身
return count count_values(value, here) count_values(value, current);在線呼叫您的函式
PS您發布的代碼需要一些調整才能構建。可以的話修一下
uj5u.com熱心網友回復:
對于初學者來說,這兩個功能都有拼寫錯誤,例如
out->leftnode = leftnode;
out->rightnode = rightnode;
和
here = current->leftnode;
current = current->rightnode;
你必須寫
out->left = leftnode;
out->right = rightnode;
和
here = current->left;
current = current->right;
在函式內count_values已經有 this if 陳述句
if (current->val != NULL) {
呼叫未定義的行為。
首先,它沒有任何意義。其次,在訪問節點的資料成員之前,您需要檢查指標current(更準確地說是指標tree)是否為空指標。
注意像這樣的陳述
count_values(value, here);
count_values(value, current);
實際上沒有任何效果。
遞回函式可以通過以下方式定義
size_t count_values( const BinaryTree *tree, int value )
{
return tree == NULL
? 0
: ( tree->val >= value ) count_values( tree->left, value ) count_values( tree->right, value );
}
指向樹的指標應該是第一個函式引數并使用限定符宣告,const因為該函式不會更改樹。并且該函式應該回傳一個無符號整數型別的物件,因為計數器不能是負值。size_t例如,您可以使用type 代替回傳型別unsigned int。
這是一個演示程式。
#include <stdio.h>
#include <stdlib.h>
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->left = leftnode;
out->right = rightnode;
return out;
}
size_t count_values( const BinaryTree *tree, int value )
{
return tree == NULL
? 0
: ( tree->val >= value ) count_values( tree->left, value ) count_values( tree->right, value );
}
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
size_t count = count_values( tree, 42);
printf("should have a count of 1, got %zu\n", count);
count = count_values(tree, 14 );
printf("should have a count of 3, got %zu\n", count);
return 0;
}
它的輸出是
should have a count of 1, got 1
should have a count of 3, got 3
uj5u.com熱心網友回復:
你讓這種方式變得比它需要的更難。
首先通過測驗是否tree是來測驗基本情況NULL。
如果不是,則在左右節點上遞回,并將這些計數添加到當前節點的計數中。
int count_values(int value, BinaryTree *tree) {
if (tree == NULL) {
return 0;
}
int count = tree->val >= value;
count = count_values(value, tree->left);
count = count_values(value, tree->right);
return count;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/488059.html
