C++實現(xiàn)AVL樹的基本操作指南
AVL樹的概念
二叉搜索樹雖可以縮短查找的效率,但如果數(shù)據(jù)有序或接近有序二叉搜索樹將退化為單支樹,查找元素相當于在順序表中搜索元素,效率低下。因此,兩位俄羅斯的數(shù)學家G.M.Adelson-Velskii和E.M.Landis在1962年發(fā)明了一種解決上述問題的方法:當向二叉搜索樹中插入新結(jié)點后,如果能保證每個結(jié)點的左右子樹高度之差的絕對值不超過1(需要對樹中的結(jié)點進行調(diào)整),即可降低樹的高度,從而減少平均搜索長度。
一棵AVL樹或者是空樹,或者是具有以下性質(zhì)的二叉搜索樹:
- 它的左右子樹都是AVL樹
- 左右子樹高度之差(簡稱平衡因子)的絕對值不超過1(-1/0/1)
- 平衡因子的計算是右子樹的高度減去左子樹的高度的差值結(jié)果

如果一棵二叉搜索樹是高度平衡的,它就是AVL樹。如果它有n個結(jié)點,其高度可保持在O(log N) ,搜索時間復雜度O( log N)。
AVL樹節(jié)點的定義
template<class K, class V>
struct AVLTreeNode
{
AVLTreeNode<K, V>* _left; //左孩子
AVLTreeNode<K, V>* _right; //右孩子
AVLTreeNode<K, V>* _parent; //父親結(jié)點
pair<K, V> _Kv; //鍵值
int _bf; //平衡因子
//構(gòu)造函數(shù)
AVLTreeNode(const pair<K, V>& Kv)
:_left(nullptr)
,_right(nullptr)
,_parent(nullptr)
,_Kv(Kv)
,_bf(0)
{ }
};
AVL樹的定義
template<class K, class V>
class AVLTree
{
typedef AVLTreeNode<K, V> Node;
public:
AVLTree()
:_root(nullptr)
{}
private:
Node* _root;
};
AVL樹的插入
AVL樹就是在二叉搜索樹的基礎(chǔ)上引入了平衡因子,因此AVL樹也可以看成是二叉搜索樹。那么AVL樹的插入
過程可以分為兩步:
按照二叉搜索樹的方式插入新節(jié)點
與根結(jié)點比較如果比根大就往右子樹插入,如果比根小就往左子樹插入,直到走到合適的位置就插入,由于這里是三叉鏈所以需要處理結(jié)點之間的關(guān)聯(lián)關(guān)系
bool Insert(const pair<K, V> &kv)
{
if (!_root) _root = new Node(kv); //初始根節(jié)點
Node* cur = _root;
Node* parent = _root;
while (cur)
{
K key = cur->_Kv.first;
if (key > kv.first) //比根結(jié)點的key值小,
{
parent = cur;
cur = cur->_left;
}
else if(key < kv.first)//比根結(jié)點的key值大,
{
parent = cur;
cur = cur->_right;
}
else
{
return false; //插入失敗
}
}
//開始插入
cur = new Node(kv);
Node* newNode = cur;
if (parent->_Kv.first > newNode->_Kv.first) //新插入的結(jié)點key值比根節(jié)點小就插入到左子樹
{
parent->_left = newNode;
newNode->_parent = parent;
}
else //新插入的結(jié)點key值比根節(jié)點大就插入到右子樹
{
parent->_right = newNode;
newNode->_parent = parent;
}
}
調(diào)整節(jié)點的平衡因子
當左右子樹的高度發(fā)生了變化,那么就需要對父親及祖先路徑上的所有結(jié)點的平衡因子進行調(diào)整

