一、AVL 樹的定義
平衡二叉查找樹又稱 AVL 樹(以提出者命名),它要麼是空樹,要麼滿足:左右子樹都是平衡二叉查找樹,且左右子樹深度差的絕對值不超過 1。將節點左子樹深度減去右子樹深度定義為平衡因子,其值只可能是 -1、0 和 1。
二、AVL 樹的插入與刪除
AVL 樹也是二叉查找樹,滿足二叉查找樹性質。它最大的特點是高度平衡——左右子樹高度差不超過 1,能絕對保證樹高為 O(lgn)。與紅黑樹一樣,插入和刪除後平衡性可能被破壞,必須做額外的修復(旋轉)操作。
以下為 C++ 實現,核心是左旋、右旋,以及插入/刪除時的左平衡與右平衡處理:
/* AVL 樹(平衡二叉查找樹) */
#include <iostream>
using namespace std;
//定義 AVL 樹結點
typedef struct AVLNode
{
int key;
int balanceFactor;
AVLNode *left, *right, *parent;
} AVLNode, AVLTree;
/* 左旋:對以 tree 為根、node 為支點的樹左旋 */
void leftRotate(AVLTree* &tree, AVLNode* node)
{
if (! tree || ! node || ! node->right) return;
AVLNode* rightChild = node->right;
node->right = rightChild->left;
if (rightChild->left) rightChild->left->parent = node;
rightChild->parent = node->parent;
if (! node->parent) tree = rightChild;
else if (node == node->parent->left) node->parent->left = rightChild;
else node->parent->right = rightChild;
rightChild->left = node;
node->parent = rightChild;
}
/* 右旋 */
void rightRotate(AVLTree* &tree, AVLNode* node)
{
if (! tree || ! node || ! node->left) return;
AVLNode* leftChild = node->left;
node->left = leftChild->right;
if (leftChild->right) leftChild->right->parent = node;
leftChild->parent = node->parent;
if (! node->parent) tree = leftChild;
else if (node == node->parent->left) node->parent->left = leftChild;
else node->parent->right = leftChild;
leftChild->right = node;
node->parent = leftChild;
}
/* 左平衡處理(插入)依據左孩子平衡因子做單右旋或先左旋再右旋 */
void leftBalanceForInsert(AVLTree* &tree, AVLNode* node)
{
if (! tree || ! node || ! node->left) return;
AVLNode* leftChild = node->left;
switch (leftChild->balanceFactor)
{
case 1: node->balanceFactor = leftChild->balanceFactor = 0; rightRotate(tree, node); break;
case 0: break; // leftChild 即新插入結點,無需處理
case -1:
switch (leftChild->right->balanceFactor)
{
case -1: node->balanceFactor = 0; leftChild->balanceFactor = 1; break;
case 0: node->balanceFactor = leftChild->balanceFactor = 0; break;
case 1: node->balanceFactor = -1; leftChild->balanceFactor = 0; break;
}
leftChild->right->balanceFactor = 0;
leftRotate(tree, node->left);
rightRotate(tree, node);
break;
}
}
/* 右平衡處理(插入)、以及插入/刪除的左右平衡處理,思路與上述對稱 */
void rightBalanceForInsert(AVLTree* &tree, AVLNode* node);
void leftBalanceForDelete(AVLTree* &tree, AVLNode* node);
void rightBalanceForDelete(AVLTree* &tree, AVLNode* node);
/* 插入後向上回溯修正平衡因子,必要時旋轉 */
void avlInsertFixup(AVLTree* &tree, AVLNode* node);
/* 插入節點,類似 BST,插入後調用 fixup 保證性質 */
void avlInsert(AVLTree* &tree, AVLNode* node);
/* 刪除節點,與插入相反對應 */
AVLNode* avlDelete(AVLTree* &tree, AVLNode* node);
其中 avlInsert 先按 BST 規則找到插入位置,再 avlInsertFixup 回溯更新平衡因子並旋轉;avlDelete 先確定實際刪除節點(無子樹或僅一空子樹者,否則用後繼替代),再以 avlDeleteFixup 自底向上修復。avlTreeMinimum 與 avlTreeSuccessor 分別用於求最小節點與中序後繼。
延伸閱讀:二叉樹的常見問題及其解決 https://www.linuxidc.com/Linux/2013-04/83661.htm |【遞歸】二叉樹的先序建立及遍歷 https://www.linuxidc.com/Linux/2012-12/75608.htm |在 JAVA 中實現的二叉樹結構 https://www.linuxidc.com/Linux/2008-12/17690.htm |【非遞歸】二叉樹的建立及遍歷 https://www.linuxidc.com/Linux/2012-12/75607.htm |二叉樹遞歸實現與二重指針 https://www.linuxidc.com/Linux/2013-07/87373.htm |二叉樹先序中序非遞歸算法 https://www.linuxidc.com/Linux/2014-06/102935.htm |輕鬆搞定面試中的二叉樹題目 https://www.linuxidc.com/linux/2014-07/104857.htm