平衡二叉树(AVL),咕咕咕!

1.AVL的介绍
AVL树是最先发明的自平衡⼆叉查找树,AVL是⼀颗空树,或者具备下列性质的二叉搜索树:它的
左右子树都是AVL树,且左右子树的⾼度差的绝对值不超过1。AVL树是⼀颗高度平衡搜索⼆叉树,通过控制高度差去控制平衡。
AVL树实现引入⼀个平衡因⼦(balance factor)的概念,每个结点都有⼀个平衡因子,任何
结点的平衡因子等于右子树的高度减去左子树的⾼度,也就是说任何结点的平衡因子等于0/1/-1,

AVL树并不是必须要平衡因子,但是有了平衡因子可以更方便去进行观察和控制树是否平衡。

AVL树整体结点数量和分布和完全⼆叉树类似,高度可以控制在 ,那么增删查改的效率也可以控制在O(log N)。

2.AVL的结构

cpp 复制代码
template<class K, class V>
struct AVLTreeNode
{
// 需要parent指针,后续更新平衡因⼦可以看到
pair<K, V> _kv;
AVLTreeNode<K, V>* _left;
AVLTreeNode<K, V>* _right;
AVLTreeNode<K, V>* _parent;
int _bf; // balance factor
AVLTreeNode(const pair<K, V>& kv)
:_kv(kv)
, _left(nullptr)
, _right(nullptr)
, _parent(nullptr)
,_bf(0)
{}
};
template<class K, class V>
class AVLTree
{
typedef AVLTreeNode<K, V> Node;
public:
..................
private:
Node* _root = nullptr;
};

3.AVL的插入

1.插⼊⼀个值按⼆叉搜索树规则进⾏插⼊。

  1. 新增结点以后,只会影响祖先结点的⾼度,也就是可能会影响部分祖先结点的平衡因⼦,所以更新 从新增结点->根结点路径上的平衡因⼦,实际中最坏情况下要更新到根,有些情况更新到中间就可以停⽌了,具体情况我们下⾯再详细分析。

  2. 更新平衡因⼦过程中没有出现问题,则插⼊结束

  3. 更新平衡因⼦过程中出现不平衡,对不平衡⼦树旋转,旋转后本质调平衡的同时,本质降低了⼦树的⾼度,不会再影响上⼀层,所以插⼊结束。
    4.平衡因子的更新
    更新原则:
    1.平衡因⼦ = 右⼦树⾼度-左⼦树⾼度
    2.只有⼦树⾼度变化才会影响当前结点平衡因⼦。
    3.插⼊结点,会增加⾼度,所以新增结点在parent的右⼦树,parent的平衡因⼦++,新增结点在
    parent的左⼦树,parent平衡因⼦--
    4.parent所在⼦树的⾼度是否变化决定了是否会继续往上更新
    更新停⽌条件:
    1.更新后parent的平衡因⼦等于0,更新中parent的平衡因⼦变化为-1->0 或者 1->0,说明更新前
    parent⼦树⼀边⾼⼀边低,新增的结点插⼊在低的那边,插⼊后parent所在的⼦树⾼度不变,不会
    影响parent的⽗亲结点的平衡因⼦,更新结束。
    2.更新后parent的平衡因⼦等于1 或 -1,更新前更新中parent的平衡因⼦变化为0->1 或者 0->-1,说 明更新前parent⼦树两边⼀样⾼,新增的插⼊结点后,parent所在的⼦树⼀边⾼⼀边低,parent所 在的⼦树符合平衡要求,但是⾼度增加了1,会影响parent的⽗亲结点的平衡因⼦,所以要继续向 上更新。
    3.更新后parent的平衡因⼦等于2 或 -2,更新前更新中parent的平衡因⼦变化为1->2 或者 -1->-2说
    明更新前parent⼦树⼀边⾼⼀边低,新增的插⼊结点在⾼的那边,parent所在的⼦树⾼的那边更⾼
    了,破坏了平衡,parent所在的⼦树不符合平衡要求,需要旋转处理,旋转的⽬标有两个:1、把
    parent⼦树旋转平衡。2、降低parent⼦树的⾼度,恢复到插⼊结点以前的⾼度。所以旋转后也不
    需要继续往上更新,插⼊结束。
    4.不断更新,更新到根,根的平衡因⼦是1或-1也停⽌了。

