C语言中的二叉搜索树

C语言中的二叉搜索树,c,struct,C,Struct,作为练习,我一直在尝试用C实现一个简单的二叉搜索树。我可以将元素插入到树中,但在某些点上(我无法找出位置),我遇到了分段错误 这是我的密码: #include <stdio.h> #include <stdlib.h> struct node { struct node *left; struct node *right; int key; }; void insert(struct node *treeNode, int key); void

作为练习,我一直在尝试用C实现一个简单的二叉搜索树。我可以将元素插入到树中,但在某些点上(我无法找出位置),我遇到了分段错误

这是我的密码:

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

struct node {
    struct node *left;
    struct node *right;
    int key;
};

void insert(struct node *treeNode, int key);
void outputTree(struct node *root);

int main(){
    //Store how many numbers the user will enter
    printf("How many numbers will you enter? > ");
    int numNumbers;
    scanf("%d", &numNumbers);

    //Create a root node
    struct node root;
    root.key = -1; //-1 Means the root node has not yet been set
    root.right = NULL;
    root.left = NULL;

    //Now iterate numNumbers times
    int i;
    for(i = 1; i <= numNumbers; ++i){
        int input;
        scanf("%d", &input);
        insert(&root, input);
    }

    outputTree(&root);

    return 0;
}

void insert(struct node *treeNode, int key){
    //First check if the node is the root node
    if((*treeNode).key == -1){
        printf("Root node is not set\n");
        (*treeNode).key = key; //If the root node hasn't been initialised
    }
    else {
        //Create a child node containing the key
        struct node childNode;
        childNode.key = key;
        childNode.left = NULL;
        childNode.right = NULL;

        //If less than, go to the left, otherwise go right
        if(key < (*treeNode).key){
            if((*treeNode).left != NULL){ 
                printf("Left node is not null, traversing\n");
                insert((*treeNode).left, key);
            }
            else {
                printf("Left node is null, creating new child\n");
                (*treeNode).left = &childNode;
            }
        }
        else {
            //Check if right child is null
            if((*treeNode).right != NULL){
                printf("Right node is not null, traversing...\n");
                insert((*treeNode).right, key);
            }
            else {
                printf("Right node is null, creating new child\n");
                (*treeNode).right = &childNode;
            }
        }
    }
}

void outputTree(struct node *root){
    //Traverse left
    if((*root).left != NULL){
        outputTree((*root).left);
    }
    printf("%d\n", (*root).key);
    if((*root).right != NULL){
        outputTree((*root).right);
    }
}
#包括
#包括
结构节点{
结构节点*左;
结构节点*右;
int键;
};
void insert(结构节点*treeNode,int键);
void输出树(结构节点*根节点);
int main(){
//存储用户将输入的数字
printf(“您将输入多少个数字?>”;
整数;
scanf(“%d”和numNumbers);
//创建根节点
结构节点根;
root.key=-1;//-1表示尚未设置根节点
root.right=NULL;
root.left=NULL;
//现在迭代numNumbers次
int i;

对于(i=1;i您可以通过静态分配在堆栈上创建child节点。当insert方法完成时,child引用将无效。 您应该对malloc使用动态分配

struct node *new_node(int key, struct node *left, struct node *right) {
    struct node *this = malloc(sizeof *this);
    this->key = key;
    this->left = left;
    this->right = right;
    return this;
}
不要忘记使用free函数释放所有分配

编辑: 因此,要创建根,只需使用

struct node *root = new_node(-1, NULL, NULL);

当您使用调试器逐步检查代码时,您应该能够准确地确定是哪一行导致了分段错误。您应该在发布到此处之前执行此操作。Re:“在写这个问题时,我刚刚想到,子节点是在堆栈上创建的,所以当递归调用返回时,树中的引用指向不再存在的结构?":完全正确!您正在堆栈上创建
childNode
,该函数在函数结束后无效。您应该使用
malloc
或类似工具。
childNode
的范围仅限于插入函数使用动态内存分配malloc或calloc将内存分配给childNode。当前,childNode是invalid在insert函数外部。无关:不写
(*treeNode).key
但写
treeNode->key
。这是等效的,但没有人写
(*treeNode).key