AVL树的实现-c++

这里是AVL树的一些原理以及介绍

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<algorithm>


using namespace std;

#define INF 0x3f3f3f3f

typedef struct Node{
    int val;
    int height;
    Node *lc;
    Node *rc;
    Node(int v=INF, int h=1){
        val=v;
        height=h;
        lc=NULL;
        rc=NULL;
    }
}Node;

int getHeight(Node *root){
    return root==NULL?0:(*root).height;
}

void avlShow(Node *root){
    if(root==NULL)
        return;
    queue<Node> que;
    while(!que.empty())
        que.pop();
    que.push(*root);
    int cnt = getHeight(root);
    while(!que.empty()){
        Node t = que.front();
        if(cnt==0)
            break;
        printf("%5d:\t", cnt);
        queue<Node> next;
        while(!next.empty())
            next.pop();
        while(!que.empty()){
            if(t.val!=INF)
                printf(" %d", t.val);
            else
                printf(" -1");

            if(t.lc!=NULL)
                next.push(*(t.lc));
            else
                next.push(Node());

            if(t.rc!=NULL)
                next.push(*(t.rc));
            else
                next.push(Node());

            que.pop();
            if(!que.empty())
                t = que.front();
        }
        printf("\n");
        cnt--;
        while(!next.empty()){
            que.push(next.front());
            next.pop();
        }
    }
}

Node* leftLeftRotation(Node *root){
    Node *lc = (*root).lc;
    (*root).lc = (*lc).rc;
    (*lc).rc = root;

    (*root).height = max(getHeight((*root).lc), getHeight((*root).rc))+1;
    (*lc).height = max(getHeight((*lc).lc), getHeight((*lc).rc));

    return lc;
}

Node* rightRightRotation(Node *root){
    Node *rc = (*root).rc;
    (*root).rc = (*rc).lc;
    (*rc).lc = root;

    (*root).height = max(getHeight((*root).lc), getHeight((*root).rc))+1;
    (*rc).height = max(getHeight((*rc).lc), getHeight((*rc).rc));

    return rc;
}

Node* leftRightRotation(Node *root){
    (*root).lc = rightRightRotation((*root).lc);
    return leftLeftRotation(root);
}

Node* rightLeftRotation(Node *root){
    (*root).rc = leftLeftRotation((*root).rc);
    return rightRightRotation(root);
}

Node* valInsert(Node *root, int key){
    if(root==NULL)
        root = new Node(key, 0);
    else{
        if(key<(*root).val){
            (*root).lc = valInsert((*root).lc, key);
            if(getHeight(root->lc)-getHeight(root->rc)==2){
                if(key<(root->lc->val))
                    root = leftLeftRotation(root);
                else
                    root = leftRightRotation(root);
            }
        }
        else{
            if(key>(*root).val){
                (*root).rc = valInsert((*root).rc, key);
                if(getHeight(root->rc)-getHeight(root->lc)==2){
                    if(key>(root->rc->val))
                        root = rightRightRotation(root);
                    else
                        root = rightLeftRotation(root);
                }
            }
        }
    }
    (*root).height = max(getHeight(root->lc), getHeight(root->rc))+1;
    return root;
}

Node* getMax(Node *root){
    if(root==NULL)
        return NULL;
    while(root->rc!=NULL)
        root = root->rc;
    return root;
}

Node* getMin(Node *root){
    if(root==NULL)
        return NULL;
    while(root->lc!=NULL)
        root=root->lc;
    return root;
}

Node* valRemove(Node *root, int key){
    if(root==NULL)
        return NULL;
    if(key<root->val){
        root->lc = valRemove(root->lc, key);
        if(getHeight(root->rc)-getHeight(root->lc)==2){
            if(getHeight(root->rc->lc)>getHeight(root->rc->rc))
                root = rightLeftRotation(root);
            else
                root = rightRightRotation(root);
        }
    }
    else{
        if(key>root->val){
            root->rc = valRemove(root->rc, key);
            if(getHeight(root->lc)-getHeight(root->rc)==2){
                if(getHeight(root->lc->rc)>getHeight(root->lc->lc))
                    root = leftRightRotation(root);
                else
                    root = leftLeftRotation(root);
            }
        }
        else{
            if((root->lc!=NULL) && (root->rc!=NULL)){
                if(getHeight(root->lc)>getHeight(root->rc)){
                    Node *maxNode = getMax(root->lc);
                    root->val = maxNode->val;
                    root->lc = valRemove(root->lc, root->val);
                }
                else{
                    Node *minNode = getMin(root->rc);
                    root->val = minNode->val;
                    root->rc = valRemove(root->rc, root->val);
                }
            }
            else{
                Node *tmp = root;
                root = (root->lc!=NULL)?root->lc:root->rc;
                delete tmp;
            }
        }
    }
    (*root).height = max(getHeight(root->lc), getHeight(root->rc))+1;
    return root;
}


void test1(){
    Node *root=NULL;
    root = valInsert(root, 8);
    root = valInsert(root, 4);
    root = valInsert(root, 2);
    root = valInsert(root, 12);
    root = valInsert(root, 6);
    root = valInsert(root, 5);
    avlShow(root);
}

void test2(){
    Node *root=NULL;
    root = valInsert(root, 8);
    root = valInsert(root, 4);
    root = valInsert(root, 12);
    root = valInsert(root, 2);
    avlShow(root);
    valRemove(root, 4);
    avlShow(root);
}
int main()
{

//    test1();
    test2();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值