簡體   English   中英

AVL樹插入導致段故障

[英]AVL Tree Insertion Causes Seg Fault

我一直在嘗試在AVL程序中實現旋轉功能,並且在調用Right Rotate函數時繼續出現段錯誤。 我執行了Valgrind測試,發現3個錯誤:

==23399== 1 errors in context 1 of 3:
==23399== Invalid read of size 4
==23399==    at 0x8048C3A: insert (avltree.c:190)
==23399==    by 0x80488B6: main (avltree.c:85)
==23399==  Address 0x3 is not stack'd, malloc'd or (recently) free'd
==23399== 
==23399== 
==23399== 1 errors in context 2 of 3:
==23399== Use of uninitialised value of size 4
==23399==    at 0x8048C3A: insert (avltree.c:190)
==23399==    by 0x80488B6: main (avltree.c:85)
==23399==  Uninitialised value was created by a stack allocation
==23399==    at 0x8048723: main (avltree.c:42)
==23399== 
==23399== 
==23399== 1 errors in context 3 of 3:
==23399== Conditional jump or move depends on uninitialised value(s)
==23399==    at 0x8048C03: insert (avltree.c:182)
==23399==    by 0x80488B6: main (avltree.c:85)
==23399==  Uninitialised value was created by a stack allocation
==23399==    at 0x8048723: main (avltree.c:42)

它在兩個錯誤中提到存在一個未初始化的值。 我認為這與我交換左右子樹的值有關,但是我似乎無法確切指出出問題的地方。

這是我的“ 右旋轉”功能:

void rightRotate(node * y)
{
    /* Assign values */
    node *x = y->left;
    node *subTree = x->right;

    /* Perform rotation */
    x->right = y; /* x is now root */
    y->left = subTree;

    /* Update heights */
    y->height = max(height(y->left), height(y->right))+1;
    x->height = max(height(x->left), height(x->right))+1;
}

所有插入的方式都是使用Insertion函數:

void insert(node ** tree, node * item)
{
    int balanceNum;

    /* If no root, item is root */
    if(!(*tree)) {
        *tree = item;
        printf("Root: \n"); /*Every node seems to get printed here */
        (*tree)->height = 0;
        return;
    }

    if(strcmp(item->key,(*tree)->key) < 0) {
        insert(&(*tree)->left, item);
    }
    else if(strcmp(item->key,(*tree)->key) > 0) {
        insert(&(*tree)->right, item);
    }
    else if(strcmp(item->key,(*tree)->key) == 0) {
        (*tree)->frequency++;
    }

    /* Update height of ancestor node */
    (*tree)->height = max(height((*tree)->left), height((*tree)->right)) + 1;
    printf("%s Height: %d\n", (*tree)->key, (*tree)->height);

    balanceNum = balance(*tree);

    if(balanceNum > 1 && strcmp(item->key,(*tree)->left->key) < 0) {
        printf("Right Rotate! Balance: %d with %s\n", balanceNum, item->key);
        rightRotate(*tree);
    }
}

為了這種特殊情況,我刪除了其余的旋轉功能。 有人可以指出我的旋轉功能可能出問題了嗎?

非常感謝您的任何建議或提示。

編輯:我更新了valgrind發布以包括我的代碼的行號。

這是因為每次插入函數時,都會執行以下代碼:

   if(!(*tree)) {
        *tree = item;
        printf("Root: \n"); /*Every node seems to get printed here */
        (*tree)->height = 0;
        return;
    }

原因是insert()被遞歸調用。

if(strcmp(item->key,(*tree)->key) < 0) {
    insert(&(*tree)->left, item);
}
else if(strcmp(item->key,(*tree)->key) > 0) {
    insert(&(*tree)->right, item);
}
else if(strcmp(item->key,(*tree)->key) == 0) {
    (*tree)->frequency++;
}

&(* tree)-> left可以為NULL。 假設當前樹中只有一個根,則其左和右為NULL指針。 插入似乎將繞過if(!(* tree))。 但是,它將調用insert(&(* tree)-> left,item)或insert(&(* tree)-> right,item),它們兩個都將NULL指針作為其第一個參數。

暫無
暫無

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

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