【问题标题】:AVL rotations in CC中的AVL旋转
【发布时间】:2021-01-06 22:03:37
【问题描述】:

当旋转发生时,只有第一次旋转在我的代码中有效,我不明白为什么我认为旋转函数返回错误的节点?也许。下面,有我的节点和树结构

#define ll unsigned long
typedef struct NODE_s *NODE; //Node structure
typedef struct NODE_s{
    NODE right,left;
    ll key;
    int height;
    void *data;
} NODE_t[1];

typedef struct AVL_s *AVL; //Tree structure
typedef struct AVL_s{
    NODE root;
} AVL_t[1];

初始化函数

AVL AVL_init(){
    AVL t = (AVL)malloc(sizeof(AVL_t));
    t->root = NULL;
    return t;
}
NODE node_init(ll init_key){
    NODE node = (NODE)malloc(sizeof(NODE_t));
    node->left = NULL;
    node->right = NULL;
    node->key = init_key;
    node->height = 1;
    node->data = NULL;
    return node;
}

旋转函数

NODE rightRotate(NODE node){

    NODE child  = node->left;
    NODE childR = child->right;

    child->right = node;
    node->left = childR;


        node->height = max(height(node->left), height(node->right))+1;
        child->height = max(height(child->left), height(child->right))+1;

    return child;
}
NODE leftRotate(NODE node)
{
    NODE child  = node->right;
    NODE childL = child->left;

    child->left = node;
    node->right = childL;


        node->height = max(height(node->left), height(node->right))+1;
        child->height = max(height(child->left), height(child->right))+1;

        return child;
}

递归插入我认为必须使用两个函数从 main 然后 tree->root 在插入函数中发送树。

NODE insert_rec(NODE node,ll rec_key){
    if(rec_key < node->key){
        if(node->left == NULL)
            node->left = node_init(rec_key);
        else
            insert_rec(node->left, rec_key);
    }else if(rec_key > node->key){
        if(node->right == NULL)
            node->right = node_init(rec_key);
        else
            insert_rec(node->right, rec_key);
    }else
        printf("Duplicate %lu Data\n",rec_key);

    //If I delete from here to end of functions, it is clearly recursive BST insert and works successfully

    node->height = 1 + max(height(node->left),height(node->right));
    int balanceFactor = balance(node);

    if (balanceFactor > 1 && rec_key < node->left->key) //leftleft
       node = rightRotate(node);   //return rightRotate(node) also working

    if (balanceFactor < -1 && rec_key > node->right->key) //rightright
        node = leftRotate(node);   //return leftRotate(node) also working

    if (balanceFactor > 1 && rec_key > node->left->key) //leftright
    {
        node->left =  leftRotate(node->left);
        node = rightRotate(node); 
    }
    if (balanceFactor < -1 && rec_key < node->right->key) //rightright
    {
        node->right = rightRotate(node->right);
        node = leftRotate(node);
    }
   return node;
}
void insert(AVL tree,ll key){
    if(key == -1){
        printf("Invalid data %lu\n",key);
    }
    else if(tree->root == NULL){
        tree->root = node_init(key);
    }
    else{
       tree->root = insert_rec(tree->root,key);
    }
}
int main() {
    AVL tree = AVL_init();
    insert(tree,111);
    }

这是我的源代码,我无法理解我没有看到的内容。如果我将其替换为正常的 BST,它正在工作,但是当发生旋转时,节点会出现在某处

【问题讨论】:

  • 在 foreverlyconfuzzled.com 上有 [] 一堆非常好的教程(带有源代码),用于各种事物(包括 AVL 树)但是,它不再可用.但是,它被存档在这里:web.archive.org/web/20180225130248/http://… 点击“图书馆”链接

标签: c avl-tree recursive-datastructures


【解决方案1】:

我解决了。函数 insert() 被丢弃并放入 insert_rec()。如果 root = null 条件,这只是根检查。

然后主函数就这样替换了,

int main() {
    AVL tree = AVL_init();
    NODE node = tree->root;
    insert_rec(node,111);
}

最后,在平衡因素的情况下,我只需要返回函数

return leftRotate(node); //instead of node = leftRotate(node); 
return rightRotate(node); //instead of node = rightRotate(node);

这种视角使得处理函数返回值变得容易

【讨论】:

  • 很好地回答了您自己的问题。我建议您接受自己的答案(单击答案左侧的赞成/反对箭头下方的复选标记),以便获得它的声誉积分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多