AVL树插入导致Seg故障

时间:2014-11-24 01:17:23

标签: c insert segmentation-fault avl-tree

我一直在尝试在我的AVL程序中实现旋转功能,并且在调用Right Rotate函数时我继续遇到seg错误。我进行了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;
}

所有插入的方式都是使用插入功能:

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帖子,以包含我的代码的行号。

1 个答案:

答案 0 :(得分:0)

这是因为每次插入函数时,都会执行以下代码:

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

&amp;(* tree) - &gt; left可以为NULL。 假设当前只有一个根在树中,它的左边和右边是NULL指针。 插入似乎将绕过if(!(* tree))。 但是它会调用insert(&amp;(* tree) - &gt; left,item)或insert(&amp;(* tree) - &gt; right,item),它们都有一个NULL指针作为它的第一个参数。 / p>