首页 > 学院 > 开发设计 > 正文

A1066. Root of AVL Tree (25)

2019-11-10 17:23:44
字体:
来源:转载
供稿:网友

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:
588 70 61 96 120Sample Output 1:
70Sample Input 2:
788 70 61 96 120 90 65Sample Output 2:
88
注意:
开始时要初始化,将root结点的height设成0,否则段错误。
要注意左旋和右旋的写法。
#include <stdio.h>#include <stdlib.h>#include <iostream>#include <string.h>#include <math.h>#include <algorithm>#include <string>#include <stack> #include <queue>using namespace std;const int maxn=110; int n,m,s;struct node{ node *left; node *right; int  v,height;//高度可理解为以该节点为根的树的层数 }*root,*null; void init(){    null=new node;    null->height=0;    root=null;//null为 高度为0 }node* newNode(int v)//设置新的节点 {     node* t=new node();     t->v=v;     t->height=1;    t->left=t->right=null;     return t; } void getNewheight(node *root){    root->height=max(root->left->height,root->right->height)+1;}void L(node *&root){    node *tmp=root->right;    root->right=tmp->left;    tmp->left=root;    getNewheight(root);    getNewheight(tmp);    root=tmp;    }void R(node *&root){    node* tmp=root->left;    root->left=tmp->right;    tmp->right=root;    getNewheight(root);    getNewheight(tmp);    root=tmp;}void insert(node *&root,int v){    if(root==null)    {     root=newNode(v);     return;        }     if(v<root->v)    {        insert(root->left,v);        getNewheight(root);        if((root->left->height)-(root->right->height)==2)        {            //ll型             if((root->left->left->height)-(root->left->right->height)==1)            {                R(root);             }else if((root->left->left->height)-(root->left->right->height)==-1)            {                //lr                L(root->left);                R(root);            }        }    }else    {        insert(root->right,v);        getNewheight(root);        if((root->left->height)-(root->right->height)==-2)        {            if((root->right->left->height)-(root->right->right->height)==1)            {//rl                R(root->right);                L(root);            }else if((root->right->left->height)-(root->right->right->height)==-1)            {	//rr                L(root);            }        }    }}int main(){    int n,v;    init();    scanf("%d",&n);    for(int i=0;i<n;i++)    {        scanf("%d",&v);        insert(root,v);    }    printf("%d/n",root->v);    return 0;}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表