AVL树

来源:互联网 发布:淘宝网衣服女装 编辑:程序博客网 时间:2024/06/03 21:43

原理网上书上已经很多了,我以自己的理解来实现一遍,全当练习。

1,节点的结构体:

    struct AvlNode    {        int element;        AvlNode* left;        AvlNode* right;        int height;        AvlNode(int &ele, AvlNode *lt, AvlNode *rt, int h = 0) :        element(ele), left(lt), right(rt), height(h){}    };    AvlNode *root;

2,获得子树的高度差,max是获得两者之中最大值,因为是常用,所有专门写了一个函数

    int height(AvlNode*t)    {        return t == nullptr ? -1 : t->height;    }    int max(int lhs, int rhs)    {        return lhs > rhs ? lhs : rhs;    }

3,单旋转左子树:

void roateWithLeftChild(AvlNode*& k2)    {        AvlNode*k1 = k2->left;        k2->left = k1->right;        k1->right = k2;        k2->height = max(height(k2->left), height(k2->right)) + 1;        k1->height = max(height(k1->left), k2->height) + 1;        k2 = k1;    }

4,单旋转右子树:

void roateWithRightChild(AvlNode*&k1)    {        AvlNode *k2 = k1->right;        k1->right = k2->left;        k2->left = k1;        k1->height = max(height(k1->left), height(k1->right)) + 1;        k2->height = max(height(k2->right), k1->height) + 1;        k1 = k2;    }

5,双旋转左子树(递归实现):

    void doubleWithLeftChild(AvlNode*& k3)    {        roateWithRightChild(k3->left);        roateWithLeftChild(k3);    }

6,双旋转右子树:

    void doubleWithRightChild(AvlNode*&k1)    {        roateWithLeftChild(k1->right);        roateWithRightChild(k1);    }

7,平衡:

    static const int ALLOWED_IMBALANCE = 1;    void balance(AvlNode* &t)    {        if (t == nullptr)            return;        if (height(t->left) - height(t->right) > ALLOWED_IMBALANCE)            if (height(t->left->left) >= height(t->left->height))                roateWithLeftChild(t);            else                doubleWithLeftChild(t);        else            if (height(t->right) - height(t->left) > ALLOWED_IMBALANCE)                if (height(t->right->right) >= height(t->right->left))                    roateWithRightChild(t);                else                    doubleWithRightChild(t);        t->height = max(height(t->left), height(t->right)) + 1;    }

8,删除:

    AvlNode *findMin(AvlNode*t)    {        if (t == nullptr)            return nullptr;        if (t->left == nullptr)            return t;        return findMin(t->left);    }    void remove(const int &x, AvlNode *&t)    {        if (t == nullptr)            return;        if (x < t->element)            remove(x, t->left);        else if (t->element < x)            remove(x, t->right);        else if (t->left != nullptr&&t->right != nullptr)        {            t->element = findMin(t->right)->element;            remove(t->element, t->right);        }        else        {            AvlNode *oldNode = t;            t = (t->left != nullptr) ? t->left : t->right;            delete oldNode;        }        balance(t);    }

9,插入:

    void insert(int &x, AvlNode* &t)    {        if (t == nullptr)            t = new AvlNode(x, nullptr, nullptr);        else if (x < t->element)            insert(x, t->left);        else if (x>t->element)            insert(x, t->right);        balance(t);    }
0 0
原创粉丝点击