cpp 复制代码
插⼊结点及更新平衡因⼦的代码实现
bool Insert(const pair<K, V>& kv)
{
    if (_root == nullptr)
    {
        _root = new Node(kv);
        return true;
    }
    Node* parent = nullptr;
    Node* cur = _root;
    while (cur)
    {
        if (cur->_kv.first < kv.first)
        {
            parent = cur;
            cur = cur->_right;
        }
        else if (cur->_kv.first > kv.first)
        {
            parent = cur;
            cur = cur->_left;
        }
        else
        {
            return false;
        }
    }
    cur = new Node(kv);
    if (parent->_kv.first < kv.first)
    {
        parent->_right = cur;
    }
    else
    {
        parent->_left = cur;
    }
    cur->_parent = parent;
    // 更新平衡因⼦
    while (parent)
    {
    // 更新平衡因⼦
        if (cur == parent->_left)
        parent->_bf--;
        else
        parent->_bf++;
        if (parent->_bf == 0)
        {
            // 更新结束
            break;
        }
        else if (parent->_bf == 1 || parent->_bf == -1)
        {
            // 继续往上更新
            cur = parent;
            parent = parent->_parent;
        }
        else if (parent->_bf == 2 || parent->_bf == -2)
        {
            // 不平衡了,旋转处理
            break;
        }
        else
        {
            assert(false);
        }
    }
return true;
}

5.旋转

  1. 保持搜索树的规则
  2. 让旋转的树从不满⾜变平衡,其次降低旋转树的⾼度
    旋转总共分为四种,左单旋/右单旋/左右双旋/右左双旋
    (1)右单旋
    失衡节点(记为parent)的平衡因子 BF(parent) = 2(左子树比右子树高 2);

其左孩子(记为subL)的平衡因子 BF(subL) = 1(左孩子的左子树更高)
展⽰的是10为根的树,有a/b/c抽象为三棵⾼度为h的⼦树(h>=0),a/b/c均符合AVL树的要
求。10可能是整棵树的根,也可能是⼀个整棵树中局部的⼦树的根。这⾥a/b/c是⾼度为h的⼦树,
是⼀种概括抽象表⽰,他代表了所有右单旋的场景,实际右单旋形态有很多种。
在a⼦树中插⼊⼀个新结点,导致a⼦树的⾼度从h变成h+1,不断向上更新平衡因⼦,导致10的平
衡因⼦从-1变成-2,10为根的树左右⾼度差超过1,违反平衡规则。10为根的树左边太⾼了,需要
往右边旋转,控制两棵树的平衡。
旋转核⼼步骤,因为5 < b⼦树的值 < 10,将b变成10的左⼦树,10变成5的右⼦树,5变成这棵树新的根,符合搜索树的规则,控制了平衡,同时这棵的⾼度恢复到了插⼊之前的h+2,符合旋转原
则。如果插⼊之前10整棵树的⼀个局部⼦树,旋转后不会再影响上⼀层,插⼊结束了。

cpp 复制代码
void RotateR(Node* parent)
{
    Node* subL = parent->_left;
    Node* subLR = subL->_right;
    // 需要注意除了要修改孩⼦指针指向,还是修改⽗亲
    parent->_left = subLR;
    if (subLR)
    subLR->_parent = parent;
    Node* parentParent = parent->_parent;
    subL->_right = parent;
    parent->_parent = subL;
    // parent有可能是整棵树的根,也可能是局部的⼦树
    // 如果是整棵树的根,要修改_root
    // 如果是局部的指针要跟上⼀层链接
    if (parentParent == nullptr)
    {
        _root = subL;
        subL->_parent = nullptr;
    }
    else{
    if (parent == parentParent->_left)
    {
        parentParent->_left = subL;
    }
    else
    {
        parentParent->_right = subL;
    }
        subL->_parent = parentParent;
    }
    parent->_bf = subL->_bf = 0;
}

