麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

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
注意:
開始時(shí)要初始化,將root結(jié)點(diǎn)的height設(shè)成0,否則段錯(cuò)誤。
要注意左旋和右旋的寫法。
#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é)點(diǎn)為根的樹的層數(shù) }*root,*null; void init(){    null=new node;    null->height=0;    root=null;//null為 高度為0 }node* newNode(int v)//設(shè)置新的節(jié)點(diǎn) {     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ā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 一级性生活视频 | 免费黄色在线 | 中文字幕1区2区 | 麻豆传传媒久久久爱 | 成人国产综合 | 中文字幕在线观看视频一区 | 国产成人精品午夜视频' | 九九视频精品在线观看 | 午夜色视频在线观看 | 久久国产成人精品国产成人亚洲 | 久久久久北条麻妃免费看 | 久久艳片 | 日韩蜜桃视频 | 国产孕妇孕交大片孕 | 久久伊人国产精品 | 欧美成人精品h版在线观看 国产一级淫片在线观看 | 毛片视频大全 | 九九热九九 | 久久精品视频黄色 | 久久久精品视频免费看 | 黄色片免费在线播放 | 一区视频 | 久久亚洲国产精品 | av中文在线观看 | 成人免费久久 | 久久不射电影 | 久久久精彩| 国产亚洲精品久久久久5区 日韩一级片一区二区三区 国产精品久久久久av | 国产亚洲自拍一区 | 国产精品久久久乱弄 | 国产精品久久久久久久久久久天堂 | 精品一区二区久久久久久按摩 | 九九视频精品在线 | 在线观看第一区 | 免费亚洲视频在线观看 | 欧美爱爱视频免费看 | 一区二区三区日韩在线观看 | 欧美1区2区在线观看 | 欧美精品一区二区性色 | 国内精品久久久久久2021浪潮 | 黄色小视频免费在线观看 |