04-1. Root of AVL Tree (25)

04-1. Root of AVL Tree (25)

时间限制

100 ms

内存限制

65536 kB

代码长度限制

8000 B

判题程序

Standard

作者

CHEN, Yue

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.

    

    

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 ythe 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


提交代

题意:给你n个数  输出以他们建立的平衡查找树的根结点

参考:http://www.cppblog.com/cxiaojia/archive/2013/07/22/187776.html

http://blog.csdn.net/realxuejin/article/details/12872035 两篇博文

#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
//#define lson rt<<1,l,MID
//#define rson rt<<1|1,MID+1,r
//#define lson root<<1
//#define rson root<<1|1
//#define MID ((l+r)>>1)
typedef long long ll;
typedef pair<int,int> P;
const int maxn=50005;
const int base=1000;
const int inf=999999;
const double eps=1e-5;
struct node//树的结点
{
    int data;//数据域
    int high;///树的高度
    node *lson,*rson;//左右儿子
    node(){lson=rson=NULL;high=0;}//构造函数  初始化的作用
};

int getHigh(node *t)//取高度函数
{
   if(t!=NULL)
       return t->high;
   return -1;
}
node *LL(node *k2)//左左单旋
{
    node *k1;
    k1=k2->lson;
    k2->lson=k1->rson;
    k1->rson=k2;

    k2->high=max(getHigh(k2->lson),getHigh(k2->rson))+1;
    k1->high=max(getHigh(k1->lson),getHigh(k1->rson))+1;
    return k1;
}

node *RR(node *k2)//右右单旋
{
    node *k1;
    k1=k2->rson;
    k2->rson=k1->lson;
    k1->lson=k2;
    k2->high=max(getHigh(k2->lson),getHigh(k2->rson))+1;
    k1->high=max(getHigh(k1->lson),getHigh(k1->rson))+1;
    return k1;
}

node* LR(node *k3)//左右双旋
{
    k3->lson=RR(k3->lson);
    return LL(k3);

}

node* RL(node * k3)//右左双旋
{
    k3->rson=LL(k3->rson);
    return RR(k3);
}

bool isbalanced(node *a,node *b)//判定左右子树高度差
{
    return abs(getHigh(a)-getHigh(b))<2;
}

node* insert(node *root,int v)//建立树的过程
{
    if(root==NULL)
    {
        root=new node();
        root->data=v;
        return root;
    }
    if(v>root->data)
    {
        root->rson=insert(root->rson,v);
        if(!isbalanced(root->lson,root->rson))
        {
            if(v>root->rson->data)
                root=RR(root);
            else
                root=RL(root);
        }

    }
    else
    {
        root->lson=insert(root->lson,v);
        if(!isbalanced(root->lson,root->rson))
        {
            if(v>root->lson->data)
                root=LR(root);
            else
                root=LL(root);
        }
    }
    root->high=max(getHigh(root->lson),getHigh(root->rson))+1;
    return root;
}
int main()
{
    int n,m,j,i,k,t;
    cin>>n;
    node* T=NULL;
    while(n--)
    {
        cin>>t;
        T=insert(T,t);
    }
    printf("%d\n",T->data);
    return 0;
}
时间: 2024-10-01 05:57:00

04-1. Root of AVL Tree (25)的相关文章

04-树4. Root of AVL Tree (25)

04-树4. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 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 tim

1066. Root of AVL Tree (25)【AVL树】——PAT (Advanced Level) Practise

题目信息 1066. Root of AVL Tree (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B 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 tha

pat1066. Root of AVL Tree (25)

1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 tim

pat04-树4. Root of AVL Tree (25)

04-树4. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 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 tim

PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***

1066 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

A1066. 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 illust

1066. Root of AVL Tree (25)

时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 o

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 illust

A1066 Root of AVL Tree (25分)

一.技术总结 这是一个平衡二叉树AVL树,就是一个二叉查找树,但是平衡因子不能够超过1. 这个树的数据结构比一般的要多一个height的参数,用于计算平衡因子,就是用当前结点的左子树的height减去右子树的height. 对于node* newNode(int data)函数,首先要新建一个结点,然后初始化height参数为1,然后左右子树结点赋值为NULL. getHeight()函数和updateHeight()函数,后者就是根据当前结点的左右子树结点高度的最大值,然后加一. getBF(