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

首頁 > 學院 > 開發設計 > 正文

A1066. Root of AVL Tree (25)

2019-11-10 17:11:34
字體:
來源:轉載
供稿:網友

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;}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 容城县| 兴安县| 临猗县| 达拉特旗| 哈密市| 莫力| 常山县| 富川| 惠水县| 浮山县| 宝清县| 韶关市| 民乐县| 江孜县| 通渭县| 广灵县| 扶绥县| 华安县| 河津市| 安图县| 广西| 汤阴县| 且末县| 建瓯市| 拉萨市| 枣阳市| 微博| 南川市| 南木林县| 四子王旗| 阳原县| 阜阳市| 海伦市| 浏阳市| 浮山县| 高唐县| 嘉义县| 漳平市| 长葛市| 贵港市| 拉孜县|