(2)左单旋

失衡节点(parent)的平衡因子 BF(parent) = -2(右子树比左子树高 2);

其右孩子(记为subR)的平衡因子 BF(subR) = -1(右孩子的右子树更高)。
展⽰的是10为根的树,有a/b/c抽象为三棵⾼度为h的⼦树(h>=0),a/b/c均符合AVL树的要
求。10可能是整棵树的根,也可能是⼀个整棵树中局部的⼦树的根。这⾥a/b/c是⾼度为h的⼦树,
是⼀种概括抽象表⽰,他代表了所有右单旋的场景,实际右单旋形态有很多种,具体跟上⾯左旋类
似。
在a⼦树中插⼊⼀个新结点,导致a⼦树的⾼度从h变成h+1,不断向上更新平衡因⼦,导致10的平
衡因⼦从1变成2,10为根的树左右⾼度差超过1,违反平衡规则。10为根的树右边太⾼了,需要往
左边旋转,控制两棵树的平衡。
旋转核⼼步骤,因为10 < b⼦树的值 < 15,将b变成10的右⼦树,10变成15的左⼦树,15变成这棵
树新的根,符合搜索树的规则,控制了平衡,同时这棵的⾼度恢复到了插⼊之前的h+2,符合旋转
原则。如果插⼊之前10整棵树的⼀个局部⼦树,旋转后不会再影响上⼀层,插⼊结束了。

cpp 复制代码
void RotateL(Node* parent)
{
    Node* subR = parent->_right;
    Node* subRL = subR->_left;
    parent->_right = subRL;
    if(subRL)
    subRL->_parent = parent;
    Node* parentParent = parent->_parent;
    subR->_left = parent;
    parent->_parent = subR;
    if (parentParent == nullptr)
    {
        _root = subR;
        subR->_parent = nullptr;
    }
    else
    {
        if (parent == parentParent->_left)
        {
            parentParent->_left = subR;
        }
        else
        {
            parentParent->_right = subR;
        }
        subR->_parent = parentParent;
    }
    parent->_bf = subR->_bf = 0;
}

(3)左右双旋

失衡节点(parent)的平衡因子 BF(parent) = 2(左子树比右子树高 2);

其左孩子(subL)的平衡因子 BF(subL) = -1(左孩子的右子树更高)。
左边高时,如果插⼊位置不是在a⼦树,⽽是插⼊在b⼦树,b⼦树⾼度从h变成h+1,引发旋转,右单旋⽆法解决问题,右单旋后,我们的树依旧不平衡。右单旋解决的纯粹的左边 高,但是插⼊在b⼦树中,10为跟的⼦树不再是单纯的左边⾼,对于10是左边⾼,但是对于5是右边高 ,需要⽤两次旋转才能解决,以5为旋转点进⾏⼀个左单旋,以10为旋转点进⾏⼀个右单旋, 这棵树就平衡了。
场景1:h >= 1时,新增结点插⼊在e⼦树,e⼦树⾼度从h-1并为h并不断更新8->5->10平衡因⼦,
引发旋转,其中8的平衡因⼦为-1,旋转后8和5平衡因⼦为0,10平衡因⼦为1。
场景2:h >= 1时,新增结点插⼊在f⼦树,f⼦树⾼度从h-1变为h并不断更新8->5->10平衡因⼦,引
发旋转,其中8的平衡因⼦为1,旋转后8和10平衡因⼦为0,5平衡因⼦为-1。
场景3:h == 0时,a/b/c都是空树,b⾃⼰就是⼀个新增结点,不断更新5->10平衡因⼦,引发旋
转,其中8的平衡因⼦为0,旋转后8和10和5平衡因⼦均为0。

