首页 > 其他分享 >1066 Root of AVL Tree

1066 Root of AVL Tree

时间:2023-05-25 09:22:40浏览次数:38  
标签:lchild Node temp root getHeight Tree AVL rchild Root

题目:

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.

F1.jpg F2.jpg

 

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

 

 

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int n;
struct Node{
    Node* lchild, *rchild;  
    int v;
};
void L(Node* &root){
    Node* temp = root -> rchild;
    root -> rchild = temp -> lchild;
    temp -> lchild = root;
    root = temp;
}
void R(Node* &root){
    Node* temp = root -> lchild;
    root -> lchild = temp -> rchild;
    temp -> rchild = root;
    root = temp;
}
int getHeight(Node* root){
    if(root == NULL) return 0;
    return max(getHeight(root -> lchild), getHeight(root -> rchild)) + 1;
}
void insert(Node* &root, int v){
    if(root == NULL){
        root = new Node();
        root -> v = v;
        root -> lchild = NULL;
        root -> rchild = NULL;
        return;
    }
    if(root -> v > v){
        insert(root -> lchild, v);
        if(getHeight(root -> lchild) - getHeight(root -> rchild) == 2){
            if(v < root -> lchild -> v){
                R(root);
            }else{
                L(root -> lchild);
                R(root);
            }
        }
    }else{
        insert(root -> rchild, v);
        if(getHeight(root -> rchild) - getHeight(root -> lchild) == 2){
            if(v > root -> rchild -> v){
                L(root);
            }else{
                R(root -> rchild);
                L(root);
            }
        }
    }
}
int main(){
    scanf("%d", &n);
    Node *root = NULL;
    for(int i = 0; i < n; i++){
        int data;
        scanf("%d", &data);
        insert(root, data);
    }
    printf("%d", root->v);
    return 0;
}

 

标签:lchild,Node,temp,root,getHeight,Tree,AVL,rchild,Root
From: https://www.cnblogs.com/yccy/p/17430182.html

相关文章

  • CF1819C The Fox and the Complete Tree Traversal
    \(\color{purple}\text{TheFoxandtheCompleteTreeTraversal}\)比较有意思的一题。先考虑一个序列的权值。对长度为\(len\)的序列排序,价值为\(len-1\),那么有时候如果后面的元素很大,前面的很小:321300200100我们可以将序列切为\([1,3]\),和\([4,6]\)两部分分别......
  • sourceTree环境配置
    安装并配置完成git1、在gitbrashhere中允许命令ssh-keygen-ted25519-C"[email protected]" 按照提示完成三次回车,在用户目录下默认生成文件夹.ssh,打开可以找到id_rsa.pub文件,获取到你的publickeycat~/.ssh/id_ed25519.pub复制生成的SSH,通过仓库主页「管理」->「部署......
  • 1110 Complete Binary Tree(附测试点2,3,4,6分析)
    题目:Givenatree,youaresupposedtotellifitisacompletebinarytree.InputSpecification:Eachinputfilecontainsonetestcase.Foreachcase,thefirstlinegivesapositiveinteger N (≤20)whichisthetotalnumberofnodesinthetree--andh......
  • el-tree 自动展开
    el-tree自动展开需求:通过输入来筛选树中的数据,由于数据是通过懒加载得到的。因此需要手动的点击每个节点来展开它们。然而,如何才能不通过手动点击来展开所有节点呢?利用默认展开节点属性:default-expanded-keys="expandList"把当前分类节点数据加入默认展开的列表中。然后遍......
  • Paper Reading: forgeNet a graph deep neural network model using tree-based ensem
    目录研究动机文章贡献本文方法图嵌入深度前馈网络forgeNet特征重要性评估具体实现模拟实验合成数据生成实验评估实验结果真实数据应用BRCA数据集microRNA数据Healthyhumanmetabolomics数据集优点和创新点PaperReading是从个人角度进行的一些总结分享,受到个人关注点的侧重......
  • MYSQL设置密码时显示Failed! Error: SET PASSWORD has no significance for user 'roo
    ​ 用这个命令进入mysqlsudomysql在sql命令行输入以下命令回车,你就可以把密码改成mynewpasswordALTERUSER'root'@'localhost'IDENTIFIEDWITHmysql_native_passwordby'mynewpassword';exit回到终端命令行,输入:sudomysql_secure_installation输入刚才的......
  • MYSQL设置密码时显示Failed! Error: SET PASSWORD has no significance for user 'roo
    ​ 用这个命令进入mysqlsudomysql在sql命令行输入以下命令回车,你就可以把密码改成mynewpasswordALTERUSER'root'@'localhost'IDENTIFIEDWITHmysql_native_passwordby'mynewpassword';exit回到终端命令行,输入:sudomysql_secure_installation输入刚才的......
  • Linux - centos6忘记root密码怎么办?
     Linux的root密码修改不像Windows的密码修改找回,Windows的登录密码忘记需要介入工具进行解决。CentOS6和CentOS7的密码方法也是不一样的,具体如下 1、开机按esc 2、按e键进入编辑模式 3、进入该编辑模式后,在quiet后面输入simple或者1然后回车 4、按b键进......
  • java.sql.SQLException: Access denied for user 'root'@'localhost' (using password
    org.apache.ibatis.exceptions.PersistenceException:###Errorqueryingdatabase.Cause:java.sql.SQLException:Accessdeniedforuser'root'@'localhost'(usingpassword:YES)###Theerrormayexistincom/itheima/mapper/BrandMapper.j......
  • 1099 Build A Binary Search Tree
    题目:ABinarySearchTree(BST)isrecursivelydefinedasabinarytreewhichhasthefollowingproperties:Theleftsubtreeofanodecontainsonlynodeswithkeyslessthanthenode'skey.Therightsubtreeofanodecontainsonlynodeswithkeysg......