c++ - 更新 AVL 树代码以执行查找或插入

标签 c++ c r segmentation-fault avl-tree

我想在特定算法中使用 AVL 树。我正在制作一个 R 包,所以我想坚持使用 C 或 C++ 实现(目前使用 C 实现)。

我从以下位置获得了 AVL 树实现的基本代码:http://www.geeksforgeeks.org/avl-tree-set-2-deletion/

我正在尝试创建一个新函数,如果键不在树中,它将插入一个新节点,否则如果它在树中,请给我访问该节点的权限,以便我查看它。不过,我是一名菜鸟 C 程序员,所以我遇到了一些问题。

这是我当前的实现。在主要功能中,我插入了几个键并打印了一个预购订单以检查插入是否正常工作(他们确实如此)。然后我尝试插入一个已经在树中的键。这会导致段错误 :( 任何人都可以帮助我吗?这可能只是我的 C 的一个简单问题:

struct node* specialInsert(struct node* root, struct node* result, int *numBefore, int key, bool *flag){
    //We are inserting a new (non-root) node!
    if (root == NULL){
        struct node* res = newNode(key);
        res->equalCount = key;
        *numBefore = 0;
        *flag = true;
        return(res);
    }else if( key == root->key){
        *flag = false;
        *numBefore = root->leftCount;
        root->equalCount = root->equalCount + key;

        if(result == NULL){
            struct node* result = newNode(root->key);
        }

        //printf("result key is: %d\n", result->key);

        return root;
    }else if( key < root->key){
        root->left = specialInsert(root->left, result, numBefore, key, flag);
        root->leftCount = root->leftCount + key;
        //if(*flag) update(root, key);
        //else return(root->left);
        update(root,key);
    } else if( key > root->key){
        root->right = specialInsert(root->right, result, numBefore, key, flag);
        *numBefore = *numBefore + root->leftCount + root->equalCount;
        root->rightCount = root->rightCount + key;
        //if(*flag) update(root, key);
        //else return(root->right);
        update(root,key);
    }
}
struct node* findOrInsert(struct node* root, struct node* result, int *numBefore, int key){
    bool *flag;
    bool val = false;
    flag = &val;

    /* 1.  Perform the normal BST rotation */
    if (root == NULL){
        struct node* res = newNode(key);
        *numBefore = 0;
        res->equalCount = key;
        return(res);
    }
    else return( specialInsert(root, result, numBefore, key, flag) );


}

在一个主要功能中我有:

struct node *root2 = NULL;
struct node *result = NULL;

root2 = findOrInsert(root2, result, x, 9);
root2 = findOrInsert(root2, result, x, 5);
root2 = findOrInsert(root2, result, x, 10);
root2 = findOrInsert(root2, result, x, 0);
root2 = findOrInsert(root2, result, x, 6);
root2 = findOrInsert(root2, result, x, 11);
root2 = findOrInsert(root2, result, x, -1);
root2 = findOrInsert(root2, result, x, 1);
root2 = findOrInsert(root2, result, x, 2);


preOrder(root2);
printf("\n");

root2 = findOrInsert(root2, result, x, 9);
printf("Number of elements less than %d: %d\n", result->key,result->leftCount);

最佳答案

您可以简单地依赖其他人编写的代码。

一个这样的实现是 Boost implementation of AVL Trees您可以通过 CRAN package BH 轻松访问它这为您提供了在 R(和 C++)中使用的 Boost Headers。

当然,你也可能喜欢调试自己的数据结构,这也是有好处的。在这种情况下,gdb 可能会有所帮助...

关于c++ - 更新 AVL 树代码以执行查找或插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22967812/

相关文章:

c++ - 将解密后的数据暂存在内存中

c++ - 从 Gtkmm::Table 中删除一行

c - 需要了解 printf 的行为

c++ - 为什么会存在 M_PI_2、M_PI_4、M_1_PI 和 M_2_PI?

c - 如何声明未定义或没有初始大小的数组?

r - ggplot2 带有空格和 tidyeval 语法的变量名

r - 如何使用 R 找到具有不同土地利用分类的栅格的平均坡度?

c++ - TForm 隐藏在主窗口后面

c++ - 如何处理使用 dlopen() 打开的已更改库

r - 当数据在列表中时,我可以向量化代码吗?