cpp 复制代码
void RotateLR(Node* parent)
{
    Node* subL = parent->_left;
    Node* subLR = subL->_right;
    int bf = subLR->_bf;
    RotateL(parent->_left);
    RotateR(parent);
    if (bf == 0)
    {
        subL->_bf = 0;
        subLR->_bf = 0;
        parent->_bf = 0;
    }
    else if (bf == -1)
    {
        subL->_bf = 0;
        subLR->_bf = 0;
    parent->_bf = 1;
    }
    else if(bf == 1)
    {
        subL->_bf = -1;
        subLR->_bf = 0;
        parent->_bf = 0;
    }
    else
    {
        assert(false);
    }
}

(4)右左双旋

场景1:h >= 1时,新增结点插⼊在e⼦树,e⼦树⾼度从h-1变为h并不断更新12->15->10平衡因⼦,引发旋转,其中12的平衡因⼦为-1,旋转后10和12平衡因⼦为0,15平衡因⼦为1。

场景2:h >= 1时,新增结点插⼊在f⼦树,f⼦树⾼度从h-1变为h并不断更新12->15->10平衡因⼦,引发旋转,其中12的平衡因⼦为1,旋转后15和12平衡因⼦为0,10平衡因⼦为-1。

场景3:h == 0时,a/b/c都是空树,b⾃⼰就是⼀个新增结点,不断更新15->10平衡因⼦,引发转,其中12的平衡因⼦为0,旋转后10和12和15平衡因⼦均为0。

cpp 复制代码
void RotateRL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
int bf = subRL->_bf;
RotateR(parent->_right);
RotateL(parent);
if (bf == 0)
{
subR->_bf = 0;
subRL->_bf = 0;
parent->_bf = 0;
}
else if (bf == 1)
{
subR->_bf = 0;
subRL->_bf = 0;
parent->_bf = -1;
}
else if (bf == -1)
{
subR->_bf = 1;
subRL->_bf = 0;
parent->_bf = 0;
}
else
{
assert(false);
}
}

4.AVL的查找

搜索效率为O(log n)

cpp 复制代码
Node* Find(const K& key)
{
Node* cur = _root;
while (cur)
{
if (cur->_kv.first < key)
{
cur = cur->_right;
}
else if (cur->_kv.first > key)
{
cur = cur->_left;
}
else
{
return cur;
}
}
return nullptr;
}

5.AVL的平衡检测

cpp 复制代码
int _Height(Node* root)
{
if (root == nullptr)
return 0;
int leftHeight = _Height(root->_left);
int rightHeight = _Height(root->_right);
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}
bool _IsBalanceTree(Node* root)
{
// 空树也是AVL树
if (nullptr == root)
return true;
// 计算pRoot结点的平衡因⼦:即pRoot左右⼦树的⾼度差
int leftHeight = _Height(root->_left);
int rightHeight = _Height(root->_right);
int diff = rightHeight - leftHeight;
// 如果计算出的平衡因⼦与pRoot的平衡因⼦不相等,或者
// pRoot平衡因⼦的绝对值超过1,则⼀定不是AVL树
if (abs(diff) >= 2)
{
cout << root->_kv.first << "⾼度差异常" << endl;
return false;
}
if (root->_bf != diff)
{
cout << root->_kv.first << "平衡因⼦异常" << endl;
return false;
}
// pRoot的左和右如果都是AVL树,则该树⼀定是AVL树
return _IsBalanceTree(root->_left) && _IsBalanceTree(root->_right);
}
// 测试代码
void TestAVLTree1()
{
AVLTree<int, int> t;
// 常规的测试⽤例
//int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
// 特殊的带有双旋场景的测试⽤例
int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
for (auto e : a)
{
t.Insert({ e, e });
}
t.InOrder();
cout << t.IsBalanceTree() << endl;
}
// 插⼊⼀堆随机值,测试平衡,顺便测试⼀下⾼度和性能等
void TestAVLTree2()
{
const int N = 100000;
vector<int> v;
v.reserve(N);
srand(time(0));
for (size_t i = 0; i < N; i++)
{
v.push_back(rand()+i);
}
size_t begin2 = clock();
AVLTree<int, int> t;
for (auto e : v)
{
t.Insert(make_pair(e, e));
}
size_t end2 = clock();
cout << "Insert:" << end2 - begin2 << endl;
cout << t.IsBalanceTree() << endl;
cout << "Height:" << t.Height() << endl;
cout << "Size:" << t.Size() << endl;
size_t begin1 = clock();
// 确定在的值
/*for (auto e : v)
{
t.Find(e);
}*/
// 随机值
for (size_t i = 0; i < N; i++)
{
t.Find((rand() + i));
}
size_t end1 = clock();
cout << "Find:" << end1 - begin1 << endl;
}

