04-Tree 5 Root of AVL Tree (25 min)

Source: Internet
Author: User
Tags min
04-Tree 5 Root of AVL Tree (25 min)

An AVL tree is a self-balancing binary search tree. In a AVL tree, the heights of the subtrees of any node differ by at the most one; If at any time they differ by more than one, the rebalancing is the done to restore this property. Figures 1-4 illustrate the rotation rules.

Now given a sequence of insertions, you is supposed to the root of the resulting AVL tree.

Input Specification:

Each input file contains the one test case. For each case, the first line contains a positive integer NN (20≤20) which are the total number of keys to being inserted. Then NN distinct integer keys is given in the next line. All the numbers in a line is separated by a space. Output Specification:

For each test case, print the root of the resulting AVL tree on 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
Main ideas:
1. Four cases of inserting new nodes of binary balance tree (recursive insertion here)
2, the realization of LL,RR,LR,RL rotation
#include <iostream> #include <vector> using namespace std;
    #define MAX_NODE 1000000 typedef struct NODE//tree Node {int left;
    int right;
int height;

}node;
    int Get_height (vector<node>& tree,int root)//recursively find the node's height in the binary tree {int hl,hr,maxh;
        if (root) {hl=get_height (Tree, tree[root].left);
        Hr=get_height (Tree, tree[root].right);
        if (hl>hr) {maxh=hl;
        }else {maxh=hr;
    } return (maxh+1);
    }else {return 0;
    }} int singleleftrotation (int a,vector<node>& Tree)//ll rotate {int b=tree[a].left;
    Tree[a].left=tree[b].right;
    Tree[b].right=a;
    Tree[a].height=get_height (Tree, A);
    Tree[b].height=get_height (Tree, B);
return B;
    } int singlerightrotation (int a,vector<node>& Tree)//rr rotates {int b=tree[a].right;
    Tree[a].right=tree[b].left;
    Tree[b].left=a;
    Tree[a].height=get_height (Tree, A); Tree[b].hEight=get_height (Tree, B);
return B; } int doubleleftrightrotation (int a,vector<node>& Tree)//lr rotate {tree[a].left=singlerightrotation (Tree[A].l
    EFT, Tree);
Return Singleleftrotation (A, Tree); } int doublerightleftrotation (int a,vector<node>& Tree)//rl rotate {tree[a].right=singleleftrotation (TREE[A].R
    ight, Tree);
Return Singlerightrotation (A, Tree); } int avl_insertion (int x,vector<node>& tree,int T)//Two fork balance tree Insert a new node {if (!
        T) {tree[x].left=0;
        tree[x].right=0;
        tree[x].height=0;
    T=x; }else {if (x<t)//is less than the current node, inserted into the left subtree of the current node {tree[t].left=avl_insertion (X, Tree, Tree[t].left
            );
                if (Get_height (tree, Tree[t].left)-get_height (tree, Tree[t].right) ==2)//In case of an imbalance after insertion (only possible for LL or LR case) {
                if (x<tree[t].left) {t=singleleftrotation (T, Tree); }else {T=doubLeleftrightrotation (T, Tree); }}}else if (x>t)//is greater than the current node, insert the right subtree of the current node {tree[t].right=avl_insertion (X, Tree, Tr
            Ee[t].right); if (Get_height (tree, Tree[t].left)-get_height (tree, Tree[t].right) ==-2) {if (X>tree[t].rig
                HT) {t=singlerightrotation (T, Tree);
                }else {t=doublerightleftrotation (T, Tree);
    }}}} tree[t].height=get_height (Tree, T);
return T;
    } int main () {vector<node> Tree (max_node);
    int root=0;
    int n=0;
    cin>>n;
    int x=0;
        while (n--) {cin>>x;
    Root=avl_insertion (X, Tree, Root);
    } cout<<root;
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.