Balanced binary tree (AVL) of the basic operation (search, insert, contribution) - and comes complete code sample

Definition 1

  • Concept: On the basis of the BST (binary search tree), based on a height difference between any nodes left and right subtrees (equilibrium factor) of the absolute value is not more than 1.
  • AVL to create a name taken from the Soviet mathematician (GMAdelse-Velskil and EMLandis) before it two.

2 Basic Operation

2.1 storage structure

struct node{
    int data;//结点权值
    int height;//当前子树高度
    node *lchild, *rchild; 
};

New node 2.2

//生成一个结点,结点权值为v
node* newNode(int v){
    node* Node = new node;
    Node->data = v;
    Node->height = 1;
    Node->lchild = Node->rchild  = NULL;
    return Node;
}

2.3 to get to the root of the root of the current sub-tree height

//获得以root为根结点的子树的当前height
int getHeight(node* root){
    if(root == NULL) return 0;
    return root->height;
}

2.4 balance factor calculated root

//计算root的平衡因子
int getBalandeFactor(node* root){
    return getHeight(root->lchild) - getHeight(root->rchild); 
}

2.5 update root of height

//更新root的height
void updateHeight(node* root){
    root->height = max(getHeight(root->lchild),  getHeight(root->rchild)) + 1;
}

2.6 the insertion node

2.6.1 single left rotation

Here Insert Picture Description

//左单旋转(RR平衡旋转),结点A的右孩子的右子树上插入新结点
void L(node* &root){
    node* temp = root->rchild;
    root->rchild = temp->lchild;
    temp->lchild = root;
    updateHeight(root);
    updateHeight(temp);
    root = temp;
}

2.6.2 Right single rotation

Here Insert Picture Description

//右单旋转(LL平衡旋转),结点A的左孩子的左子树插入新结点
void R(node* &root){
    node* temp = root->lchild;
    root->lchild = temp->rchild;
    temp->rchild = root;
    updateHeight(root);
    updateHeight(temp);
    root = temp;
}

2.6.3 insertion node

/*
BF:平衡因子
树形                            判定条件                             操作        
LL(结点A的左孩子的左子树插入新结点) BF(root) = 2,BF(root->lchild = 1)   右旋       
LR(结点A的左孩子的右子树插入新结点)  BF(root) = 2,BF(root->lchild = -1)  先对root->lchild左旋,再对root右旋
RR(结点A的右孩子的右子树插入新结点) BF(root) = -2,BF(root->lchild = -1)   左旋
RL(结点A的右孩子的左子树插入新结点)  BF(root) = 2,BF(root->lchild = 1)  先对root->lchild右旋,再对root左旋
*/