6.总体的代码

cpp 复制代码
#include <iostream>
#include <algorithm> // 用于max函数计算高度
using namespace std;

template<class T>
struct AVLTreeNode
{
    AVLTreeNode(const T& data = T())  
        : _pLeft(nullptr)
        , _pRight(nullptr)
        , _pParent(nullptr)
        , _data(data)
        , _bf(0) // 平衡因子:右子树高度 - 左子树高度
    {}

    AVLTreeNode<T>* _pLeft;
    AVLTreeNode<T>* _pRight;
    AVLTreeNode<T>* _pParent;
    T _data;
    int _bf;   // 节点的平衡因子
};

// AVL: 二叉搜索树 + 平衡因子的限制(bf ∈ {-1,0,1})
template<class T>
class AVLTree  
{
    typedef AVLTreeNode<T> Node;
public:
    AVLTree() : _pRoot(nullptr) {}

    // 在AVL树中插入值为data的节点 
    bool Insert(const T& data)
    {
        // 1. 先按照二叉搜索树的规则插入节点
        if (_pRoot == nullptr) // 空树,直接作为根节点
        {
            _pRoot = new Node(data);
            return true;
        }

        Node* cur = _pRoot;
        Node* parent = nullptr;
        // 找到插入位置
        while (cur != nullptr)
        {
            parent = cur;
            if (data < cur->_data) // 往左走
            {
                cur = cur->_pLeft;
            }
            else if (data > cur->_data) // 往右走
            {
                cur = cur->_pRight;
            }
            else // 重复值,插入失败
            {
                cout << "数据已存在,插入失败!" << endl;
                return false;
            }
        }

        // 创建新节点并挂载到父节点上
        Node* newNode = new Node(data);
        newNode->_pParent = parent;
        if (data < parent->_data)
        {
            parent->_pLeft = newNode;
        }
        else
        {
            parent->_pRight = newNode;
        }

        // 2. 更新平衡因子,并检查是否失衡(bf的绝对值>1)
        cur = newNode; // 从新节点开始往上更新平衡因子
        while (parent != nullptr)
        { 
            // 更新父节点的平衡因子
            if (cur == parent->_pLeft) // 新节点在左,父节点bf--
            {
                parent->_bf--;
            }
            else // 新节点在右,父节点bf++
            {
                parent->_bf++;
            }

            // 平衡因子为0,说明父节点所在子树高度未变,无需继续向上更新
            if (parent->_bf == 0)
            {
                break;
            }
            // 平衡因子为±1,说明高度变化,需要继续向上更新
            else if (abs(parent->_bf) == 1)
            {
                cur = parent;
                parent = parent->_pParent;
            }
            // 平衡因子为±2,说明失衡,需要旋转调整
            else if (abs(parent->_bf) == 2)
            {
                // 根据失衡类型选择旋转方式
                if (parent->_bf == 2) // 右子树高
                {
                    if (cur->_bf == 1) // 右右失衡:左单旋
                    {
                        RotateL(parent);
                    }
                    else // 右左失衡:右左双旋
                    {
                        RotateRL(parent);
                    }
                }
                else // parent->_bf == -2 左子树高
                {
                    if (cur->_bf == -1) // 左左失衡:右单旋
                    {
                        RotateR(parent);
                    }
                    else // 左右失衡:左右双旋
                    {
                        RotateLR(parent);
                    }
                }
                break; // 旋转后整棵树恢复平衡,无需继续向上
            }
        }

        return true;
    }

    // 验证是否是合法的AVL树
    bool IsAVLTree()
    {
        return _IsAVLTree(_pRoot);
    }

