• 1066 Root of AVL Tree (25 分)


    1066 Root of AVL Tree (25 分)
     

    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 (≤) 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

    前面学完AVL这题就是板子题了。
    只要插入就行,然后输出根节点。

     1 #include <iostream>
     2 #include <cmath>
     3 #include <vector>
     4 #include <algorithm>
     5 #include <queue>
     6 using namespace std;
     7 struct Node {
     8   int val;
     9   Node *left, *right;
    10 } *tree;
    11 int n, m;
    12 
    13 Node *leftrotate(Node * tree) {
    14   Node *temp = tree->left;
    15   tree->left = temp->right;
    16   temp->right = tree;
    17   return temp;
    18 }
    19 
    20 Node *rightrotate(Node * tree) {
    21   Node *temp = tree->right;
    22   tree->right = temp->left;
    23   temp->left = tree;
    24   return temp;
    25 }
    26 
    27 Node *rightleftrotate(Node * tree) {
    28   tree->left = rightrotate(tree->left);
    29   return leftrotate(tree);
    30 }
    31 
    32 Node *leftrightrotate(Node * tree) {
    33   tree->right = leftrotate(tree->right);
    34   return rightrotate(tree);
    35 }
    36 
    37 int getlen(Node * root) {
    38   if (root == NULL)
    39     return 0;
    40   return max(getlen(root->left), getlen(root->right)) + 1;
    41 }
    42 Node *insert(Node * tree, int val) {
    43   if (tree == NULL) {
    44     tree = new Node();
    45     tree->val = val;
    46   } else if (val < tree->val) {
    47     tree->left = insert(tree->left, val);
    48     int ll = getlen(tree->left), rr = getlen(tree->right);
    49     if (ll - rr >= 2) {
    50       if (val < tree->left->val)
    51         tree = leftrotate(tree);
    52       else
    53         tree = rightleftrotate(tree);
    54     }
    55   } else if (val > tree->val) {
    56     tree->right = insert(tree->right, val);
    57     int ll = getlen(tree->left), rr = getlen(tree->right);
    58     if (rr - ll >= 2) {
    59       if (val > tree->right->val)
    60         tree = rightrotate(tree);
    61       else
    62         tree = leftrightrotate(tree);
    63     }
    64   }
    65   return tree;
    66 }
    67 vector<int> v, vt;
    68 void output(Node *root, int x){
    69     if(root != NULL){
    70         output(root->left, x<<1);
    71           //cout <<root->val<<endl;
    72         output(root->right, (x<<1)+1);
    73     }else{
    74         v.push_back(x);
    75     }
    76 }    
    77 
    78 int main() {
    79   cin >> n;
    80   for (int i = 0; i < n; i++) {
    81     cin >> m;
    82     tree = insert(tree, m);
    83   }
    84   cout << tree->val << endl;
    85   return 0;
    86 }
     
     
     
     
  • 相关阅读:
    CentOS 6.5通过yum的方式安装MySql
    Hbase集群搭建
    Thread类的常见问题
    关hashMap跟hashTable的区别
    mysql 循环插入100w
    Centos 多个mysql数据库
    CentOS 搭建 FastDFS-5.0.5集群
    RPC
    dubbo简述
    自己去看dubbo源码
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11329696.html
Copyright © 2020-2023  润新知