AVL樹旋轉原理和簡易實現
二叉搜索樹雖然可以提高搜索效率,但是如果插入的數據有序時很有可能變成單支,如果變成單支樹的時候,那麼查找時效率也不高了。因此引入AVL樹。
AVL樹是當向這棵樹插入節點的時候,要保證每個節點的左右子樹的高度差都不超過1,如果超過1時就要對這棵樹的分支進行旋轉。
AVL樹的特性:
- 它的左右子樹都是AVL樹
- 左右子樹高度之差(簡稱平衡因子)的絕對值不超過1(-1/0/1)
這棵樹就是一顆AVL樹,它的每個節點的平衡因子的節點的絕對值都不超過1。
AVL樹節點的定義
template<class K,class V>
struct AVLTreeNode
{
AVLTreeNode<K, V>* _left;//指向左
AVLTreeNode<K, V>* _right;//指向右
AVLTreeNode<K, V>* _parent;//指向父親
std::pair<K, V> _kv;
int _bf;//平衡因子
};
AVL樹的插入
AVL樹的插入需要以下幾步:
- 如果是空樹,那麼直接讓root等於這個新開節點,並把平衡因子設置爲0
- 如果不是空樹,那麼我們就去找插入的位置,比該值小去左邊找,比該值大去右邊找
- 找到之後我們新開一個節點,然後將這個節點連接到這個樹中
- 最後也是最重要的一步:更新平衡因子。
前三步和二叉搜索樹是一樣的,但是因爲AVL樹是一個絕對平衡的樹,因此插入之後需要更新平衡因子,(如果在該節點的左邊插入那麼平衡因子- -,如果在該節點的右邊插入那麼平衡因子+ +)如果說平衡因子的絕對值大於1,那就說明這棵樹的左右高度差已經超過1了,因此需要對這個支進行旋轉。
旋轉又分爲以下幾種情況:
情況一:新節點插入較高左子樹的左側(右單旋)
當插入節點之後,60的平衡因子會變成-2,需要進行調整。如果b存在,那麼讓b變成60的左子樹,讓60變成30的右子樹,30變爲根節點。整個旋轉過程結束後,30的平衡因子變成0,滿足AVL樹的條件。
可以這麼旋轉的原因是,30的右子樹b如果存在則一定比30大,但是比60小,因此這個b一定可以去做60的左子樹。
旋轉方法
void RotateR(Node* parent)//右單旋
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
subLR->_parent = parent;
subL->_right = parent;
Node* ppnode = parent->_parent;
parent->_parent = subL;
if (parent == _root)
{
_root = subL;
}
else
{
if (ppnode->_left == parent)
ppnode->_left = subL;
else
ppnode->_right = subL;
}
subL->_parent = ppnode;
subL->_bf = parent->_bf = 0;
}
情況二: 新節點插入較高右子樹的右側(左單旋)
插入節點後,30的平衡因子變爲2,需要進行旋轉,讓b變爲30的右子樹,讓30變爲60的左子樹,60變爲根節點。整個旋轉過程結束後,60的平衡因子變成0,滿足AVL樹的條件。
可以這麼旋轉的原因是60的左子樹b一定比60小,但是一定比30大,因此可以去做30的右子樹。
旋轉方法
void RotateL(Node* parent)//左單旋
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
subRL->_parent = parent;
subR->_left = parent;
Node* ppnode = parent->_parent;
parent->_parent = subR;
if (parent == _root)
{
_root = subR;
}
else
{
if (ppnode->_left == parent)
ppnode->_left = subR;
else
ppnode->_right = subR;
}
subR->_parent = ppnode;
subR->_bf = parent->_bf = 0;
}
情況三:新節點插入較高左子樹的右側(左右雙旋)
void RotateLR(Node* parent)//左右雙旋
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
int bf = subLR->_bf;
RotateL(parent->_left);
RotateR(parent);
if (bf == 1)
{
subL->_bf = -1;
parent->_bf = 0;
}
else if (bf == -1)
{
subL->_bf = 0;
parent->_bf = 1;
}
else if (bf == 0)
{
subL->_bf = parent->_bf = 0;
}
}
情況四:新節點插入較高右子樹的左側(右左雙旋)
void RotateRL(Node* parent)//右左雙旋
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
int bf = subRL->_bf;
RotateR(parent->_right);
RotateL(parent);
subRL->_bf = 0;
if (bf == 1)
{
subR->_bf = 0;
parent->_bf = -1;
}
else if (bf == -1)
{
parent->_bf = 0;
subR->_bf = 1;
}
else if (bf == 0)
{
subR->_bf = parent->_bf = 0;
}
}
AVL樹的判斷平衡
判斷平衡我們可以採用遞歸的方法,就是去判斷每一個節點的左右子樹是不是平衡的(左右子樹的高度差不超過1)。
既然要求高度差,那麼我們就要寫一個求高度的函數Height
,然後再寫一個IsBalance
判斷平衡,這裏要注意的是必須要遞歸左子樹和右子樹,因爲必須保證每一顆子樹都是平衡的。
int Height(Node* root)//求AVL樹的高度
{
if (root == nullptr)
return 0;
int LeftHeight = Height(root->_left);
int RightHeight = Height(root->_right);
return LeftHeight > RightHeight ? LeftHeight + 1 : RightHeight + 1;
}
bool IsBalance()
{
return _IsBalance(_root);
}
bool _IsBalance(Node *root)//看每一個節點的左右子樹高度差是否不超過1
{
if (root == nullptr)
return true;
int LeftHeight = Height(root->_left);
int RightHeight = Height(root->_right);
//每棵子樹的都應該是平衡的(所以需要遞歸左子樹和右子樹)
return abs(LeftHeight - RightHeight) < 2
&& _IsBalance(root->_left)
&& _IsBalance(root->_right);
}
AVL樹的中序遍歷
void Inorder()
{
_Inorder(_root);
cout << endl;
}
void _Inorder(Node* root)//中序遍歷
{
if (root == nullptr)
return;
_Inorder(root->_left);
cout << root->_kv.first << " ";
_Inorder(root->_right);
}
完整代碼如下
AVLTree.h
#pragma once
#include <iostream>
#include <assert.h>
using std::cout;
using std::endl;
template<class K,class V>
struct AVLTreeNode
{
AVLTreeNode<K, V>* _left;
AVLTreeNode<K, V>* _right;
AVLTreeNode<K, V>* _parent;
std::pair<K, V> _kv;
int _bf;//平衡因子
AVLTreeNode(std::pair<K, V> &kv)//構造函數
:_left(nullptr)
, _right(nullptr)
, _parent(nullptr)
, _kv(kv)
, _bf(0)
{}
};
template<class K,class V>
class AVLTree
{
typedef AVLTreeNode<K,V> Node;
public:
AVLTree()
: _root(nullptr)
{}
bool Insert(std::pair<K, V> &kv)
{
//空樹的情況
if (_root == nullptr)
{
_root = new Node(kv);
_root->_bf = 0;
return 0;
}
//不爲空樹
Node* parent = nullptr;
Node* cur = _root;
while (cur)//找到插入的位置
{
if (cur->_kv.first > kv.first)
{
parent = cur;
cur = cur->_left;
}
else if (cur->_kv.first < kv.first)
{
parent = cur;
cur = cur->_right;
}
else
return false;
}
cur = new Node(kv);//新開一個節點
//連接起來
if (parent->_kv.first > kv.first)
{
parent->_left = cur;
cur->_parent = parent;
}
else
{
parent->_right = cur;
cur->_parent = parent;
}
//更新平衡因子
while (parent)
{
//如果在左邊插入,平衡因子--
if (cur == parent->_left)
{
parent->_bf--;
}
//如果在右邊插入,平衡因子++
else
{
parent->_bf++;
}
//判斷平衡因子
if (parent->_bf == 0)//平衡
{
break;
}
else if (abs(parent->_bf) == 1)//繼續更新,向上走
{
cur = parent;
parent = parent->_parent;
}
else if (abs(parent->_bf) == 2)
{
//旋轉
if (parent->_bf == 2)
{
if (cur->_bf == 1)
{
RotateL(parent);
}
else if (cur->_bf == -1)
{
RotateRL(parent);
}
}
else if (parent->_bf == -2)
{
if (cur->_bf == -1)
{
RotateR(parent);
}
else if (cur->_bf == 1)
{
RotateLR(parent);
}
}
else
{
assert(false);
}
break;
}
else
{
//拋異常
assert(false);
}
}
return true;
}
void RotateL(Node* parent)//左單旋
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
subRL->_parent = parent;
subR->_left = parent;
Node* ppnode = parent->_parent;
parent->_parent = subR;
if (parent == _root)
{
_root = subR;
}
else
{
if (ppnode->_left == parent)
ppnode->_left = subR;
else
ppnode->_right = subR;
}
subR->_parent = ppnode;
subR->_bf = parent->_bf = 0;
}
void RotateR(Node* parent)//右單旋
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
subLR->_parent = parent;
subL->_right = parent;
Node* ppnode = parent->_parent;
parent->_parent = subL;
if (parent == _root)
{
_root = subL;
}
else
{
if (ppnode->_left == parent)
ppnode->_left = subL;
else
ppnode->_right = subL;
}
subL->_parent = ppnode;
subL->_bf = parent->_bf = 0;
}
void RotateLR(Node* parent)//左右雙旋
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
int bf = subLR->_bf;
RotateL(parent->_left);
RotateR(parent);
if (bf == 1)
{
subL->_bf = -1;
parent->_bf = 0;
}
else if (bf == -1)
{
subL->_bf = 0;
parent->_bf = 1;
}
else if (bf == 0)
{
subL->_bf = parent->_bf = 0;
}
}
void RotateRL(Node* parent)//右左雙旋
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
int bf = subRL->_bf;
RotateR(parent->_right);
RotateL(parent);
subRL->_bf = 0;
if (bf == 1)
{
subR->_bf = 0;
parent->_bf = -1;
}
else if (bf == -1)
{
parent->_bf = 0;
subR->_bf = 1;
}
else if (bf == 0)
{
subR->_bf = parent->_bf = 0;
}
}
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 IsBalance()
{
return _IsBalance(_root);
}
bool _IsBalance(Node *root)
{
if (root == nullptr)
return true;
int LeftHeight = Height(root->_left);
int RightHeight = Height(root->_right);
//每棵子樹的都應該是平衡的(所以需要遞歸左子樹和右子樹)
return abs(LeftHeight - RightHeight) < 2
&& _IsBalance(root->_left)
&& _IsBalance(root->_right);
}
void Inorder()
{
_Inorder(_root);
cout << endl;
}
void _Inorder(Node* root)
{
if (root == nullptr)
return;
_Inorder(root->_left);
cout << root->_kv.first << " ";
_Inorder(root->_right);
}
private:
Node* _root;
};
void TestAVLTree()
{
AVLTree<int, int> t;
int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
for (auto e : a)
{
t.Insert(std::make_pair(e, e));
}
t.Inorder();
cout << t.IsBalance() << endl;
}
Test.cpp
#include "AVLTree.h"
int main()
{
TestAVLTree();
system("pause");
return 0;
}