Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

AVL 樹:平衡因子、四種旋轉與 C++ 完整實作

AVL 樹是一棵高度平衡的二叉搜索樹。首先它必須滿足二叉搜索樹的性質:

  • 若左子樹不為空,左子樹上所有節點的值都小於根節點;
  • 若右子樹不為空,右子樹上所有節點的值都大於根節點;
  • 左右子樹本身也各自是二叉搜索樹。

AVL 樹的三條性質

  1. 任一節點的左右子樹高度差(平衡因子)的絕對值不超過 1。
  2. 每個節點的左右子樹都是 AVL 樹。
  3. 每個節點都有一個平衡因子 bf,取值只能是 -1、0、1。

其中 bf = 右子樹高度 − 左子樹高度。

節點定義

// AVL樹的節點類
template<class K, class V>
class AVLTreeNode
{
    K _key;
    V _value;
    int _bf;                            // 平衡因子 -1,0,1(右樹高減左樹高)
    AVLTreeNode<K, V>* _parent;         // 指向父節點
    AVLTreeNode<K, V>* _left;           // 指向左孩子
    AVLTreeNode<K, V>* _right;          // 指向右孩子

    AVLTreeNode(const K& key = K(), const V& value = V())
        : _key(key)
        , _value(value)
        , _bf(0)
        , _parent(NULL)
        , _left(NULL)
        , _right(NULL)
    {}
};

插入:更新平衡因子並回溯

插入新節點後,父節點的平衡因子必然改變。首先判斷 parent->_bf 是否仍落在 -1 到 1 之間:

  • 若父節點 bf 為 0,表示該子樹已平衡,不會再影響祖先,回溯結束。
  • 若父節點 bf 為 1 或 -1,需再往上一層,把父節點當成當前節點,繼續檢查祖父節點。
  • 若父節點 bf 為 2 或 -2,已違反性質 1,必須旋轉以降低高度。

旋轉的唯一目的,就是降低樹高。由於旋轉至少牽涉三層節點,所以一定要回溯至少一層,才會發現非法的平衡因子。

需要旋轉的四種情形

1. 父節點 bf = 2(父節點右樹比左樹高):

  • 當前節點 bf = 1,形如「\」,進行左旋;
  • 當前節點 bf = -1,形如「>」,進行右左雙旋。

2. 父節點 bf = -2(父節點左樹比右樹高):

  • 當前節點 bf = -1,形如「/」,進行右旋;
  • 當前節點 bf = 1,形如「<」,進行左右雙旋。

插入主流程

// AVLTree插入算法
template<class K, class V>
bool AVLTree<K, V>::Insert(const K& key, const V& value)
{
    // 1. 空樹
    if (_root == NULL)
    {
        _root = new AVLTreeNode<K, V>(key, value);
        return true;
    }

    // 2. 找插入位置
    AVLTreeNode<K, V>* parent = NULL;
    AVLTreeNode<K, V>* cur = _root;
    while (cur)
    {
        if (cur->_key < key)      { parent = cur; cur = cur->_right; }
        else if (cur->_key > key) { parent = cur; cur = cur->_left;  }
        else                      return false;   // 已存在
    }

    // 3. 插入新節點
    cur = new AVLTreeNode<K, V>(key, value);
    cur->_parent = parent;
    if (parent->_key > key) parent->_left = cur;
    else                    parent->_right = cur;

    // 4. 回溯更新平衡因子
    while (parent)
    {
        if (cur == parent->_left)  parent->_bf--;
        else if (cur == parent->_right) parent->_bf++;

        if (parent->_bf == 0)
            break;                              // 已平衡,結束
        else if (parent->_bf == -1 || parent->_bf == 1)
        {                                       // 繼續往上回溯
            cur = parent;
            parent = cur->_parent;
        }
        else                                    // 2 或 -2,需要旋轉
        {
            if (parent->_bf == 2)
            {
                if (cur->_bf == 1)  _RotateL(parent);
                else                _RotateRL(parent);
            }
            else if (parent->_bf == -2)
            {
                if (cur->_bf == -1) _RotateR(parent);
                else                _RotateLR(parent);
            }
            break;
        }
    }
    return true;
}

左旋

左旋有兩種典型場景:parent 有兩個孩子時,插入 c 之前樹是平衡的,插入後回溯到祖父節點發現 bf = 2,便以該節點為軸左旋;parent 只有一個孩子時,插入 a 後 parent 的 bf 變成 2,同樣以 parent 為軸左旋。

// 左旋
template<class K, class V>
void AVLTree<K, V>::_RotateL(AVLTreeNode<K, V>*& parent)
{
    AVLTreeNode<K, V>* subR  = parent->_right;
    AVLTreeNode<K, V>* subRL = subR->_left;
    AVLTreeNode<K, V>* ppNode = parent->_parent;   // 標記祖先節點

    // 1. 重接 parent 子樹
    parent->_right = subRL;
    if (subRL) subRL->_parent = parent;

    // 2. 重接 subR 子樹
    subR->_left = parent;
    parent->_parent = subR;

    // 3. 把祖先節點接到 subR 上
    subR->_parent = ppNode;
    if (ppNode == NULL)                     // 祖先為空,subR 成為新的根
    {
        _root = subR;
    }
    else
    {
        if (parent == ppNode->_left) ppNode->_left = subR;
        else                         ppNode->_right = subR;
    }

    // 4. 重置平衡因子
    parent->_bf = 0;
    subR->_bf = 0;

    // 5. 更新當前父節點
    parent = subR;
}