    // 中序遍历(验证二叉搜索树的性质:升序)
    void InOrder()
    {
        _InOrder(_pRoot);
        cout << endl;
    }

private:
    // 中序遍历辅助函数
    void _InOrder(Node* pRoot)
    {
        if (pRoot == nullptr)
            return;

        _InOrder(pRoot->_pLeft);
        cout << pRoot->_data << " ";
        _InOrder(pRoot->_pRight);
    }

    // 计算树的高度
    size_t _Height(Node* pRoot)
    {
        if (pRoot == nullptr)
            return 0;

        size_t leftHeight = _Height(pRoot->_pLeft);
        size_t rightHeight = _Height(pRoot->_pRight);
        return max(leftHeight, rightHeight) + 1;
    }

    // 验证AVL树的核心逻辑:
    // 1. 满足二叉搜索树性质
    // 2. 每个节点的平衡因子 = 右子树高度 - 左子树高度
    // 3. 每个节点的平衡因子绝对值 ≤ 1
    bool _IsAVLTree(Node* pRoot)
    {
        if (pRoot == nullptr)
            return true;

        // 计算当前节点的实际平衡因子
        size_t leftHeight = _Height(pRoot->_pLeft);
        size_t rightHeight = _Height(pRoot->_pRight);
        int realBf = rightHeight - leftHeight;

        // 检查平衡因子是否正确且合法
        if (realBf != pRoot->_bf || abs(realBf) > 1)
        {
            cout << "节点" << pRoot->_data << "失衡:实际bf=" << realBf << ",记录bf=" << pRoot->_bf << endl;
            return false;
        }

        // 递归检查左右子树
        return _IsAVLTree(pRoot->_pLeft) && _IsAVLTree(pRoot->_pRight);
    }

    // 右单旋(处理左左失衡)
    //        parent              subL
    //       /     \             /    \
    //      subL   T2    -->    T1   parent
    //     /  \                /    /     \
    //    T1   subLR      insert   subLR   T2
    //    /
    //  insert
   void RotateR(Node* parent)
  {
      Node* subL = parent->_pLeft;
      Node* subLR = subL->_pRight;
      // 需要注意除了要修改孩⼦指针指向,还是修改⽗亲
      parent->_pLeft = subLR;
      if (subLR)
        subLR->_pParent = parent;
      Node* parentParent = parent->_pParent;
      subL->_pRight = parent;
      parent->_pParent = subL;
      // parent有可能是整棵树的根,也可能是局部的⼦树
      // 如果是整棵树的根,要修改_root
      // 如果是局部的指针要跟上⼀层链接
      if (parentParent == nullptr)
      {
        _root = subL;
        subL->_pParent = nullptr;
      }
      else
      {
        if (parent == parentParent->_pLeft)
          {
            parentParent->_pLeft = subL;
          }
        else
          {
            parentParent->_pRight = subL;
          }
        subL->_pParent = parentParent;
    }
      parent->_bf = subL->_bf = 0;
    }


    // 左单旋(处理右右失衡)
    //    parent                cur
    //   /    \               /    \
    //  T1(h) cur     -->    parent T4
    //       /  \           /   \
    //    T2(h)  T4(h+1)   T1   T2
    void RotateL(Node* pParent) 
    {
        Node* pCur = pParent->_pRight;
        Node* pCurLeft = pCur->_pLeft;
        Node* pGrandParent = pParent->_pParent;

        // 1. 处理cur的左子树挂载到parent的右子树
        pParent->_pRight = pCurLeft;
        if (pCurLeft != nullptr)
        {
            pCurLeft->_pParent = pParent;
        }

        // 2. cur成为新的父节点,parent挂载到cur的左子树
        pCur->_pLeft = pParent; 
        pParent->_pParent = pCur;

        // 3. 处理cur与原祖父节点的关系
        if (pGrandParent == nullptr) // 原parent是根节点
        {
            _pRoot = pCur;
            pCur->_pParent = nullptr;
        }
        else
        {
            if (pGrandParent->_pLeft == pParent)
            {
                pGrandParent->_pLeft = pCur;
            }
            else
            {
                pGrandParent->_pRight = pCur;
            }
            pCur->_pParent = pGrandParent;
        }

        // 4. 重置平衡因子
        pParent->_bf = 0;
        pCur->_bf = 0;
    }

