AVL树的创建,插入算法

#pragma once

#include<iostream>
using namespace std;//这么写不太合适,头文件中最好不要包括标准命名空间
#include<stack>

template<class K, class V>
struct AVLTreeNode
{
    AVLTreeNode(const K& key, const V& value)
    : _key(key)
    , _value(value)
    , _bf(0)
    , _left(NULL)
    , _right(NULL)
    , _parent(NULL)
    {}

    K _key;
    V _value;
    int _bf;
    AVLTreeNode<K, V>* _left;
    AVLTreeNode<K, V>* _right;
    AVLTreeNode<K, V>* _parent;
};

template<class K, class V>
class AVLTree
{
    typedef AVLTreeNode<K, V> Node;
public:
    AVLTree():_root(NULL){};


    bool Insert(const K& key,const V& value)
    {
        if (_root == NULL)
        {
            _root = new Node(key, value);
            return true;
        }

        Node* parent = _root->_parent;
        Node* pcur = _root;
        while (pcur)
        {
            if (key > pcur->_key)
            {
                parent = pcur;
                pcur = pcur->_right;
            }

            else if (key < pcur->_key)
            {
                parent = pcur;
                pcur = pcur->_left;
            }
            else
                return false;
        }//找到要插入的位置

        if (key < parent->_key)
        {
            parent->_left = new Node(key, value);
            pcur = parent->_left;
        }
        else
        {
            parent->_right = new Node(key, value);
            pcur = parent->_right;
        }
        pcur->_parent = parent;
        pcur->_bf = 0;

        while (parent)
        {
            if (pcur == parent->_left)
            {
                (parent->_bf)--;
            }
            else
            {
                    (parent->_bf)++;
            }

            if (0 == parent->_bf)
                break;
            else if (parent->_bf == 1 || parent->_bf == -1)
            {
                pcur = parent;
                parent = parent->_parent;
            }
            else
            {
                if (parent->_bf == 2)
                {
                    if (pcur->_bf == 1)
                        RotateL(parent);
                    else
                        RotateRL(parent);
                }
                else if (parent->_bf == -2)
                {
                    if (pcur->_bf == -1)
                        RotateR(parent);
                    else
                        RotateLR(parent);
                }
                break;
            }
        }
        return true;
    }

    bool IsBalance()
    {
        return _Isbalance(_root);
    }

    size_t Height()
    {
        return _Height(_root);
    }

    ////void SetBf(Node*& parent, Node*& pcur)
    ////{
    //// while (parent)
    //// {
    //// if (pcur == parent->_left)
    //// {
    //// (parent->_bf)--;
    //// }
    //// else
    //// {
    //// (parent->_bf)++;
    //// }

    //// if (0 == parent->_bf)
    //// break;
    //// else if (parent->_bf == 1 || parent->_bf == -1)
    //// {
    //// pcur = parent;
    //// parent = parent->_parent;
    //// }
    //// else
    //// {
    //// if (parent->_bf == 2)
    //// {
    //// if (pcur->_bf == 1)
    //// RotateL(parent);
    //// else
    //// RotateRL(parent);
    //// }
    //// else if (parent->_bf == -2)
    //// {
    //// if (pcur->_bf == -1)
    //// RotateR(parent);
    //// else
    //// RotateLR(parent);
    //// }
    //// break;
    //// }
    //// }
    ////}
    //// bool Remove(const K& key)
    //// {
    //// if (_root == NULL)
    //// return false;
    //// Node* parent = NULL;
    //// Node* root = _root;
    //// while (root)
    //// {
    //// if (key > root->_key)
    //// {
    //// parent = root;
    //// root = root->_right;
    //// }
    //// else if (key < root->_key)
    //// {
    //// parent = root;
    //// root = root->_left;
    //// }
    //// else
    //// break;
    //// }

    //// if (root == NULL)
    //// return false;//没找到key
    //// Node* del = root;
    //// if (root->_left == NULL)
    //// {
    //// if (root == parent->_left)
    //// {
    //// parent->_left = root->_right;
    //// root->_right->_parent = parent;
    //// parent->_bf++;
    //// SetBf(parent, root);

