我的二进制搜索树程序中的分段错误

时间:2014-07-14 13:17:16

标签: c data-structures binary-search-tree

当我运行程序来创建二叉搜索树时,我遇到了一个seg错误。该程序应该从用户读取2个数字。第一个是我想要创建多少个节点,第二个是头节点是什么。在这些节点中,我存储了一个从1到10的随机值。我在运行它时遇到了一个seg错误,在运行我的调试器后,它告诉我seg错误是在我打印头节点时。可能有更多的错误,因为这是我第一次做二分查找树。我将发布我的代码,然后给出一些示例输出。非常感谢您的帮助。

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

typedef struct TreeNode_ {
    int value;
    struct TreeNode_ *left;
    struct TreeNode_ *right;
}TreeNode;


TreeNode* insert(TreeNode* node, const int value);
TreeNode* create_node(const int value);
void destroy(TreeNode* node);
void display(TreeNode* node);

int main (int argc, char *argv[]) {
    if (argc < 3) {
        printf("%s is missing parameters\n", argv[0]);
        return 0;
    }
    const int size = atoi(argv[1]);
    const int root_val = atoi(argv[2]);
    TreeNode* root = NULL;
    int i;
    root = insert(root,root_val);
    printf("Head node is %d\n", root->value);
    for (i = 0; i < size; ++i) {
        int value = rand() % 10 + 1;
        insert(root,value);
    }
    display(root);
    destroy(root);
}

TreeNode *create_node(const int value){
    TreeNode *node;
    node = (TreeNode*)malloc(sizeof(TreeNode));
    node->value = value;
    node->left = 0;
    node->right = 0;
    return node;
}

void display(TreeNode *node){
    if(node == 0) return;
    if(node->left != 0)
         display(node->left);

    printf("\n%d\n", node->value);

    if(node->right != 0)
            display(node->right);
}

TreeNode *insert(TreeNode *node, const int value){
    if(node == 0)
             return;

    if(node != 0){
        if(value < node->value){
            insert(node->left, value);
        }
        else if(value > node->value){
            insert(node->right, value);
        }
        else
            return;
    }
    node = create_node(value);
    return node;
}

void destroy(TreeNode *node){
    if(node == 0) return;

    destroy(node->left);
    destroy(node->right);
    free(node);
    node = 0;
}

示例输出:

./a.out 6 5 
Head node is 5 
4 
4 
5 
6 
6 
7 
8

1 个答案:

答案 0 :(得分:0)

你走了。不要在下次发布完整的代码,尝试只放置出现seg错误的部分。我还在您的main中添加了srand(time(NULL));来初始化随机数生成器。

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

typedef struct TreeNode_ {
    int value;
    struct TreeNode_ *left;
    struct TreeNode_ *right;
}TreeNode;


TreeNode* insert(TreeNode* node, const int value);
TreeNode* create_node(const int value);
void destroy(TreeNode* node);
void display(TreeNode* node);

int main (int argc, char *argv[]) {
            if (argc < 3) {
                    printf("%s is missing parameters\n", argv[0]);
                    return 0;
            }

            srand(time(NULL));

            const int size = atoi(argv[1]);
            const int root_val = atoi(argv[2]);
            TreeNode* root = NULL;
            int i;
            root = insert(root,root_val);
            printf("Head node is %d\n", root->value);
            for (i = 0; i < size; ++i) {
                    int value = rand() % 10 + 1;
                    insert(root,value);
            }
            display(root);
            destroy(root);

}

TreeNode *create_node(const int value){

    TreeNode *node;
    node = (TreeNode*)malloc(sizeof(TreeNode));
    node->value = value;
    node->left = NULL;
    node->right = NULL;
    return node;

}

void display(TreeNode *node){

    if(node == 0) return;
    if(node->left != 0)
         display(node->left);

    printf("\n%d\n", node->value);

    if(node->right != 0)
            display(node->right);
}

TreeNode * insert(TreeNode *node, const int value){
    if(node == NULL)
        node = create_node(value);

    else if(node != NULL){
            if(value <= node->value){
                    node->left = insert(node->left, value);
            }
            else if(value > node->value){
                    node->right = insert(node->right, value);
            }
    }

    return node;

}

void destroy(TreeNode *node){

    if(node == 0) return;

    destroy(node->left);
    destroy(node->right);
    free(node);
    node = 0;
}