1066. Root of AVL Tree (25)

题目详情:https://www.patest.cn/contests/pat-a-practise/1066

提交:这里写图片描述

代码:

#include <iostream>
#include <malloc.h>
using namespace std;
#define N 22
int n,number[N];
typedef struct AVL
{
    int data,height; //height存储以当前节点为根节点的树的高度 
    struct AVL* leftChild;
    struct AVL* rightChild;
}AVL;
int getHeight( AVL* root )  //求以root为根节点的树高 
{
    if( root == NULL )
        return 0;
    else
    {
        int leftChild = getHeight(root->leftChild);//得到左子树的树高 
        int rightChild = getHeight(root->rightChild);//得到右子树的树高 
        return leftChild>rightChild?leftChild+1:rightChild+1;//返回两者中的较大值 
    }
}
void updateHeight( AVL* root ) //更新以root为根节点的树的高度 
{
    root->height = getHeight(root);  
}
int getHeightFactor( AVL* root ) //得到节点root的平衡因子 
{
    return getHeight(root->leftChild)-getHeight(root->rightChild);  
}
void leftRotation( AVL* &root )  //左旋 
{
    AVL* temp = root->rightChild;
    root->rightChild = temp->leftChild;
    temp->leftChild = root;
    updateHeight(root);
    updateHeight(temp);
    root = temp;
}
void rightRotation( AVL* &root ) //右旋 
{
    AVL* temp = root->leftChild;
    root->leftChild = temp->rightChild;
    temp->rightChild = root;
    updateHeight(root);
    updateHeight(temp);
    root = temp;
}
AVL* insertAVL( AVL* root,int key ) //往平衡二叉树上插入节点 
{
    if( root == NULL )  //找到插入位置 
    {
        root = (AVL*)malloc(sizeof(AVL));
        root->data = key;
        root->height = 1; //此时以该节点为根节点的树只有这一个节点,所以树高为1
        root->leftChild = root->rightChild =NULL; //置空 
        return root; 
    }
    else if( root->data > key )  //根节点的值较大,往左子树上查 
    {
        root->leftChild = insertAVL(root->leftChild,key);
        updateHeight(root);  //更新节点的高度
        if( getHeightFactor(root) == 2 ) //因为是往左子树上插,所以左子树的树高增大,平衡因子也是增大的 
        {
            if( getHeightFactor(root->leftChild) == 1 )  //若root->leftChild的平衡因子为1,则是LL型树 ,右转即可 
            {
                rightRotation(root);
            }
            else if( getHeightFactor(root->leftChild) == -1 ) //若root->leftChild的平衡因子为-1,则是LR型树 ,
            {                                                 //先左转(该节点的左子树左转)再右转 
                leftRotation( root->leftChild );
                rightRotation(root);
            }   
        }
        return root;    
    }
    else if( root->data < key )  //根节点的值较小,往右子树上查 
    {
        root->rightChild = insertAVL(root->rightChild,key);
        updateHeight(root);  //更新节点的高度
        if( getHeightFactor(root) == -2 ) //因为是往右子树上插,所以右子树的树高增大,平衡因子是减小的
        {
            if( getHeightFactor(root->rightChild) == -1 )  //若root->leftChild的平衡因子为-1,则是RR型树 ,左转即可 
            {
                leftRotation(root);
            }
            else if( getHeightFactor(root->rightChild) == 1 ) //若root->rightChild的平衡因子为1,则是RL型树 , 
            {                                                 //先右转(该节点的右子树左转)再左转 
                rightRotation( root->rightChild );
                leftRotation(root);
            }   
        } 
        return root;
    }
}
int main ()
{
    cin>>n;
    AVL* root = NULL;
    for( int i=0;i<n;++i ) //处理输入 
    {
        cin>>number[i];
        root = insertAVL(root,number[i]);
    }
    cout<<root->data<<endl;
    return 0;
}

我参考着《算法笔记》写出来的,至于为什么节点的平衡因子不会超过[-2,2]这个范围,个人猜想插入一个节点,平衡因子只能渐变为2或者-2,而一旦发现平衡因子为2或者-2,当即调整,所以不会超出这个范围。又可以根据孩子节点的平衡因子值判断是什么样的树形,进行相应的调整。
以前以为平衡二叉树挺难得,今天敲出来感觉也就这样 [手动傲娇状].另外有些程序很相似,比如上面的左旋,右旋与交换两个数的函数(如下):

交换两个数

void swap ( int &a,int &b )
{
    int temp = a;
    a = b;
    b = temp;
}

左旋:

void leftRotation( AVL* &root )  //左旋 
{
    AVL* temp = root->rightChild;
    root->rightChild = temp->leftChild;
    temp->leftChild = root;
    updateHeight(root);
    updateHeight(temp);
    root = temp;
}

右旋:

void rightRotation( AVL* &root ) //右旋 
{
    AVL* temp = root->leftChild;
    root->leftChild = temp->rightChild;
    temp->rightChild = root;
    updateHeight(root);
    updateHeight(temp);
    root = temp;
}

发现没?都是首尾相接的,有点对称的感觉。
在这里借用这篇博客中的图: http://blog.csdn.net/collonn/article/details/20128205
情况1和情况2都是LL型树,需要进行右转。
情况3和情况4都是RR型树,需要进行左转。
情况5和情况6都是LR型树,先失衡节点的左子树左转,转换成LL型后再右转。
情况5和情况6都是RL型树,先失衡节点的右子树右转,转换成RR型后再左转。

AVL树在插入结束后,递归返回的过程检查各个节点的平衡因子,并调整。而插入的过程和BST树基本相同。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值