04-樹5 Root of AVL Tree(25 分)

傳送門

04-樹5 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 (≤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 <iostream>
#include <cstdlib>

using namespace std;

typedef struct _node {int v;struct _node * left;struct _node * right;int height;} *AVLtree;
int max(int a, int b);
AVLtree insert (AVLtree t, int x);
AVLtree singleLeftRotation(AVLtree a);
AVLtree doubleLeftRightRotation(AVLtree a);
AVLtree doubleRightLeftRotation(AVLtree a);
AVLtree singleRightRotation(AVLtree a);
int getHeight(AVLtree t);

int main()
{
    int n, v;
    cin >> n;
    AVLtree root = NULL;
    for ( int i=0; i<n; i++ ) {
        cin >> v;
        root = insert(root, v);
    }
    cout << root->v;
    return 0;
}

int max(int a, int b)
{
    return a>b ? a:b;
}

AVLtree insert (AVLtree t, int x)
{
    if ( !t ) {//empty tree
        t = (AVLtree)malloc(sizeof(struct _node));
        t->v = x;
        t->left = t->right = NULL;
        t->height = 0;
    } else if ( x<t->v ) {
        t->left = insert(t->left, x);
        if ( getHeight(t->left)-getHeight(t->right)==2 ) {
            if ( x<t->left->v ) t = singleLeftRotation(t);
            else t = doubleLeftRightRotation(t);
        }
    } else if ( x>t->v ) {
        t->right = insert(t->right, x);
        if ( getHeight(t->left)-getHeight(t->right)==-2 ) {
            if ( x>t->right->v ) t = singleRightRotation(t);
            else t = doubleRightLeftRotation(t);
        }
    }
    //renew the height
    t->height = max( getHeight(t->left), getHeight(t->right) ) + 1;
    return t;
}

AVLtree singleLeftRotation(AVLtree a)
{
    AVLtree b = a->left;
    a->left = b->right;
    b->right = a;
    a->height = max( getHeight(a->left), getHeight(a->right) ) + 1;
    b->height = max( getHeight(b->left), getHeight(b->right) ) + 1;
    return b;
}

AVLtree doubleLeftRightRotation(AVLtree a)
{
    AVLtree b = a->left, c = b->right, cl = c->left, cr = c->right;
    a->left = cr;
    b->right = cl;
    c->left = b;
    c->right= a;
    a->height = max( getHeight(a->left), getHeight(a->right) ) + 1;
    b->height = max( getHeight(b->left), getHeight(b->right) ) + 1;
    c->height = max( getHeight(c->left), getHeight(c->right) ) + 1;
    return c;
}

AVLtree doubleRightLeftRotation(AVLtree a)
{
    AVLtree b = a->right, c = b->left, cl = c->left, cr = c->right;
    a->right = cl;
    b->left = cr;
    c->right = b;
    c->left = a;
    a->height = max( getHeight(a->left), getHeight(a->right) ) + 1;
    b->height = max( getHeight(b->left), getHeight(b->right) ) + 1;
    c->height = max( getHeight(c->left), getHeight(c->right) ) + 1;
    return c;
}

AVLtree singleRightRotation(AVLtree a)
{
    AVLtree b = a->right;
    a->right = b->left;
    b->left = a;
    a->height = max( getHeight(a->left), getHeight(a->right) ) + 1;
    b->height = max( getHeight(b->left), getHeight(b->right) ) + 1;
    return b;
}

int getHeight(AVLtree t)
{
    if ( !t ) return 0;
    else return t->height;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 你六周歲了,上小學(xué)了。 中午到家,你告訴我,期中考試數(shù)學(xué)考了一百分。你很開心。我夸獎(jiǎng)了你,因?yàn)槟闫綍r(shí)就把做錯(cuò)的題目...
    小章魚fancy閱讀 407評(píng)論 0 1
  • 熱油鍋爆蒜末、姜末,加入紫菜洗水后裝清水入鍋煮開,加入打碎勻的雞蛋,調(diào)入鹽和雞精,關(guān)火,盛入裝有蔥花的盆里
    渡把閱讀 309評(píng)論 0 0
  • 尹天佑跑到宿舍區(qū),然后“0.0,忘記問那棟了,不會(huì)又跑回去吧!”尹天佑發(fā)楞道 “你是尹天佑吧?我是你的室友,來我?guī)?..
    魔童兔兔TUT閱讀 339評(píng)論 0 0

友情鏈接更多精彩內(nèi)容