    // 左右双旋(先左旋cur,再右旋parent)
    void RotateLR(Node* pParent)
    {
        Node* pCur = pParent->_pLeft;
        Node* pCurRight = pCur->_pRight;
        int bf = pCurRight->_bf; // 记录旋转前curRight的平衡因子,用于后续重置

        // 第一步:对cur做左单旋
        RotateL(pCur);
        // 第二步:对parent做右单旋
        RotateR(pParent);

        // 根据原curRight的平衡因子重置相关节点的bf
        if (bf == 1)
        {
            pParent->_bf = 0;
            pCur->_bf = -1;
            pCurRight->_bf = 0;
        }
        else if (bf == -1)
        {
            pParent->_bf = 1;
            pCur->_bf = 0;
            pCurRight->_bf = 0;
        }
        else // bf == 0
        {
            pParent->_bf = 0;
            pCur->_bf = 0;
            pCurRight->_bf = 0;
        }
    }

    // 右左双旋(先右旋cur,再左旋parent)
    void RotateRL(Node* pParent)
    {
        Node* pCur = pParent->_pRight;
        Node* pCurLeft = pCur->_pLeft;
        int bf = pCurLeft->_bf; // 记录旋转前curLeft的平衡因子

        // 第一步:对cur做右单旋
        RotateR(pCur);
        // 第二步:对parent做左单旋
        RotateL(pParent);

        // 根据原curLeft的平衡因子重置相关节点的bf
        if (bf == 1)
        {
            pParent->_bf = -1;
            pCur->_bf = 0;
            pCurLeft->_bf = 0;
        }
        else if (bf == -1)
        {
            pParent->_bf = 0;
            pCur->_bf = 1;
            pCurLeft->_bf = 0;
        }
        else // bf == 0
        {
            pParent->_bf = 0;
            pCur->_bf = 0;
            pCurLeft->_bf = 0;
        }
    }

private:
    Node* _pRoot;
};

// 测试代码
int main()
{
    AVLTree<int> avl;
    // 插入测试数据,验证平衡
    int arr[] = {16, 3, 7, 11, 9, 26, 18, 14, 15};
    for (auto num : arr)
    {
        avl.Insert(num);
    }

    // 中序遍历(验证二叉搜索树性质)
    cout << "中序遍历结果:";
    avl.InOrder();

    // 验证是否为合法AVL树
    if (avl.IsAVLTree())
    {
        cout << "这是一棵合法的AVL树!" << endl;
    }
    else
    {
        cout << "这不是一棵合法的AVL树!" << endl;
    }

    return 0;
}
 
相关推荐
一叶落4381 小时前
LeetCode 137. 只出现一次的数字 II —— 位运算解法
c语言·数据结构·算法·leetcode·哈希算法
阿豪只会阿巴2 小时前
咱这后续安排
c++·人工智能·算法·leetcode·ros2
像素猎人2 小时前
以数据结构之——树来体会深度优先搜索【dfs】和广度优先搜索【bfs】的妙用:学比特算法课的自用笔记
数据结构·c++·学习·dfs·bfs·深度优先搜索
逆境不可逃2 小时前
LeetCode 热题 100 之 215. 数组中的第K个最大元素 347. 前 K 个高频元素 295. 数据流的中位数
算法·leetcode·职场和发展
凤年徐2 小时前
优选算法——滑动窗口
c++·算法
咏方舟【长江支流】2 小时前
[连载] C++ 零基础入门-4.C++ 键盘输入 cin 一步一步学
c++·c++ 零基础到底层实战·c++ 零基础入门·咏方舟-长江支流
DDzqss2 小时前
3.14打卡day35
算法
橙子也要努力变强2 小时前
进程间通信基础
c++·操作系统
WHS-_-20222 小时前
mCore: Achieving Sub-millisecond Scheduling for 5G MU-MIMO Systems
java·算法·5g