//更新祖先路徑的所以結(jié)點的平衡因子
/*
總結(jié)五種情況:
1、新增結(jié)點出現(xiàn)在父結(jié)點的左邊,平衡因子減減
2、新增結(jié)點出現(xiàn)在父結(jié)點的右邊,平衡因子加加
3、父親的平衡因子為0就不再調(diào)整
4、父親結(jié)點的平衡因子為1或者-1繼續(xù)調(diào)整
5、父親結(jié)點的平衡因子為2或者-2那就旋轉(zhuǎn)
*/
while (parent)
{
if (parent->_left == cur) parent->_bf--; //1、
if (parent->_right == cur) parent++; //2、
if (parent->_bf == 0) break; //3、
if (parent->_bf == -1 || parent->_bf == 1)//4、
{
cur = parent;
parent = parent->_parent;
}
if (parent->_bf == -2 || parent->_bf == 2) //5、
{
//旋轉(zhuǎn)
if (parent->_bf == -2)
{
if (cur->_bf == -1) RotateR(parent); //左邊高,右單旋
else RotateLR(parent); //左右雙旋
}
else //右 parent->_bf == 2
{
if (cur->_bf == 1) RotateL(parent);//右邊高左單旋轉(zhuǎn)
else RotateRL(parent); //右左雙旋
}
break;
}
}
AVL樹的四種旋轉(zhuǎn)
旋轉(zhuǎn)的原則是遵循搜索樹的規(guī)則,盡量讓兩邊平衡
如果在一棵原本是平衡的AVL樹中插入一個新節(jié)點,可能造成不平衡,此時必須調(diào)整樹的結(jié)構(gòu),使之平衡化。根據(jù)節(jié)點插入位置的不同,AVL樹的旋轉(zhuǎn)分為四種:
右單旋
新節(jié)點插入較高左子樹的左側(cè)—左左:右單旋

不管是哪種單旋都得考慮兩種情況:
1、局部旋轉(zhuǎn),如果parent并不是樹的_root結(jié)點,那么就需要調(diào)整subL和根結(jié)點的關(guān)系
2、獨立旋轉(zhuǎn),parent就是樹的_root結(jié)點,那么subL就是旋轉(zhuǎn)后的根節(jié)點了
3、subLR有可能為null
//右單旋
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR) subLR->_parent = parent; //防止subLR為nullptr
subL->_right = parent;
Node* parent_parent = parent->_p arent; //指針備份
parent->_parent = subL;
if (_root == parent) //如果parent就是樹的根
{
_root = subL; //subL取代parent
_root->_parent = nullptr;
}
else //如果parent并不是樹的根
{
if (parent_parent->_left == parent) parent->_left = subL;
else parent_parent->_right = subL;
subL->_parent = parent_parent; //subL去做parent_parent的孩子
}
//調(diào)節(jié)平衡因子
subL->_bf = parent->_bf = 0;
}
左單旋
新節(jié)點插入較高右子樹的右側(cè)—右右:左單旋

跟右單旋幾乎是一樣的做法
1、局部旋轉(zhuǎn),如果parent并不是樹的_root結(jié)點,那么就需要調(diào)整subL和根結(jié)點的關(guān)系
2、獨立旋轉(zhuǎn),parent就是樹的_root結(jié)點,那么subL就是旋轉(zhuǎn)后的根節(jié)點了
3、subRL有可能為null
//左單旋
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL) subRL->_parent = parent;
subR->_left = parent;
Node* parent_parent = parent->_parent;
parent->_parent = subR;
if (_root == parent)
{
_root = subR;
_root->_parent = nullptr;
}
else
{
if (parent_parent->_left == parent) parent_parent->_left = subR;
else parent_parent->_right = subR;
subR->_parent = parent_parent;
}
subR->_bf = parent->_bf = 0;
}
左右雙旋
新節(jié)點插入較高左子樹的右側(cè)—左右:先左單旋再右單旋
1、新增結(jié)點在b或c都會影響左右子樹的高度,從而引發(fā)雙旋
h > 0情況一:

h > 0,情況二:

h == 0情況三:

//左右旋轉(zhuǎn)
void RotateLR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
int bf = subLR->_bf;
RotateL(parent->_left);
RotateR(parent);
if (bf == -1) //h > 0,新增結(jié)點在b
{
parent->_bf = 1;
subLR->_bf = 0;
subL->_bf = 0;
}
else if (bf == 1) //h > 0,新增結(jié)點在c
{
subL->_bf = -1;
subLR->_bf = 0;
parent->_bf = 0;
}
else if(bf == 0) //h = 0
{
parent->_bf = 0;
subLR->_bf = 0;
subL->_bf = 0;
}
}
右左雙旋
右左雙旋跟左右雙旋的情況基本是類似的,這里就不列舉多種情況了

