AVL 樹是一棵高度平衡的二叉搜索樹。首先它必須滿足二叉搜索樹的性質:
其中 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 之間:
旋轉的唯一目的,就是降低樹高。由於旋轉至少牽涉三層節點,所以一定要回溯至少一層,才會發現非法的平衡因子。
1. 父節點 bf = 2(父節點右樹比左樹高):
2. 父節點 bf = -2(父節點左樹比右樹高):
// 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,需要旋轉降高:
當 parent 有兩個孩子時,插入 subRL 或 subRR 之前樹必定平衡。插入後 grandfather 的 bf 變為 -2,做法是先以 parent 為軸左旋把 subR 提到原 parent 位置,再以 grandfather 為軸右旋把 subR 提上去,此時以 subR 為根的左右子樹等高,平衡恢復。此情況需依插入的是 subR 的左孩子還是右孩子來調整平衡因子:
// 左右雙旋
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:
parent 有兩個孩子時,插入 subLL 或 subLR 使 grandfather 的 bf 變為 2,做法是先以 parent 為軸右旋把 subL 提到原 parent 位置,再以 grandfather 為軸左旋把 subL 提上去。此時需依插入位置調整:
// 右左雙旋
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 是否歸零,後者則取決於插入方向。把這兩點想通,四種旋轉就只是同一套指標重接動作的排列組合。