c - 创建AVL树时访问冲突异常

标签 c avl-tree tree-balancing

我正在尝试从元素 vector 创建一个 AVL 完美平衡树。 我已经开始使用少量元素(8),以便我可以检查算法的正确性。当打印树中的值时出现我的问题,我不断收到下一个异常

"Exception thrown: read access violation.
nod->stanga was 0x4."

当我到达 (root)->(right)->left-:value 时,即使我在打印任何内容之前检查指针是否为空。 结构为:

typedef struct node
{   int key;
    int size;
    node *stanga;
    node *dreapta;
}TreeNode;

数组: int vector [8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; 打印功能:

void printElements(TreeNode *nod)
{
    if (nod != NULL)
    {
        printf("Nodul  este : %d \n", nod->key);
        if (nod->dreapta != NULL && nod->stanga != NULL)
        {
            printf("Nodul  dreapta al  nodului  %d este  : %d \n", nod->key, nod->dreapta->key);
            printf("Nodul  stamga al  nodului  este %d : %d\n ", nod->key, nod->stanga->key);

        }
        if (nod->dreapta != NULL)
        {
            printf("ramura dreapta a nodului  %d  cu valoare dreapta este : %d\n", nod->key,nod->dreapta->key);
            printElements(nod->dreapta);
        }
        if (nod->stanga != NULL)
        {
            printf("ramura dreapta a nodului  %d cu valoare stanga este : %d \n ",nod->key, nod->stanga->key);
            printElements(nod->stanga);
        }
    }
    else
    {
        printf("the end of the tree");
    }
}

调用方式:

TreeNode  *nod = (TreeNode*)malloc(sizeof(TreeNode));
    nod=Build_tree(0, 7);
    printElements(nod);

构建树是我的构建函数:

TreeNode* Build_tree(int start, int end)
{

    if (start < end)
    {
        int medium = (start + end) / 2;
        TreeNode  *n1 = (TreeNode*)malloc(sizeof(TreeNode));
        n1->key = vector[medium];
        n1->size = 1;
        if (n1->stanga == NULL)
        {
            n1->size = n1->dreapta->size + 1;//alocam sizeul nodului din drepata
        }
        if (n1->dreapta == NULL)
        {
            n1->size = n1->stanga->size + 1;//altefl alocam sizeul nodului din stanga
        }

        n1->stanga = Build_tree(start, medium-1);
        n1->dreapta = Build_tree(medium+1,end);
        return n1;
    }

}

我对使用指针和平衡树有点生疏。有人可以帮我提供线索吗?

最佳答案

Build_tree 中的 if (start < end) 语句有一个 else 条件,在该条件下您会脱离函数,从而返回未定义的内容。我插入了一个 fprintf(stderr, “错误: 无法到达此处 (%d,%d)\n”, start,end),并得到以下输出:

Error: cannot get here (0, 0)
Error: cannot get here (2, 2)
Error: cannot get here (4, 4)
Error: cannot get here (6, 5)
Error: cannot get here (7, 7)
Nodul  este : 4 
Nodul  dreapta al  nodului  4 este  : 6 
Nodul  stamga al  nodului  este 4 : 2
 ramura dreapta a nodului  4  cu valoare dreapta este : 6
Nodul  este : 6 
ramura dreapta a nodului  6  cu valoare dreapta este : 7
Nodul  este : 7 
ramura dreapta a nodului  4 cu valoare stanga este : 2 
 Nodul  este : 2 

现在,另一边应该做什么?

关于c - 创建AVL树时访问冲突异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50010862/

相关文章:

c - 给定 .dll、.lib 和 .h,并希望使用 DLL 中的函数编写 C 程序

c++ - #error inside of#define - 可能在 C++ 中(如果未定义某些常量,则在调用宏 MyMacro 时生成错误)?

c++ - 错误 C2661 : 'node::node' : no overloaded function takes 3 arguments

C:创建AVL平衡树

algorithm - 使用 +、- 运算符平衡算术表达式树

c++ - 当被系统 DLL 调用时,Hooked VirtualAlloc 返回 nullptr

c - 嵌套数组的初始化?

java - Java 的 AVL 树实现

java - 计算平衡系数

C++ 递归函数中的平衡树/调用顺序