国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

A1066. Root of AVL Tree (25)

2019-11-10 19:39:13
字體:
供稿:網(wǎng)友

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結(jié)點的height設(shè)成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;//高度可理解為以該節(jié)點為根的樹的層數(shù) }*root,*null; void init(){    null=new node;    null->height=0;    root=null;//null為 高度為0 }node* newNode(int v)//設(shè)置新的節(jié)點 {     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;}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿瓦提县| 甘德县| 德清县| 澜沧| 齐河县| 通州市| 高淳县| 宜兰县| 潜江市| 轮台县| 东阿县| 锡林浩特市| 山东省| 鹰潭市| 青田县| 绥滨县| 涞源县| 拉萨市| 武穴市| 鹰潭市| 清涧县| 东海县| 沙雅县| 永川市| 齐河县| 渭南市| 仙居县| 香港 | 曲阳县| 高碑店市| 庆阳市| 慈利县| 积石山| 嘉善县| 历史| 三原县| 杭锦后旗| 台南市| 无棣县| 青铜峡市| 大竹县|