AVLtree

/*******************************
author: yomi
date: 17.1.13
mood:짱짱짱
ps:기뿐이 너무 촣다~
*******************************/
///这个是按照深度 判断平衡程度 也可以直接定义一个平衡程度变量
#include<cstdio>
#include <algorithm>
#include <iostream>
using namespace std;


typedef struct treenode
{
        int data;
        treenode *lchild;
        treenode *rchild;
        int height;     /*深度*/
}node,*AVLTree;

void pre_(node *t)
{
    if(t)
    {
        cout << t->data << ' ';
        pre_(t->lchild);
        pre_(t->rchild);
    }
}
void in(node *t)
{
    if(t)
    {
        in(t->lchild);
        cout << t->data << ' ';
        in(t->rchild);
    }
}

int Height(AVLTree T)
{
    if(!T)
    {
        return -1;
    }
    else
    {
        return T->height;
    }
}
///LL
AVLTree R_Rotate(AVLTree k1)/// the deeper one is on the left.
{
    AVLTree k2;

    k2 = k1->lchild;
    k1->lchild = k2->rchild;
    k2->rchild = k1;

    k1->height =  max(Height(k1->rchild),Height(k1->lchild))+1;
    k2->height =  max(Height(k2->rchild),Height(k2->lchild))+1;
    return k2;
}

///RR
AVLTree L_Rotate(AVLTree k1)
{
    AVLTree k2;

    k2 = k1->rchild;
    k1->rchild = k2->lchild;
    k2->lchild = k1;

    k1->height =  max(Height(k1->rchild),Height(k1->lchild))+1;
    k2->height =  max(Height(k2->rchild),Height(k2->lchild))+1;
    return k2;
}

///8 2 4
AVLTree LR(AVLTree k1)
{

    AVLTree k2;
    k2 = k1->lchild;
    cout << "from root's lchild: ";
    pre_(k2);///2 4
    cout << endl;
    k2 = L_Rotate(k2);
    cout << "from root's lchild: ";
    pre_(k2);/// 4 2
    cout << endl;
    k1->lchild = k2;
    cout << "from root : ";///
    pre_(k1);
    cout << endl;
    return R_Rotate(k1);
}


AVLTree RL(AVLTree k1){
    AVLTree k2;
    k2 = k1->rchild;
    k2 = R_Rotate(k2);
    k1->rchild = k2;
    return L_Rotate(k1);
}
AVLTree AVL_Insert(AVLTree *T, int e)
{
    if (*T == NULL)         //如果空树,就构造一颗根结点
    {
        *T = new node;
        if(!*T)
        {
            return NULL;
        }

        (*T)->data = e;
        (*T)->lchild = NULL;
        (*T)->rchild = NULL;
        (*T)->height = 0;     
    }
    else if(e < (*T)->data)        
    {
        (*T)->lchild =AVL_Insert(&(*T)->lchild,e);          

        if (Height((*T)->lchild) - Height((*T)->rchild) == 2)         
        {

            if (e < (*T)->lchild->data)               
            {
                *T = R_Rotate(*T);
            }
            else
            {
                cout << "LR" << endl;
                *T = LR(*T);     

            }
        }
    }
    else if(e > (*T)->data)          
    {
        (*T)->rchild =AVL_Insert(&(*T)->rchild,e);
        if (Height((*T)->rchild) - Height((*T)->lchild) == 2)
        {
            if (e > (*T)->rchild->data)
            {
                *T = L_Rotate(*T);
            }
            else
            {
                *T = RL(*T);
            }
        }
    }

    (*T)->height = max(Height((*T)->rchild),Height((*T)->lchild))+1;       
    return *T;
}

int main()
{
        AVLTree T = NULL;
        int n;
        int f;
        cin >> f;
        for (int i=1;i<=f;i++)
        {
            cin >> n;
            AVL_Insert(&T,n);
        }

        pre_(T);
        cout << endl;
        in(T);

        cout << endl << endl;
    return 0;
}
///如此可怕。。。
/*
2 4 7 8 5 6
7 4 2 6 8
2 4 6 7 8

5 6 8 7 4 2
6 4 2 5 8 7
2 4 5 6 7 8

6 2 5 4 8 7
6 2 8 7
2 6 7 8

4
8 2 4 7
8 2
2 8

3
8 2 4
2 8
2 8



*/

/*
3
8 2 4
LR
from root's lchild: 2 4
from root's lchild: 4 2
from root : 8 4 2
4 2 8
2 4 8

3
2 4 8
4 2 8
2 4 8

3
4 8 2
4 2 8
2 4 8

6
2 4 7 8 5 6
5 4 2 7 6 8
2 4 5 6 7 8

6
6 2 5 4 8 7
LR
from root's lchild: 2 5
from root's lchild: 5 2
from root : 6 5 2
5 2 4 7 6 8
2 4 5 6 7 8


*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值