【问题标题】:Implementing an AVL tree, but getting seg fault on large data file insertion实现 AVL 树,但在插入大数据文件时出现段错误
【发布时间】:2021-01-22 16:38:55
【问题描述】:

使用 avl_tree 创建字典比较程序,但在读取 200,000 多个唯一单词的大文件时出现段错误。较小的文件似乎可以正常工作。

我的一些想法可能是我的 word_compre 函数可能会干扰旋转。或者我可能错误地执行了轮换。

// String compare based on alphabetical order
int word_compare(char *word, struct tree_node *root)
{
    return strcasecmp(word, root->word);
}

// Get the maximum of two integers
int get_max(int int_a, int int_b)
{
    if (int_a > int_b)
        return int_a;
    else
        return int_b;
}

// Get the height of the tree
int get_height(struct tree_node *root)
{
    if (root == NULL)
        return 0;
    else
        return root->height;
}

// Determine if the tree or subtree is unbalanced by comparing the max height
// of the left and right branches
int get_balance(struct tree_node *root)
{
    if (root == NULL)
        return 0;
    else
        return get_height(root->left) - get_height(root->right);
}

// Rotate left subtree rooted with right child node
struct tree_node *left_rotate(struct tree_node* root)
{
    // Store the right child node to rotate
    struct tree_node *right_child = root->right;

    // Store the left of the right child node to rotate
    struct tree_node *subtree = right_child->left;

    // Perform rotation
    right_child->left = root;
    root->right = subtree;

    // Update heights
    root->height = get_max(get_height(root->left),
                   get_height(root->right)) + 1;
    right_child->height = get_max(get_height(right_child->left),
                      get_height(right_child->right)) + 1;

    return right_child;
}

// Rotate right subtree rooted with left child node
struct tree_node *right_rotate(struct tree_node *root)
{
    // Store the left child node to rotate
    struct tree_node *left_child = root->left;

    // Store the right of the left child node to rotate
    struct tree_node *subtree = left_child->right;

    // Perform rotation
    left_child->right = root;
    root->left = subtree;

    // Update heights
    root->height = get_max(get_height(root->left),
                   get_height(root->right)) + 1;
    left_child->height = get_max(get_height(left_child->left),
                     get_height(left_child->right)) + 1;

    return left_child;
}

// Insert new node into a tree
struct tree_node *insert_node(struct tree_node *root, char *word, int fp_num)
{
    // Viable spot found, insert a new node
    if (root == NULL) {

        #ifdef DEBUG
        printf("INSERTED NODE\n");
        printf("WORD: %s\n", word);
        #endif

        if (fp_num == 1) {
            // Allocate a new node
            root = (struct tree_node *)malloc(sizeof(struct tree_node));

            // Fail to allocate a new node
            if (root == NULL) {
                printf("\nFailed to allocate a node\n");
                return NULL;
            }
            // Initialize the new node
            else {
                root->word = strdup(word);
                root->count = 1;
                root->left = NULL;
                root->right = NULL;
                root->height = 1;
                return root;
            }
        }
        else {
            return root;
        }
    }

    int exist = word_compare(word, root);

    if (exist < 0) {
        root->left = insert_node(root->left, word, fp_num);
    }
    else if (exist > 0) {
        root->right = insert_node(root->right, word, fp_num);
    }
    else {
        if (fp_num != 1 && root->count == fp_num - 1) {
            root->count = fp_num;
        }
    }


    // Check the balance of the tree
    int balance = get_balance(root);

    // If the tree is imbalanced, fixed them with one of the following cases
    // Left-Left-Case
    if (balance > 1 && (word_compare(word, root->left) < 0))
        return right_rotate(root);

    // Right-Right-Case
    if (balance < -1 && (word_compare(word, root->right) > 0))
        return left_rotate(root);

    // Left-Right-Case
    if (balance > 1 && (word_compare(word, root->left) > 0)) {
        root->left = left_rotate(root->left);
        return right_rotate(root);
    }
    // Right-Left-Case
    if (balance < -1 && (word_compare(word, root->right) < 0)) {
        root->right = right_rotate(root->right);
        return left_rotate(root);
    }

    return root;
}

// Perform in-order traversal sort of the tree and display results
void in_order(struct tree_node *root, int fp_num)
{
    if (root == NULL)
        return;

    // Recur until the left most node
    if (root->left)
        in_order(root->left, fp_num);

    // Find the amount of dollars from the cents and puts the
    // remainder after a decimal point
    if (fp_num != 1 && root->count == fp_num)
        printf("%s\n", root->word);

    // Recur until the right most node
    if (root->right)
        in_order(root->right, fp_num);
}


// Delete tree
void free_tree(struct tree_node *root)
{
    if (root != NULL)
    {
        free_tree(root->left);
        free(root->word);
        free_tree(root->right);
        free(root);
    }
}

【问题讨论】:

    标签: c struct avl-tree


    【解决方案1】:

    修复它。没有在插入函数中更新当前节点/根高度。

    // Insert new node into a tree
    struct tree_node *insert_node(struct tree_node *root, char *word, int fp_num)
    {
        // Viable spot found, insert a new node
        if (root == NULL) {
    
            #ifdef DEBUG
            printf("INSERTED NODE\n");
            printf("WORD: %s\n", word);
            #endif
    
            if (fp_num == 1) {
                // Allocate a new node
                root = (struct tree_node *)malloc(sizeof(struct tree_node));
    
                // Fail to allocate a new node
                if (root == NULL) {
                    printf("\nFailed to allocate a node\n");
                    return NULL;
                }
                // Initialize the new node
                else {
                    root->word = strdup(word);
                    root->count = 1;
                    root->left = NULL;
                    root->right = NULL;
                    root->height = 1;
                    return root;
                }
            }
            else {
                return root;
            }
        }
    
        int word_exist = word_compare(word, root);
    
        if (word_exist < 0) {
            root->left = insert_node(root->left, word, fp_num);
        }
        else if (word_exist > 0) {
            root->right = insert_node(root->right, word, fp_num);
        }
        else {
            if (fp_num != 1 && root->count == fp_num - 1) {
                root->count = fp_num;
            }
        }
    
        // Update height of current node
        root->height = 1 + get_max(get_height(root->left), get_height(root->right));
    
        // Check the balance of the tree
        int balance = get_balance(root);
    
        // If the tree is imbalanced, fixed them with one of the following cases
        // Left-Left-Case
        if (balance > 1 && (word_compare(word, root->left) < 0))
            return right_rotate(root);
    
        // Right-Right-Case
        if (balance < -1 && (word_compare(word, root->right) > 0))
            return left_rotate(root);
    
        // Left-Right-Case
        if (balance > 1 && (word_compare(word, root->left) > 0)) {
            root->left = left_rotate(root->left);
            return right_rotate(root);
        }
        // Right-Left-Case
        if (balance < -1 && (word_compare(word, root->right) < 0)) {
            root->right = right_rotate(root->right);
            return left_rotate(root);
        }
    
        return root;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-05
      • 2019-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      相关资源
      最近更新 更多