• AVL树


    1.AVL树定义

      AVL 树是一种平衡二叉树,得名于其发明者的名字( Adelson-Velskii 以及 Landis)。(可见名字长的好处,命名都能多占一个字母出来)。平衡二叉树递归定义如下:

        a.左右子树的高度差小于等于 1。

        b.其每一个子树均为平衡二叉树。

    /*
            1.查找
            2.插入
            3.创建 
    */
    #include<cstdio>
    #include<stdlib.h>
    typedef int ElementType;
    typedef struct node{
        int height;
        ElementType data;
        node* lchild;
        node* rchild;
    }node,AVLTree;
    //生成一个新的结点 
    node* newNode(ElementType x){
        node* p=(node*)malloc(sizeof(node));
        if(p==NULL)
            return NULL;
        p->data=x;
        p->height=1;
        p->lchild=NULL;
        p->rchild=NULL;
        return p;
    }
    //返回以root为根节点的子树的高度 
    int getHeight(node* root){
        if(root==NULL)
            return 0;
        return root->height;
    } 
    //计算root的平衡因子
    int getBalanceFactor(node* root){
        return getHeight(root->lchild)-getHeight(root->rchild);
    } 
    int max(int a,int b){
        if(a>b)
            return a;
        else
            return b;
    }
    //跟新以root为根节点的子树的高度
    void updateHeight(node* root){
        root->height=max(getHeight(root->lchild),getHeight(root->rchild))+1;
    }
    //查找AVL树中数据域为x的结点 
    node* search(node* root,ElementType x){
        if(root==NULL)
            return NULL;
        if(root->data==x)
            return root;
        if(x>root->data) 
            return search(root->rchild,x);
        else
            return search(root->lchild,x);
    }
    //左旋操作
    void LeftRotation(node* &root){
        node* temp=root->rchild;
        root->rchild=temp->lchild;
        temp->lchild=root;
        updateHeight(root);
        updateHeight(temp);
        root=temp;
    } 
    //右旋操作
    void RightRotation(node* &root){
        node* temp=root->lchild;
        root->lchild=temp->rchild;
        temp->rchild=root;
        updateHeight(root);
        updateHeight(temp);
        root=temp; 
    } 
    //往AVL树中插入一个数据域为x的新结点,并保持AVL树特性
    bool insert(node* &root,ElementType x){
        node* p=newNode(x);
        if(p==NULL)
            return false;
        if(root==NULL){
            root=p;
            return true;
        }
        if(x>root->data){
            insert(root->rchild,x);
            updateHeight(root);
            if(getBalanceFactor(root)==-2){
                if(getBalanceFactor(root->rchild)==-1){
                    LeftRotation(root);
                }
                else if(getBalanceFactor(root->rchild)==1){
                    RightRotation(root->rchild);
                    LeftRotation(root);
                }
            }
        }
        else{
            insert(root->lchild,x);
            updateHeight(root);
            if(getBalanceFactor(root)==2){
                if(getBalanceFactor(root->lchild)==1){
                    RightRotation(root);
                }
                else if(getBalanceFactor(root->lchild)==-1){
                    LeftRotation(root->lchild);
                    RightRotation(root);
                }
            }
        }
    }
    //创建AVL树
    node* Create(ElementType data[],int n){
        node* root=NULL;
        for(int i=0;i<n;i++){
            insert(root,data[i]);
        }
        return root;
    } 
    int main(){
        return 0;
    }
  • 相关阅读:
    tensorflow几个常见错误
    15、TDM模型
    算法97:成绩排名---动态规划
    推荐阅读论文
    Reservoir Sampling 蓄水池采样算法
    14、RALM: 实时 look-alike 算法在推荐系统中的应用
    NLP传统基础(3)---潜在语义分析LSA主题模型---SVD得到降维矩阵
    【SqlServer】SqlServer中的计算列
    【SQL】数据库中的五种约束
    【.NET】SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM之间
  • 原文地址:https://www.cnblogs.com/foodie-nils/p/13854774.html
Copyright © 2020-2023  润新知