대학을 위해 프로젝트를 완료해야하지만 어떻게 할 수 있는지 알 수 없습니다.이진 검색 트리 노드 구조가없는 재귀
문제는 다음과 같은 주어진 기능을 가진 이진 검색 트리 응용 프로그램을 만들고 싶다는 것입니다. 어떤 종류의 재귀를 빌드해야하지만 내 문제는 bst_insert (tree * bst, int key) 함수가 트리을 입력으로 사용하고 노드를 사용하지 않는다는 것입니다. 그래서 아래에 작성한 아이디어 (bst_insert (bst-> root_node-> left, key);)가 작동하지 않습니다.
누군가가 내가 해결할 솔루션을 얻기 위해 무엇을 할 수 있는지 알고 있습니까?
감사합니다. 여기
는bst->root_node->left
을 통과, 여기
typedef struct node {
int key;
struct node *left;
struct node *right;
} node;
typedef struct tree {
node *root_node;
int (*compare_keys)(int x, int y);
} tree;
void bst_insert(tree *bst, int key);
당신이 허용 된 경우에도 tree.c 파일
void init(tree *bst) {
bst->root_node = 0;
bst->compare_keys = 0;
}
void bst_insert(tree *bst, int key) {
if (bst->root_node == NULL) {
bst->root_node = (node*)malloc(sizeof(node));
bst->root_node->key = key;
bst->root_node->left = NULL;
bst->root_node->right = NULL;
}
else {
if (key < bst->root_node->key) {
bst_insert(bst->root_node->left, key);
}
if (key > bst->root_node->key) {
bst_insert(bst->root_node->right, key);
}
}
}
분명히 프로젝트를 지정한 사람과 의견이 다릅니다. 당신은 아마 그들과 이야기해야합니다. – EOF
"함수는 노드가 아닌 입력으로 트리를 사용합니다"- 이것이 규칙입니다. – babon
노드를 가져 가기 위해'bst_insert'를 변경하지 않는 이유는 무엇입니까? – 4386427