Root of AVL Tree (25)

果然我还是太弱了。。。这道题花了我大约5个钟头,看了各种资料,来实现一颗自平衡的搜索树

推荐大家看这个,如何一步一步写平衡二叉树

http://www.cppblog.com/cxiaojia/archive/2013/07/22/187776.html

平衡二叉树的难点在于插入以后,如何调整,如何计算树的高度等等等等

而递归用来解决这种问题是很简单的(一点也不简单)

#include <iostream>
using namespace std;
typedef struct treeNode{
    int data;
    struct treeNode* leftSon;
    struct treeNode* rightSon;
    int height;
}treeNode;

int max(int a, int b)
{
    if (a > b){
        return a;
    }
    else{
        return b;
    }
}
int GetHeight(treeNode *root)
{
    if (root == NULL){
        return 0;
    }
    else return root->height;
}
treeNode* RotatingL(treeNode *root)
{
    treeNode* K2 = root;
    treeNode* K1 = root->leftSon;
    K2->leftSon = K1->rightSon;
    K1->rightSon = K2;
    K2->height = max(GetHeight(K2->leftSon), GetHeight(K2->rightSon)) + 1;
    K1->height = max(GetHeight(K1->leftSon), GetHeight(K1->rightSon)) + 1;
    return K1;
}
treeNode* RotatingR(treeNode *root)
{
    treeNode* K2 = root;
    treeNode* K1 = root->rightSon;
    K2->rightSon = K1->leftSon;
    K1->leftSon = K2;
    K2->height = max(GetHeight(K2->leftSon), GetHeight(K2->rightSon)) + 1;
    K1->height = max(GetHeight(K1->leftSon), GetHeight(K1->rightSon)) + 1;
    return K1;
}
treeNode* RotatingLR(treeNode *root)
{
    root->leftSon = RotatingR(root->leftSon);
    return RotatingL(root);
}
treeNode* RotatingRL(treeNode *root)
{
    root->rightSon = RotatingL(root->rightSon);
    return RotatingR(root);
}
treeNode* insert(treeNode* root, int value)
{
    if (root == NULL){
        root = (treeNode *)malloc(sizeof(treeNode));
        root->data = value;
        root->leftSon = root->rightSon = NULL;
    }
    else if (value < root->data){
        root->leftSon = insert(root->leftSon, value);
        if (GetHeight(root->leftSon) - GetHeight(root->rightSon) == 2){
            if (value < root->leftSon->data){
                root = RotatingL(root);
            }
            else{
                root = RotatingLR(root);
            }
        }
    }
    else {
        root->rightSon = insert(root->rightSon, value);
        if (GetHeight(root->rightSon) - GetHeight(root->leftSon) == 2){
            if (value > root->rightSon->data){
                root = RotatingR(root);
            }
            else{
                root = RotatingRL(root);
            }
        }
    }

    root->height = max(GetHeight(root->leftSon), GetHeight(root->rightSon)) + 1;
    return root;
}

int main()
{
    int n;
    cin >> n;
    treeNode* root = NULL;
    for (int i = 0; i < n; i++){
        int t;
        cin >> t;
        root = insert(root, t);
    }

    cout << root->data;
}
时间: 2024-10-19 16:00:05

Root of AVL Tree (25)的相关文章

04-树4. Root of AVL Tree (25)

04-树4. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 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 tim

1066. Root of AVL Tree (25)【AVL树】——PAT (Advanced Level) Practise

题目信息 1066. Root of AVL Tree (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B 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 tha

04-1. Root of AVL Tree (25)

04-1. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 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

pat1066. Root of AVL Tree (25)

1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 tim

pat04-树4. Root of AVL Tree (25)

04-树4. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 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 tim

PAT 甲级 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

A1066. 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 illust

1066. Root of AVL Tree (25)

时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 o

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 illust

A1066 Root of AVL Tree (25分)

一.技术总结 这是一个平衡二叉树AVL树,就是一个二叉查找树,但是平衡因子不能够超过1. 这个树的数据结构比一般的要多一个height的参数,用于计算平衡因子,就是用当前结点的左子树的height减去右子树的height. 对于node* newNode(int data)函数,首先要新建一个结点,然后初始化height参数为1,然后左右子树结点赋值为NULL. getHeight()函数和updateHeight()函数,后者就是根据当前结点的左右子树结点高度的最大值,然后加一. getBF(