簡體   English   中英

實現 AVL 樹,但在插入大數據文件時出現段錯誤

[英]Implementing an AVL tree, but getting seg fault on large data file insertion

使用 avl_tree 創建字典比較程序,但在讀取 200,000 多個唯一單詞的大文件時出現段錯誤。 較小的文件似乎工作正常。

我的一些想法可能是我的 word_compre function 可能會干擾旋轉。 或者我可能錯誤地實施了輪換。

// 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);
    }
}

解決它。 沒有在插入 function 中更新當前節點/根高度。

// 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM