AVL树旋转子连接错误

时间:2015-03-10 13:32:18

标签: c++ tree binary-search-tree

我在理论上已经知道了AVL Tree,但我无法将其放入代码中。我已经能够左右左右旋转左右旋转。但我无法对左右和右右案件进行轮换。这是左旋转的那个。

void leftRotate(AVLNode **rootptr){

AVLNode *temp1, *temp2;

if(rootptr!=NULL && *rootptr!=NULL && (*rootptr)->right!=NULL){
    temp1 = *rootptr;
    if(temp1->parent->left == temp1 && temp1->right != NULL) {
        temp1->right->parent = temp1->parent;
        temp1->parent->left = temp1->right;

        temp1->parent = temp1->right;
        temp1->right->left = temp1;
        temp1->left = temp1->right = NULL;
        temp1 = temp1->parent;
    }else{
        temp2 = temp1->parent;
        if(temp1->parent->parent == NULL){
            temp1->parent->parent = temp1;
            temp1->parent->right = temp1->left;
            temp1->left = temp1->parent;
            temp1->parent = NULL;
        }

    }   
    updateHeight(temp1->left);
    updateHeight(temp1);
}
}

其他部分适用于右翼情况。这部分是继续给我一个状态访问冲突错误的行。它应该将pivot父级放入其左子级

temp1->left = temp1->parent;

我已经检查了它,temp1-> left是NULL,temp1-> parent存在但是我似乎无法将它放入树中。如果我删除那部分,它可以工作但当然我错过了temp1的左孩子。

0 个答案:

没有答案