构造AVL树代码模板(易于理解)

本文详细介绍了使用C++编写的AVL树模板类,包括节点结构、节点平衡度计算、旋转操作(左旋和右旋)、插入节点、删除节点以及维护平衡的方法。
摘要由CSDN通过智能技术生成

#include<iostream>

#include<string>

#include<cmath>

#include<stdlib.h>

#include <vector>

#include<queue>

#include<algorithm>

using namespace std;

template<class T>

class AVL_tree{

public:

     typedef struct node{

         T vlue;

      int  height_balance;//用来记录当前节点的平衡度

      struct node * left ,*right;

      node(T key){

        left = nullptr;

        right = nullptr;

        height_balance = 0;

        vlue = key;

      }

     } AVL_node;

     AVL_node * root = nullptr;

     int get_height (AVL_node * root){//用来获得当前这棵树的高度

        if(root ==nullptr){

            return 0;

        }

        else{

            return  max(get_height(root->left),get_height(root->right)) + 1;

        }

     }

     int get_balance(AVL_node * root){//获得当前节点的平衡度

        int lef = get_height(root->left);

        int rig = get_height(root->right);

        return lef - rig ;

     }

     AVL_node * Rotate_right(AVL_node * cur){//右旋操作

        AVL_node * cur_sun = cur->left;

        AVL_node * cur_re_sun = cur_sun->right;

        cur->left = cur_re_sun ;

        cur_sun->right = cur;

        cur_sun->height_balance = get_balance(cur_sun);

        cur->height_balance = get_balance( cur);

         return cur_sun;

     }

     AVL_node * Rotate_left(AVL_node* cur){//左旋操作

        AVL_node * cur_sun  = cur->right ;

        AVL_node * cur_le_sun= cur_sun->left;

        cur->right = cur_le_sun;

        cur_sun->left = cur;

        cur_sun->height_balance = get_balance(cur_sun);

        cur->height_balance = get_balance( cur);

        return cur_sun;

     }

     AVL_node * insert_node(T k ,AVL_node* root){

        //先插入节点

        if(root==nullptr){

            AVL_node * NEW_node = new AVL_node(k);

            return NEW_node;

        }

        if(k ==root->vlue ){

            return root;

        }

        else if(k < root->vlue){

          root->left = insert_node(k, root->left);//向左子树找插入位置

        }

        else if(k> root->vlue){

            root->right = insert_node(k,root->right);//向右子树找插入位置

        }

       root->height_balance  =get_balance(root);//

       if(root->height_balance > 1 && k<root->left->vlue){//LL 型

           return Rotate_right(root) ;

       }

       else if(root->height_balance <-1 && k>root->right->vlue) {//RR型

            return Rotate_left(root);

       }

       else if(root->height_balance > 1 && k>root->left->vlue){//LR型

                     root->left = Rotate_left(root->left);

                     return  Rotate_right(root) ;

       }

       else if(root->height_balance < -1 && k< root->right->vlue){//RL型

                    root->right = Rotate_right(root->right);

                    return Rotate_left(root);

       }

       return root;

     }

     void mid_dfs(AVL_node* root ){//打印节点

        if(root ==nullptr){

            return ;

        }

        mid_dfs(root->left);

        cout << root->height_balance<<':' << root->vlue<<endl;

        mid_dfs(root->right);

        return ;

     }

     void create_AVL_tree(T k){

        root = insert_node(k,root);

        return ;

     }

     void print_AVL(){

        mid_dfs(root );

     }

     void mid_way_print(){

        if(root==nullptr){

            cout<< "AVL is empty"<<endl;

        }

       queue<AVL_node *> q;

       q.push(root);

       while(!q.empty()){

        int n =q.size();

        for(int i =0 ; i < n ; i++){

            AVL_node * tem = q.front();

            q.pop();

            cout << tem->height_balance << ':' <<tem->vlue<<' ';

            if(tem->left!=nullptr){

                q.push(tem->left);

            }

            if(tem->right!=nullptr){

                q.push(tem->right);

            }

        }

        cout <<endl;

       }

     }

   int get_later_vlue(AVL_node * root){

           AVL_node *cur = root ;

           while(cur->right != nullptr){

            cur = cur ->right;

           }

           return cur->vlue;

   }

 AVL_node * delete_node(int k,AVL_node * root){//删除节点

    if(root == nullptr){

        return nullptr;

    }

    if( k < root->vlue ){

       root->left = delete_node(k,root->left);

    }

   else if( k > root->vlue){

        root->right = delete_node(k,root->right);

    }

   else if(k==root->vlue){

        if(root->left==nullptr && root->right==nullptr){

            return nullptr;

        }

        else if(root->left!=nullptr && root->right==nullptr){

            return root->left;

        }

        else  if(root->right!=nullptr && root->left==nullptr){

            return root->right;

        }

        else  if(root->right!=nullptr && root->left !=nullptr){

              int need_vlue = get_later_vlue(root->right);

              root->vlue = need_vlue;

              root->right = delete_node(need_vlue,root->right);

        }

        int root_balance_degree = get_balance( root);

        //删除的情况其实可以考虑插入的情景

        // LL 型

        if(root_balance_degree > 1 &&  get_balance(root->left) >=0  ){

            return Rotate_right(root);

        }

        //RR型

        else if(root_balance_degree < -1 && get_balance(root->right)<=0 ){

            return Rotate_left(root);

        }

        //LR型

        else if(root_balance_degree > 1 && get_balance(root->left ) <0){

            Rotate_left(root->left);

            return Rotate_right(root);

        }

        ///RL型

        else if(root_balance_degree < -1 && get_balance(root->right)>1){

            Rotate_right(root->right);

            return Rotate_left(root);

        }

        return root;

    }

 }

};

  • 25
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值