zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    [CareerCup] 4.4 Create List at Each Depth of Binary Tree 二叉树的各层创建链表
    [CareerCup] 4.3 Create Minimal Binary Search Tree 创建最小二叉搜索树
    [CareerCup] 4.2 Route between Two Nodes in Directed Graph 有向图中两点的路径
    [ZT] 医学图像分析相关的会议
    [CareerCup] 4.1 Balanced Binary Tree 平衡二叉树
    [CareerCup] 3.7 Adopt Animal 领养动物
    [LeetCode] 241. Different Ways to Add Parentheses 添加括号的不同方式
    OpenCV Save CvRect to File 保存CvRect变量到文件
    [CareerCup] 3.6 Sort Stack 栈排序
    [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列
  • 原文地址:https://www.cnblogs.com/foodie-nils/p/13854774.html
Copyright © 2011-2022 走看看