当前位置:网站首页>Balanced binary tree AVL

Balanced binary tree AVL

2022-06-26 00:45:00 Be nice 123

Right hand operation

// To p Do right-handed processing for binary sort tree of root 
// After processing p Point to the new tree root , That is, the root node of the left subtree before rotation processing 
void R_Rotate(BiTree &p){
    
    BiTree L;
    L=p->lchild;//L Point to p The root node of the left subtree of 
    p->lchild=L->rchild;//L The right branch of the tree is linked to p The left subtree 
    L->rchild=p;
    p=L;//p Point to the new root node 
}

Left hand operation

/* To p Make a left-handed treatment for the binary sorting tree of the root   After processing p Point to the new tree root , That is, the root node of the right subtree before rotation processing  */
void L_Rotate(BiTree &p){
    
    BiTree R;
    R=p->rchild;//R Point to P Root node of right subtree of 
    p->rchild=R->lchild;//R The left subtree of is connected to p The right subtree 
    R->lchild=p;
    p=R;//p Point to the new root node 
}

Left balance rotation processing

/* Pointer to T The binary tree with the node as the root should be left balanced and rotated   At the end of this algorithm , The pointer T Point to the new root node */
void LeftBalance(BiTree &T){
    
    BiTree L,Lr;
    L=T->lchild;//L Point to T The root node of the left subtree of 
    switch(L->bf){
    
        // Check T The balance degree of the left subtree of , And make corresponding balance treatment 
        case LH:// The new node is inserted in T On the left child's left tree , We need to do a single right turn 
            T->bf=L->bf=EH;
            R_Rotate(T);
            break;
        case RH:// The new node is inserted in T On the right subtree of the left child , Double spin 
            Lr=L->rchild;//Lr Point to T The left child's right tree root 
            switch(Lr->bf)// modify T And the left child's balance factor 
            {
    
                case LH:
                    T->bf=RH;
                    L->bf=EH;
                    break;
                case EH:
                    T->bf=L->bf=EH;
                    break;
                case RH:
                    T->bf=EH;
                    L->bf=LH;
                    break;
            }
            Lr->bf=EH;
            L_Rotate(T->lchild);// Yes T The left subtree of is left-handed balanced 
            R_Rotate(T);// Yes T Do a right-handed balance 

    }
}

Right balance rotation processing

void RightBalance(BiTree &T){
    
    BiTree R,Rl;
    R=T->rchild;
    switch(R->bf){
    
        case RH:// Corresponding to RR
            T->bf=R->bf=EH;
            L_Rotate(T);
            break;
        case LH:// Corresponding to RL
            Rl=R->lchild;
            switch(Rl->bf){
    
                case LH:
                    T->bf=EH;
                    R->bf=RH;
                    break;
                case EH:
                    T->bf=R->bf=EH;
                    break;
                case RH:
                    T->bf=LH;
                    R->bf=EH;
                    break;
            }
            Rl->bf=EH;
            R_Rotate(T->rchild);
            L_Rotate(T);
    }
}

Balanced binary tree insertion

/*  If in balanced binary sort tree T There is no and e Nodes with the same keyword , Insert a   The data element is e New node of , And back to true; Otherwise return to false; If the two forks are caused by insertion   The sort tree is out of balance , Balance rotation is required , Boolean variables taller reflect T Is it tall or not  */
bool InsertAVL(BiTree &T, int e,bool &taller){
    
    if(!T){
    
        // Insert new node , The trees grow tall , Set up taller=true;
        T=(BiTree)malloc(sizeof(BiTNode));
        T->data=e;
        T->lchild=T->rchild=NULL;
        T->bf=EH;
        taller=true;
    }
    else{
    
        if(e == T->data){
    
            // Trees already exist and e Nodes with the same keyword are not inserted 
            taller=false;
            return false;
        }
        if(e < T->data){
    
            // Should continue in T Search in the left subtree of 
            if(!InsertAVL(T->lchild, e, taller))// Not inserted 
                return false;
            if(taller){
    
                // Inserted into T And the left subtree is tall 
                switch(T->bf){
    
                    // Check T The balance of 
                    case LH:// Originally, the left subtree was higher than the right subtree , It needs to be left balanced 
                        LeftBalance(T);
                        taller=false;
                        break;
                    case EH:// Originally left and right subtrees were equal in height , Now because the left sub tree is higher and the tree is higher 
                        T->bf=LH;
                        taller=true;
                        break;
                    case RH:// Originally, the right subtree was higher than the left subtree , Now left and right subtrees are the same height 
                        T->bf=EH;
                        taller=false;
                        break;
                }
            }
        }
        else{
    
            // Should continue in T Search in the right subtree of 
            if(!InsertAVL(T->rchild, e, taller))// Not inserted 
                return false;
            if(taller){
    
                // Check T The balance of 
                switch(T->bf){
    
                case LH:// Originally, the left subtree was higher than the right subtree , Now wait for height 
                    T->bf=EH;
                    taller=false;
                    break;
                case EH:// Originally left and right subtrees were equal in height , Now because the right subtree is higher and the tree is higher 
                    T->bf=RH;
                    taller=true;
                    break;
                case RH:// Originally, the right subtree was higher than the left subtree , Right balance is required 
                    RightBalance(T);
                    taller = false;
                    break;

                }
            }

        }

    }
    return true;
}