    //// }
    //// else
    //// {
    //// parent->_right = root->_right;
    //// root->_right->_parent = parent;
    //// parent->_bf--;
    //// SetBf(parent, root);
    //// }
    //// }
    //// else if (root->_right == NULL)
    //// {
    //// if (root == parent->_left)
    //// {
    //// parent->_left = root->_left;
    //// root->_left->_parent = parent;
    //// parent->_bf++;
    //// SetBf(parent, root);

    //// }
    //// else
    //// {
    //// parent->_right = root->_left;
    //// root->_left->_parent = parent;
    //// parent->_bf--;
    //// SetBf(parent, root);
    //// }
    //// }
    //// else
    //// {
    //// ;//找左边最大的或者右边最小的替换删除并更新平衡因子;
    //// }
    //// }

    void InOrder()//非递归写法太没意思了
    {
        if (_root == NULL)
            return;
        stack<Node*> T;
        Node* pcur = _root;
        while (!T.empty()||pcur!=NULL)
        {
            while (pcur)
            {
                T.push(pcur);
                pcur = pcur->_left;
            }
            if (!T.empty())
            {
                pcur = T.top();
                cout << pcur->_key << " ";
                T.pop();
                pcur = pcur->_right;
            }
        }
    }

private:
    bool _Isbalance(Node*root)
    {
        if (NULL == root)
            return true;
        int left = _Height(root->_left);
        int right = _Height(root->_right);
        int diff = left - right;
        if (root->_bf != right - left)
            cout << "平衡因子有问题" << root->_value << endl;
        return diff >= -1 && diff <= 1 && _Isbalance(root->_left) && _Isbalance(root->_right);
    }

    size_t _Height(Node* root)//时间复杂度o(N)
    {
        if (root == NULL)
            return 0;
        size_t left = _Height(root->_left);
        size_t right = _Height(root->_right);
        return left > right ? left + 1 : right+1;
    }

    void RotateR(Node* parent)
    {
        Node* pparent = parent->_parent;
        Node*pcur = parent->_left;
        parent->_left = pcur->_right;
        if (pcur->_right)
            pcur->_right->_parent = parent;
        pcur->_right = parent;
        parent->_parent = pcur;
        pcur->_parent = pparent;
        if (pparent)
        {
            if (parent == pparent->_left)
                pparent->_left = pcur;
            else
                pparent->_right = pcur;
        }
        else
        {
            pcur->_parent = NULL;
            _root = pcur;
        }
        pcur->_bf = parent->_bf = 0;
    }


    void RotateL(Node* parent)
    {
        Node* pparent = parent->_parent;
        Node* pcur = parent->_right;
        parent->_right = pcur->_left;
        if (pcur->_left)
            pcur->_left->_parent = parent;
        pcur->_left = parent;
        parent->_parent = pcur;
        pcur->_parent = pparent;
        if (pparent)
        {
            if (parent == pparent->_left)
                pparent->_left = pcur;
            else
                pparent->_right = pcur;
        }
        else
        {
            pcur->_parent = NULL;
            _root = pcur;
        }
        pcur->_bf = parent->_bf = 0;
    }

    void RotateRL(Node* parent)
    {
        Node* SubR = parent->_right;
        Node* SubRL = SubR->_left;
        int bf = SubRL->_bf;
        RotateR(parent->_right);
        RotateL(parent);
        if (bf == 1)
        {
            SubR->_bf = 0;
            parent->_bf = -1;
        }
        else if (bf == -1)
        {
            SubR->_bf = 1;
            parent->_bf = 0;
        }
        else
        {
            SubR->_bf = parent->_bf = 0;
        }
    }

    void RotateLR(Node* parent)
    {
        Node* SubL = parent->_left;
        Node* SubLR = SubL->_right;
        int bf = SubLR->_bf;
        RotateL(parent->_left);
        RotateR(parent);
        if (bf == 1)
        {
            parent->_bf = 0;
            SubL->_bf = -1;
        }
        else if (bf == -1)
        {
            parent->_bf = 1;
            SubL->_bf = 0;
        }
        else
        {
            SubL->_bf = parent->_bf = 0;
        }
        SubLR->_bf = 0;
    }
protected:
    Node* _root;
};
    原文作者:AVL树
    原文地址: https://blog.csdn.net/o_bvious/article/details/52242295
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