04-树5 Root of AVL Tree (25 分)

04-树5 Root of AVL Tree (25 分)
虽然借鉴了别人的思路,但是第一次自己写出代码,值得纪念,继续加油。
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SkZA7eBD-1612435619666)(~/31)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GrN2rsLu-1612435619668)(~/32)]

Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤20) which is the total number of keys to be inserted. Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:
For each test case, print the root of the resulting AVL tree in one line.

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

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

typedef struct TNode *PtrToTNode;
typedef PtrToTNode Tree;
struct TNode{
    int data;
    Tree left;
    Tree right;
};

Tree LeftSingleRotation(Tree T1);   //四种旋转
Tree RightSingleRotation(Tree T1);
Tree LeftRightRotation(Tree T1);
Tree RightLeftRotation(Tree T1);
int GetHeight(Tree T1);

Tree Insert(int n,Tree T1)  //后续输入节点依次插入并做平衡因子判断
{
    if(!T1)
    {
        T1 = (Tree)malloc(sizeof(struct TNode));
        T1->left = nullptr;
        T1->right = nullptr;
        T1->data = n;
        T1->height = 1;
    }
    else if(T1->data>n)
    {
        T1->left = Insert(n, T1->left);
        if(GetHeight(T1->left)-GetHeight(T1->right)==2) //左子树麻烦节点出现
        {
            if(n<T1->left->data)//左左
            {
                T1 = LeftSingleRotation(T1);
            }
            else if(n>T1->left->data)//左右
            {
                T1 = LeftRightRotation(T1);
            }
        }
    }
    else if(T1->data<n)
    {
        T1->right = Insert(n, T1->right);
        if(GetHeight(T1->right)-GetHeight(T1->left)==2) //右子树麻烦节点出现
        {
            if(n>T1->right->data)//右右
            {
                T1 = RightSingleRotation(T1);
            }
            else if(n< T1->right->data)//右左
            {
                T1 = RightLeftRotation(T1);
            }
        }
    }
    return T1;
}

Tree BuildTree(int n)   //第一个数据用于建树
{
    Tree T1;
    T1 = (Tree)malloc(sizeof(struct TNode));
    T1->data = n;
    T1->left = nullptr;
    T1->right = nullptr;
    return T1;
}

int GetHeight(Tree T1)  //递归求树高用作判断平衡依据
{
    int hl, hr,h;
    if(!T1)
        return 0;
    else
    {
        hl = GetHeight(T1->left);
        hr = GetHeight(T1->right);
        h = max(hl, hr);
        return h + 1;
    }
}

Tree LeftSingleRotation(Tree A)
{
    Tree B;
    B = A->left;
    A->left = B->right;
    B->right = A;
    //根结点和根节点的左子树高度发生变化,其余不变
    return B;//B成为原A树的根结点
}

Tree RightSingleRotation(Tree A)
{
    Tree B;
    B = A->right;
    A->right = B->left;
    B->left = A;

    return B;
}

Tree LeftRightRotation(Tree A)
{
    Tree B, C;
    B = A->left;
    C = B->right;
    A->left = C->right;
    B->right = C->left;
    C->right = A;
    C->left = B;
    return C;
}

Tree RightLeftRotation(Tree A)
{
    Tree B, C;
    B = A->right;
    C = B->left;
    A->right = C->left;
    B->left = C->right;
    C->left = A;
    C->right = B;
    return C;
}

int max(int a,int b)
{
    return a > b ? a : b;
}

int main()
{
    int n,da;
    Tree T;
    //cin >> n;
    scanf("%d", &n);
    //cin >> da;
    scanf("%d", &da);
    T = BuildTree(da);
    for (int i = 1; i < n;i++)
    {
        scanf("%d", &da);
        T = Insert(da, T);
    }
    printf("%d", T->data);
    system("pause");
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值