码迷,mamicode.com
首页 > 其他好文 > 详细

04-树5 Root of AVL Tree

时间:2017-12-08 11:58:50      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:names   actor   put   input   inpu   一个   include   比较大小   algo   

题目

技术分享图片
技术分享图片
Sample Input 1:
5
88 70 61 96 120
Sample Output 1:
70
Sample Input 2:
7
88 70 61 96 120 90 65
Sample Output 2:
88

基本思路

考差了AVL树的插入操作,插入可以用一个递归函数完成,比较大小然后向左或右递归,递归边界为遇到空结点,返回时,检查每个结点的平衡因子是否为2或-2,是的话则进行旋转。

代码

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define AVL 
typedef struct Node* link;
struct Node
{
    int val;
    int height;
    link left;
    link right;
};

link insert(link & T, int val);
void L_Rotate(link & root);
void R_Rotate(link & root);
void freeTree(link T);
int getHeight(link T);
void updateHeight(link T);
int getBalanceFactor(link T);

int main()
{
    int N, temp;
    scanf("%d", &N);
    link root=NULL;
    for (int i = 0; i < N; i++)
    {
        scanf("%d", &temp);
        insert(root, temp);
    }
    printf("%d", root->val);
    return 0;
}



void R_Rotate(link & root)
{
    link A, B, C;
    A = root;
    B = A->left;
    C = B->right;
    A->left = C;
    B->right = A;
    updateHeight(A);
    updateHeight(B);
    root = B;
}

void L_Rotate(link & root)
{
    link A, B, C;
    A = root;
    B = A->right;
    C = B->left;
    A->right = C;
    B->left = A;
    updateHeight(A);
    updateHeight(B);
    root = B;
}
link insert(link & T, int val)
{
    
    if (T == NULL)          //如果该结点为空则创立新结点,结束递归
    {
        T = new Node;
        T->val = val;
        T->left = NULL;
        T->right = NULL;
        T->height = 1;             //插入的新结点高度为1
        return T;

    }

    
    if (val > T->val)                                 //插入右边  
    {
        T->right = insert(T->right, val);
        updateHeight(T);
#if defined(AVL)              //如果是AVL树,完成插入操作后要检查平衡因子
        if (getBalanceFactor(T) == -2)
        {
            if (getBalanceFactor(T->right) == -1)    //RR
            {
                L_Rotate(T);
            }
            else if (getBalanceFactor(T->right) == 1)  //LR
            {
                R_Rotate(T->right);
                L_Rotate(T);
            }

        }
#endif
    }
    else
    {
        T->left = insert(T->left, val);
        updateHeight(T);
#if defined(AVL)              //如果是AVL树,完成插入操作后要检查平衡因子
        if (getBalanceFactor(T) == 2)
        {
            if (getBalanceFactor(T->left) == 1)    //RR
            {
                R_Rotate(T);
            }
            else if (getBalanceFactor(T->left) == -1)  //LR
            {
                L_Rotate(T->left);
                R_Rotate(T);
            }
        }
#endif
    }

    return T;
}



void freeTree(link T)
{
    if (T->right)
        freeTree(T->right);
    if(T->left)
        freeTree(T->left);
    delete T;
}

int getHeight(link T)
{
    if (T == NULL)
        return 0;
    else
        return T->height;
}

int getBalanceFactor(link T)
{
    return getHeight(T->left)-getHeight(T->right);
}

void updateHeight(link T)
{
    T->height = max(getHeight(T->left), getHeight(T->right))+1;
}

总结

04-树5 Root of AVL Tree

标签:names   actor   put   input   inpu   一个   include   比较大小   algo   

原文地址:http://www.cnblogs.com/messier/p/8004192.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!