Complete test code

// Balanced binary trees , Balanced tree AVL Trees ,G.M.Adelson-Velsky and E.M.Landis
#include<stdio.h>
#include<stdlib.h>
#define ElemType int

#define LH 1// Left high 
#define EH 0// Equal height 
#define RH -1// Right high 

// Balanced binary tree node 
typedef struct BiTNode{
    // Node structure 
    ElemType data;// Node data 
    int bf;// The equilibrium factor of the node 
    struct BiTNode *lchild,*rchild;// Left and right child pointer 
}BiTNode,*BiTree;

// To p Do right-handed processing for binary sort tree of root 
// After processing p Point to the new tree root , That is, the root node of the left subtree before rotation processing 
void R_Rotate(BiTree &p){
    
    BiTree L;
    L=p->lchild;//L Point to p The root node of the left subtree of 
    p->lchild=L->rchild;//L The right branch of the tree is linked to p The left subtree 
    L->rchild=p;
    p=L;//p Point to the new root node 
}

/* To p Make a left-handed treatment for the binary sorting tree of the root   After processing p Point to the new tree root , That is, the root node of the right subtree before rotation processing  */
void L_Rotate(BiTree &p){
    
    BiTree R;
    R=p->rchild;//R Point to P Root node of right subtree of 
    p->rchild=R->lchild;//R The left subtree of is connected to p The right subtree 
    R->lchild=p;
    p=R;//p Point to the new root node 
}

/* Pointer to T The binary tree with the node as the root should be left balanced and rotated   At the end of this algorithm , The pointer T Point to the new root node */
void LeftBalance(BiTree &T){
    
    BiTree L,Lr;
    L=T->lchild;//L Point to T The root node of the left subtree of 
    switch(L->bf){
    
        // Check T The balance degree of the left subtree of , And make corresponding balance treatment 
        case LH:// The new node is inserted in T On the left child's left tree , We need to do a single right turn 
            T->bf=L->bf=EH;
            R_Rotate(T);
            break;
        case RH:// The new node is inserted in T On the right subtree of the left child , Double spin 
            Lr=L->rchild;//Lr Point to T The left child's right tree root 
            switch(Lr->bf)// modify T And the left child's balance factor 
            {
    
                case LH:
                    T->bf=RH;
                    L->bf=EH;
                    break;
                case EH:
                    T->bf=L->bf=EH;
                    break;
                case RH:
                    T->bf=EH;
                    L->bf=LH;
                    break;
            }
            Lr->bf=EH;
            L_Rotate(T->lchild);// Yes T The left subtree of is left-handed balanced 
            R_Rotate(T);// Yes T Do a right-handed balance 

    }
}

void RightBalance(BiTree &T){
    
    BiTree R,Rl;
    R=T->rchild;
    switch(R->bf){
    
        case RH:// Corresponding to RR
            T->bf=R->bf=EH;
            L_Rotate(T);
            break;
        case LH:// Corresponding to RL
            Rl=R->lchild;
            switch(Rl->bf){
    
                case LH:
                    T->bf=EH;
                    R->bf=RH;
                    break;
                case EH:
                    T->bf=R->bf=EH;
                    break;
                case RH:
                    T->bf=LH;
                    R->bf=EH;
                    break;
            }
            Rl->bf=EH;
            R_Rotate(T->rchild);
            L_Rotate(T);
    }
}




