AVL树 实现代码

自己写的一个AVL树模板(未加data,只有key),代码比较短小紧凑。

ps:算起来class的内容也不过50来行,我到觉得avl树比sbt实现简单多了……仅稍复杂于treap

#include <iostream>
using namespace std;

#define zero(arr) (memset(arr, 0, sizeof(arr)))
const int MAXN = 2000000;

class AVLTree
{
public:
    AVLTree(){root=top=0; H[0]=0; zero(L); zero(R);}
    
    void insert(int key){ins(root, key);}
    void remove(int key){del(root, key);}
    
    int height(){return H[root];}
    void output(){out(root);}
private:
    void out(int rt){if(!rt)return; out(L[rt]); cout<<K[rt]<<endl; out(R[rt]);}
    
    void ins(int& rt, int key){
        if (rt == 0) {rt=new_node(key); return ;}
        if (key < K[rt])
            ins(L[rt], key), balance(rt);
        else
            ins(R[rt], key), balance(rt);
        CH(rt); // don't lose it!
    }
    
    void del(int& rt, int key){
        if (rt == 0) return ;
        if (key == K[rt]){
            if (!L[rt] && !R[rt]) rt=0;
            else if (L[rt]){
                for (t=L[rt]; R[t]; t=R[t]) ;
                K[rt]=K[t], del(L[rt],K[rt]);
            }else
                K[rt]=K[R[rt]], del(R[rt],K[rt]);
        }
        if (key < K[rt])
            del(L[rt], key), balance(rt);
        if (key > K[rt])
            del(R[rt], key), balance(rt);
        CH(rt); // don't lose it!
    }
    
    void balance(int &rt){
        if (H[L[rt]]-H[R[rt]] > 1)
            H[L[L[rt]]] < H[R[L[rt]]] ? zag(L[rt]),zig(rt) : zig(rt);
        if (H[R[rt]]-H[L[rt]] > 1)
            H[R[R[rt]]] < H[L[R[rt]]] ? zig(R[rt]),zag(rt) : zag(rt);
    }
    void CH(int t){H[t] = max(H[R[t]], H[L[t]]) + (t>0);}// correct height
    void zig(int &y){int x=L[y]; L[y]=R[x]; R[x]=y; CH(y); CH(x); y=x;}//右旋 
    void zag(int &x){int y=R[x]; R[x]=L[y]; L[y]=x; CH(x); CH(y); x=y;}//左旋 
    int new_node(int key){++top; K[top]=key; H[top]=1; return top;}
    
    int L[MAXN], R[MAXN], K[MAXN], H[MAXN];
    int root, top, t;
};

AVLTree avl;


int main()
{
    for (int i=0; i<100; i++)
        avl.insert(i);
        
    for (int i=0; i<100; i+=2)
        avl.remove(i);
        
    avl.output();
    cout<<"height: "<<avl.height()<<endl;
    
    system("pause");
    return 0;
}

加模板和内存管理版本

#include <iostream>
#include <cstdlib>
using namespace std;

template <class T, int MAXN>
class AVLTree
{
public:
        AVLTree(){H[0]=R[0]=L[0]=0; top=1; root=0; vn=0;}

        void insert(const T &x){ins(root, x);}
        void remove(const T &x){del(root, x);}

        void output(){out(root); cout<<endl<<"height :"<<H[root]<<endl;}

private:
        void out(int &rt){if(rt){out(L[rt]); cout<<K[rt]<<" "; out(R[rt]);}}

        void ins(int &rt, const T &x){
                if (rt == 0){rt = new_node(x); return;}
                if (x < K[rt])
                        ins(L[rt], x), balance(rt);
                else
                        ins(R[rt], x), balance(rt);
        }

        void del(int &rt, const T &x){
                if (!rt) return ;
                if (K[rt] == x){
                        if (!L[rt] && !R[rt]) v[vn++]=rt, rt=0;
                        else {
                                if (!L[rt]) i = R[rt];
                                else for (i=L[rt]; R[i]; i=R[i]) ;
                                K[rt] = K[i];
                                del(L[rt] ? L[rt]:R[rt], K[rt]);
                        }
                }
                if (x < K[rt])
                        del(L[rt], x), balance(rt);
                if (x > K[rt])
                        del(R[rt], x), balance(rt);
        }

private://平衡
        void balance(int &rt){
                if (H[L[rt]] - H[R[rt]] > 1)
                        H[L[L[rt]]] < H[L[R[rt]]] ? zag(rt),zig(rt) : zig(rt);
                if (H[R[rt]] - H[L[rt]] > 1)
                        H[R[R[rt]]] < H[R[L[rt]]] ? zig(rt),zag(rt) : zag(rt);
                hh(rt);
        }
        void zig(int &y){int x=L[y]; L[y]=R[x]; R[x]=y; hh(y); hh(x); y=x;}//right rotate
        void zag(int &x){int y=R[x]; R[x]=L[y]; L[y]=x; hh(x); hh(y); x=y;}//left rotate
        void hh(int x){H[x] = max(H[L[x]], H[R[x]]) + (x>0);}
        
private://内存管理
        int new_node(const T &x){int p=np(); K[p]=x; H[p]=1; L[p]=R[p]=0; return p;}
        int np(){return vn>0 ? v[--vn] : top++;}
        int v[MAXN], vn, top;

private://储存数据
        int root, i;
        int L[MAXN], R[MAXN], H[MAXN];
        T K[MAXN];
};

    原文作者:AVL树
    原文地址: https://blog.csdn.net/wxy__/article/details/7275082
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