新節(jié)點插入較高右子樹的左側(cè)—右左:先右單旋再左單旋
//右左旋轉(zhuǎn)
void RotateRL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
int bf = subRL->_bf;
RotateR(parent->_right);
RotateL(parent);
if (bf == -1) //h > 0,新增結(jié)點在b
{
parent->_bf = 0;
subR->_bf = 1;
subRL->_bf = 0;
}
else if (bf == 1) //h > 0,新增結(jié)點在c
{
parent->_bf = -1;
subR->_bf = 0;
subRL->_bf = 0;
}
else if (bf == 0)//h = 0
{
subR->_bf = 0;
subRL->_bf = 0;
parent->_bf = 0;
}
}
查找
Node* Find(const K& key)
{
Node* cur = _root;
while (cur)
{
if (key > cur->_Kv.first) cur = cur->_right; //左子樹
else if (key < cur->_Kv.first) cur = cur->_left; //右子樹
else return cur;
}
}
其他接口
判斷是不是平衡二叉樹
int height(Node* root) //求高度
{
return !root ? 0
: max(height(root->_left),
height(root->_right)) + 1;
}
void _Inorder(Node* root)//中序遍歷
{
if (!root) return;
_Inorder(root->_left);
printf("%d : %d\n",root->_Kv.first, root->_Kv.second);
_Inorder(root->_right);
}
//判斷是不是平衡二叉樹
bool IsAVLTree()
{
return _IsAVLTree(_root);
}
bool _IsAVLTree(Node* root)
{
if (!root) return true;
int left = height(root->_left);
int right = height(root->_right);
//檢查平衡因子
if (right - left != root->_bf)
{
printf("錯誤的平衡因子 %d :%d\n", root->_Kv.first, root->_Kv.second);
return false;
}
return (abs(right - left) < 2)
&& _IsAVLTree(root->_left)
&& _IsAVLTree(root->_right);
}
析構(gòu)函數(shù)
//析構(gòu)函數(shù)
~AVLTree()
{
Destroy(_root);
_root = nullptr;
}
void Destroy(Node *root)//后序銷毀結(jié)點
{
if (!root) return;
Destroy(root->_left);
Destroy(root->_right);
delete root;
}
拷貝構(gòu)造
Node* copy(Node* cp)
{
if (!cp) return nullptr;
Node* newnode = new Node(cp->_Kv);
newnode->_left = copy(cp->_left);
newnode->_right = copy(cp->_right);
return newnode;
}
//拷貝構(gòu)造
AVLTree(const AVLTree<K, V>& job)
{
if(&job != this)
_root = copy(job._root);
}
拷貝賦值
void operator=(AVLTree<K, V> tmp)
{
if (&tmp != this)
swap(tmp._root, this->_root);
}
重載operator[ ]
V& operator[](const K& key)
{
return (Insert(make_pair(key, V())).first)->_Kv.second;
}
AVL樹的完整實現(xiàn)代碼博主已經(jīng)放在 git.
總結(jié)
到此這篇關(guān)于C++實現(xiàn)AVL樹的文章就介紹到這了,更多相關(guān)C++實現(xiàn)AVL樹內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言中 int main(int argc,char *argv[])的兩個參數(shù)詳解
這篇文章主要介紹了C語言中 int main(int argc,char *argv[])的兩個參數(shù)詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
C++靜態(tài)成員函數(shù)不能調(diào)用非靜態(tài)成員變量(詳解)
下面小編就為大家?guī)硪黄狢++靜態(tài)成員函數(shù)不能調(diào)用非靜態(tài)成員變量(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
c語言中的局部跳轉(zhuǎn)及全局跳轉(zhuǎn)功能
本文介紹了C語言中的goto語句,以及如何使用setjmp和longjmp實現(xiàn)跨函數(shù)的跳轉(zhuǎn),詳細講解了setjmp和longjmp的使用方法和注意事項,以及使用這種全局跳轉(zhuǎn)后變量狀態(tài)的不確定性,感興趣的朋友一起看看吧2024-09-09