void Insert(node* &root, int v){
    if(root == NULL){
        root = newNode(v);
        return;
    }

    if(v < root->data){//v比根结点的权值小
        Insert(root->lchild, v);//往左子树插
        updateHeight(root); //更新树高
        if(getBalandeFactor(root) == 2){
            if(getBalandeFactor(root->lchild) == 1){//LL型
                R(root);
            }else if(getBalandeFactor(root->lchild) == -1){//LR型
                L(root->lchild);
                R(root);
            }
        }
    }else{//v比根结点的权值大
        Insert(root->rchild, v);//往右子树插
        updateHeight(root);//更新树高
        if(getBalandeFactor(root) == -2){
            if(getBalandeFactor(root->rchild) == -1){//RR型
                L(root);
            }else if(getBalandeFactor(root->rchild) == 1){//RL型
                R(root->rchild);
                L(root);
            }
        }
    }

2.7 create AVL tree

//AVL树建立
node* Create(int data[], int n){
    node* root = NULL;//新建空根结点
    
    for (int i = 0; i < n; ++i)//将data[0]~data[n-1]插入AVL树中
    {
        Insert(root, data[i]);
    }

    return root;//返回根结点
}

2.8 Find node value

//查找值
void Search(node* root, int x){
    if(root == NULL){
        printf("Search fail!\n");
        return;
    }

    if(x == root->data){
        printf("%d\n", root->data);
    }else if(x < root->data){
        Search(root->lchild, x);
    }else{
        Search(root->rchild, x);
    }
}

3.0 The complete code

#include <cstdio>
#include <algorithm>

using std::max;

struct node{
    int data, height;
    node *lchild, *rchild;
};

//生成一个结点,结点权值为v
node* newNode(int v){
    node* Node = new node;
    Node->data = v;
    Node->height = 1;
    Node->lchild = Node->rchild  = NULL;
    return Node;
}

//获得以root为根结点的子树的当前height
int getHeight(node* root){
    if(root == NULL) return 0;
    return root->height;
}

//计算root的平衡因子
int getBalandeFactor(node* root){
    return getHeight(root->lchild) - getHeight(root->rchild);
}

//更新root的height
void updateHeight(node* root){
    root->height = max(getHeight(root->lchild),  getHeight(root->rchild)) + 1;
}

//查找值
void Search(node* root, int x){
    if(root == NULL){
        printf("Search fail!\n");
        return;
    }

    if(x == root->data){
        printf("%d\n", root->data);
    }else if(x < root->data){
        Search(root->lchild, x);
    }else{
        Search(root->rchild, x);
    }
}

//左单旋转(RR平衡旋转),结点A的右孩子的右子树上插入新结点
void L(node* &root){
    node* temp = root->rchild;
    root->rchild = temp->lchild;
    temp->lchild = root;
    updateHeight(root);
    updateHeight(temp);
    root = temp;
}

//右单旋转(LL平衡旋转),结点A的左孩子的左子树插入新结点
void R(node* &root){
    node* temp = root->lchild;
    root->lchild = temp->rchild;
    temp->rchild = root;
    updateHeight(root);
    updateHeight(temp);
    root = temp;
}


/*
BF:平衡因子
树形                            判定条件                             操作
LL(结点A的左孩子的左子树插入新结点) BF(root) = 2,BF(root->lchild = 1)   右旋
LR(结点A的左孩子的右子树插入新结点)  BF(root) = 2,BF(root->lchild = -1)  先对root->lchild左旋,再对root右旋
RR(结点A的右孩子的右子树插入新结点) BF(root) = -2,BF(root->lchild = -1)   左旋
RL(结点A的右孩子的左子树插入新结点)  BF(root) = 2,BF(root->lchild = 1)  先对root->lchild右旋,再对root左旋
*/

void Insert(node* &root, int v){
    if(root == NULL){
        root = newNode(v);
        return;
    }

    if(v < root->data){//v比根结点的权值小
        Insert(root->lchild, v);//往左子树插
        updateHeight(root); //更新树高
        if(getBalandeFactor(root) == 2){
            if(getBalandeFactor(root->lchild) == 1){//LL型
                R(root);
            }else if(getBalandeFactor(root->lchild) == -1){//LR型
                L(root->lchild);
                R(root);
            }
        }
    }else{//v比根结点的权值大
        Insert(root->rchild, v);//往右子树插
        updateHeight(root);//更新树高
        if(getBalandeFactor(root) == -2){
            if(getBalandeFactor(root->rchild) == -1){//RR型
                L(root);
            }else if(getBalandeFactor(root->rchild) == 1){//RL型
                R(root->rchild);
                L(root);
            }
        }
    }
}

//AVL树建立
node* Create(int data[], int n){
    node* root = NULL;//新建空根结点

    for (int i = 0; i < n; ++i)//将data[0]~data[n-1]插入AVL树中
    {
        Insert(root, data[i]);
    }

    return root;//返回根结点
}

int main(int argc, char const *argv[])
{
    int tree[7] = {100, 80, 60, 90, 120, 130, 110};
    node* root = Create(tree, 7);

    Search(root, 110);
    Search(root,1);
    return 0;
}
Published 321 original articles · won praise 51 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_33375598/article/details/104260424