/*  If in balanced binary sort tree T There is no and e Nodes with the same keyword , Insert a   The data element is e New node of , And back to true; Otherwise return to false; If the two forks are caused by insertion   The sort tree is out of balance , Balance rotation is required , Boolean variables taller reflect T Is it tall or not  */
bool InsertAVL(BiTree &T, int e,bool &taller){
    
    if(!T){
    
        // Insert new node , The trees grow tall , Set up taller=true;
        T=(BiTree)malloc(sizeof(BiTNode));
        T->data=e;
        T->lchild=T->rchild=NULL;
        T->bf=EH;
        taller=true;
    }
    else{
    
        if(e == T->data){
    
            // Trees already exist and e Nodes with the same keyword are not inserted 
            taller=false;
            return false;
        }
        if(e < T->data){
    
            // Should continue in T Search in the left subtree of 
            if(!InsertAVL(T->lchild, e, taller))// Not inserted 
                return false;
            if(taller){
    
                // Inserted into T And the left subtree is tall 
                switch(T->bf){
    
                    // Check T The balance of 
                    case LH:// Originally, the left subtree was higher than the right subtree , It needs to be left balanced 
                        LeftBalance(T);
                        taller=false;
                        break;
                    case EH:// Originally left and right subtrees were equal in height , Now because the left sub tree is higher and the tree is higher 
                        T->bf=LH;
                        taller=true;
                        break;
                    case RH:// Originally, the right subtree was higher than the left subtree , Now left and right subtrees are the same height 
                        T->bf=EH;
                        taller=false;
                        break;
                }
            }
        }
        else{
    
            // Should continue in T Search in the right subtree of 
            if(!InsertAVL(T->rchild, e, taller))// Not inserted 
                return false;
            if(taller){
    
                // Check T The balance of 
                switch(T->bf){
    
                case LH:// Originally, the left subtree was higher than the right subtree , Now wait for height 
                    T->bf=EH;
                    taller=false;
                    break;
                case EH:// Originally left and right subtrees were equal in height , Now because the right subtree is higher and the tree is higher 
                    T->bf=RH;
                    taller=true;
                    break;
                case RH:// Originally, the right subtree was higher than the left subtree , Right balance is required 
                    RightBalance(T);
                    taller = false;
                    break;

                }
            }

        }

    }
    return true;
}

void visit(BiTree T){
    
    printf("%d ",T->data);
}
// Middle order traversal of binary trees 
void InOrder(BiTree T){
    
    if(T){
    
        InOrder(T->lchild);// Recursively traverses the left subtree 
        visit(T);
        InOrder(T->rchild);
    }
}

void PreOrder(BiTree T){
    
    if(T){
    
        visit(T);
        PreOrder(T->lchild);// Recursively traverses the left subtree 
        PreOrder(T->rchild);
    }
}


// After the sequence traversal , Destroy the binary sort tree 
bool DestroyBiTree(BiTree T){
    // Delete without &
    if(T==NULL){
    
        printf(" Blank nodes #\n");
        return false;
    }
    DestroyBiTree(T->lchild);
    DestroyBiTree(T->rchild);
    printf(" The destruction %d\n",T->data);
    free(T);
    T=NULL;// To prevent the generation of wild pointers 
    return true;

}



int main(){
    
    int i;
    int a[10]={
    3,2,1,4,5,6,7,10,9,8};
    BiTree T=NULL;
    bool taller;
    for(i=0;i<10;i++){
    
        InsertAVL(T,a[i],taller);
    }

    printf(" In the sequence traversal :\n");
    InOrder(T);

    printf("\n\n The balanced binary tree can be drawn according to the output of preorder and inorder traversal \n And then verify the correctness of the results \n");
    printf("\n\n The former sequence traversal :\n");
    PreOrder(T);

    printf("\n\n Remember to destroy it after use ( Traverse and destroy in sequence )!\n");
    DestroyBiTree(T);
    return 0;
}

test result

 Insert picture description here

原网站

版权声明
本文为[Be nice 123]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206252049049194.html