右旋

右旋同樣有兩種場景:parent 同時有左右孩子時,插入 c 後 parent 的 bf 變為 -2,以 parent 為軸右旋;parent 只有一個孩子時,插入 a 後 subL 與 parent 的平衡因子都被改變,仍需以 parent 為軸右旋。

// 右旋
template<class K, class V>
void AVLTree<K, V>::_RotateR(AVLTreeNode<K, V>*& parent)
{
    AVLTreeNode<K, V>* subL  = parent->_left;
    AVLTreeNode<K, V>* subLR = subL->_right;
    AVLTreeNode<K, V>* ppNode = parent->_parent;

    // 1. 重接 parent 子樹
    parent->_left = subLR;
    if (subLR) subLR->_parent = parent;

    // 2. 重接 subL 子樹
    subL->_right = parent;
    parent->_parent = subL;

    // 3. 把祖先節點接到 subL 上
    if (ppNode == NULL)                     // subL 成為新的根
    {
        subL->_parent = NULL;
        _root = subL;
    }
    else
    {
        subL->_parent = ppNode;
        if (ppNode->_left == parent)  ppNode->_left = subL;
        else if (ppNode->_right == parent) ppNode->_right = subL;
    }

    // 4. 重置平衡因子
    parent->_bf = 0;
    subL->_bf = 0;

    // 5. 更新當前父節點
    parent = subL;
}

左右雙旋

當 parent 只有一個孩子時,插入 subLR 之前 AVL 樹是平衡的(左右子樹高度差不超過 1)。插入 subLR 後祖父節點 bf 變為 -2,需要旋轉降高:

  • 先以 subL 為軸左旋,把 subLR 往上提,讓 grandfather、parent、subL 落在同一條直線上;
  • 再以 parent 為軸右旋,把 subLR 提上來,左樹高度降 1,grandfather 的 bf 加 1 後變為 -1,恢復平衡;
  • 雙旋完成後把 parent 與 subL 的 bf 都設為 0。

當 parent 有兩個孩子時,插入 subRL 或 subRR 之前樹必定平衡。插入後 grandfather 的 bf 變為 -2,做法是先以 parent 為軸左旋把 subR 提到原 parent 位置,再以 grandfather 為軸右旋把 subR 提上去,此時以 subR 為根的左右子樹等高,平衡恢復。此情況需依插入的是 subR 的左孩子還是右孩子來調整平衡因子:

  • subR 的 bf 為 1(有 subRR 無 subRL):雙旋後 grandfather 的 bf 設為 0,parent 設為 -1;
  • subR 的 bf 為 -1(有 subRL 無 subRR):雙旋後 grandfather 的 bf 設為 1,parent 設為 0。
// 左右雙旋
template<class K, class V>
void AVLTree<K, V>::_RotateLR(AVLTreeNode<K, V>*& parent)
{
    AVLTreeNode<K, V>* pNode = parent;
    AVLTreeNode<K, V>* subL  = parent->_left;
    AVLTreeNode<K, V>* subLR = subL->_right;
    int bf = subLR->_bf;

    _RotateL(parent->_left);
    _RotateR(parent);

    if (bf == 1)       { pNode->_bf = 0;  subL->_bf = -1; }
    else if (bf == -1) { pNode->_bf = 1;  subL->_bf = 0;  }
    else               { pNode->_bf = 0;  subL->_bf = 0;  }
}

右左雙旋

parent 只有一個孩子時,subRL 的插入使 parent 的 bf 變為 2:

  • 先以 subR 為軸右旋,讓 parent、subR、subRL 共線;
  • 再以 parent 為軸左旋,把 subRL 提上去,達到降高的目的;
  • 雙旋後 parent 與 subR 的 bf 都設為 0。

parent 有兩個孩子時,插入 subLL 或 subLR 使 grandfather 的 bf 變為 2,做法是先以 parent 為軸右旋把 subL 提到原 parent 位置,再以 grandfather 為軸左旋把 subL 提上去。此時需依插入位置調整:

  • subL 的 bf 為 1(有 subLR 無 subLL):雙旋後 grandfather 的 bf 設為 -1,parent 設為 0;
  • subL 的 bf 為 -1(有 subLL 無 subLR):雙旋後 grandfather 的 bf 設為 0,parent 設為 1。
// 右左雙旋
template<class K, class V>
void AVLTree<K, V>::_RotateRL(AVLTreeNode<K, V>*& parent)
{
    AVLTreeNode<K, V>* pNode = parent;
    AVLTreeNode<K, V>* subR  = parent->_right;
    AVLTreeNode<K, V>* subRL = subR->_left;
    int bf = subRL->_bf;

    _RotateR(parent->_right);
    _RotateL(parent);

    if (bf == 1)       { pNode->_bf = 0;  subR->_bf = -1; }
    else if (bf == -1) { pNode->_bf = 1;  subR->_bf = 0;  }
    else               { pNode->_bf = 0;  subR->_bf = 0;  }
}

重點回顧

AVL 樹的實作難點不在旋轉本身,而在「回溯何時停止」以及「雙旋後平衡因子怎麼設」。前者取決於父節點 bf 是否歸零,後者則取決於插入方向。把這兩點想通,四種旋轉就只是同一套指標重接動作的排列組合。

Copyright © Linux教程網 All Rights Reserved