AVL樹
目前創(chuàng)新互聯(lián)公司已為成百上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站改版維護(hù)、企業(yè)網(wǎng)站設(shè)計、黃州網(wǎng)站維護(hù)等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。AVL樹又稱為高度平衡的二叉搜索樹,是1962年有俄羅斯的數(shù)學(xué)家G.M.Adel'son-Vel'skii和E.M.Landis提出來的。它能保持二叉樹的高度平衡,盡量降低二叉樹的高度,減少樹的平均搜索長度。
AVL樹的性質(zhì)
左子樹和右子樹的高度之差的絕對值不超過1
樹中的每個左子樹和右子樹都是AVL樹
每個節(jié)點都有一個平衡因子(balance factor--bf),任一節(jié)點的平衡因子是-1,0,1。(每個節(jié)點的平衡因子等于右子樹的高度減去左子樹的高度 )
AVL樹的效率
一棵AVL樹有N個節(jié)點,其高度可以保持在log2N,插入/刪除/查找的時間復(fù)雜度也是log2N。
(ps:log2N是表示log以2為底N的對數(shù),evernote不支持公式。^^)
這里要注意在插入和刪除時對平衡因子BF的修改
插入
#pragma once
#include
using namespace std;
#include
template< class K, class V>
struct AVLBSTreeNode
{
AVLBSTreeNode
AVLBSTreeNode
AVLBSTreeNode
K _key;
V _value;
int _bf;//平衡因子
AVLBSTreeNode(const K& key, const V& value)
:_left(NULL)
, _right(NULL)
, _parent(NULL)
, _key(key)
, _value(value)
, _bf(0)
{}
};
template
class AVLBSTree
{
typedef AVLBSTreeNode
public:
AVLBSTree()
:_root(NULL)
{}
bool Insert(const K& key, const V& value)//插入
{
if (_root == NULL)
{
_root = new Node(key, value);
return true;
}
Node* cur = _root;
Node* parent = NULL;
while (cur)
{
if (cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else if (cur->_key < key)
{
parent = cur;
cur = cur->_right;
}
else
{
return false;
}
}
cur = new Node(key, value);
if (parent->_key > key)
{
parent->_left = cur;
cur->_parent = parent;
}
else
{
parent->_right = cur;
cur->_parent = parent;
}
//更新平衡因子,不平衡進(jìn)行旋轉(zhuǎn)
while (parent)
{
if (cur == parent->_right)
{
parent->_bf++;
}
else
{
parent->_bf--;
}
if (parent->_bf == 0)//平衡因子為0對這個樹的高度不會產(chǎn)生影響
{
break;
}
else if (parent->_bf == 1 || parent->_bf == -1)
{
cur = parent;
parent = cur->_parent;
}
else
{
if (parent->_bf == -2)
{
if (cur->_bf == -1)
{
RotateR(parent);//右旋
}
else
{
RotateLR(parent);//先左旋再右旋
}
}
else
{
if (cur->_bf == 1)
{
RotateL(parent);//左旋
}
else
{
RotateRL(parent);//先右旋再左旋
}
}
break;
}
}
return true;
}
void Inorder()//中序遍歷
{
_Inorder(_root);
cout << endl;
}
bool IsBalance()//檢查平衡因子
{
return _IsBalance(_root);
}
Node* Find(const K& key)//查找
{
Node* cur = _root;
while (cur)
{
if (cur->_key > key)
{
cur = cur->_left;
}
else if (cur->_key < key)
{
cur = cur->_right;
}
else
{
return cur;
}
}
return NULL;
}
bool Remove(const K& key)//刪除
{
if (_root == NULL)
{
return false;
}
Node* cur = _root;
Node* parent = NULL;
while (cur)
{
if (cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else if (cur->_key < key)
{
parent = cur;
cur = cur->_right;
}
else
{
if (cur->_left == NULL && cur->_right == NULL)
{
if (parent == NULL)
{
_root = NULL;
}
else
{
if (parent->_left == cur)
{
parent->_left = NULL;
parent->_bf++;
}
else
{
parent->_right = NULL;
parent->_bf--;
}
}
delete cur;
}
else if (cur->_left == NULL && cur->_right != NULL)
{
if (parent == NULL)
{
_root = cur->_right;
_root->_bf = 0;
}
else
{
if (parent->_left == cur)
{
parent->_left = cur->_right;
parent->_bf++;
}
else
{
parent->_right = cur->_right;
parent->_bf--;
}
}
delete cur;
}
else if (cur->_right == NULL && cur->_left != NULL)
{
if (parent == NULL)
{
_root = cur->_left;
_root++;
}
else
{
if (parent->_left == cur)
{
parent->_left = cur->_left;
parent->_bf++;
}
else
{
parent->_right = cur->_left;
parent->_bf--;
}
}
delete cur;
}
else
{
Node* parent = cur;
Node* left = cur->_right;
while (left->_left)
{
parent = left;
left = left->_left;
}
cur->_key = left->_key;
cur->_value = left->_value;
if (parent->_left == left)
{
parent->_bf++;
parent->_left = left->_right;
}
else
{
parent->_bf--;
parent->_right = left->_right;
}
delete left;
}
break;
}
}
while (parent)
{
if (parent->_bf == 0)//平衡因子為0對這個樹的高度不會產(chǎn)生影響
{
return true;
}
else if (parent->_bf == 1 || parent->_bf == -1)
{
return true;
}
else
{
if (parent->_bf == -2)
{
if (cur->_bf == -1)
{
RotateR(parent);
}
else
{
RotateLR(parent);
}
}
else
{
if (cur->_bf == 1)
{
RotateL(parent);
}
else
{
RotateRL(parent);
}
}
break;
}
}
}
protected:
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
{
subLR->_parent = parent;
}
Node* ppnode = parent->_parent;
subL->_right = parent;
parent->_parent = subL;
if (ppnode == NULL)
{
_root = subL;
}
else
{
if (ppnode->_left == parent)
{
ppnode->_left = subL;
}
else
{
ppnode->_right = subL;
}
}
subL->_parent = ppnode;
subL->_bf = parent->_bf = 0;
}
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
{
subRL->_parent = parent;
}
Node* ppnode = parent->_parent;
subR->_left = parent;
parent->_parent = subR;
if (ppnode == NULL)
{
_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)//subLRde左邊插入
{
parent->_bf = 1;
subL->_bf = 0;
}
else if (bf == 1)//subLR的右邊插入
{
parent->_bf = 0;
subL->_bf = -1;
}
else//subRL就是插入的元素
{
subL->_bf = parent->_bf = 0;
}
subLR->_bf = 0;
}
void RotateRL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
int bf = subRL->_bf;
RotateR(parent->_right);
RotateL(parent);
if (bf == -1)//subLRde左邊插入
{
parent->_bf = 0;
subR->_bf = 1;
}
else if (bf == 1)//subLR的右邊插入
{
parent->_bf = -1;
subR->_bf = 0;
}
else//subRL就是插入的元素
{
subR->_bf = parent->_bf = 0;
}
subRL->_bf = 0;
}
void _Inorder(Node* root)
{
if (root == NULL)
{
return;
}
_Inorder(root->_left);
cout << root->_key << " ";
_Inorder(root->_right);
}
bool _IsBalance(Node* root)
{
if (root == NULL)
{
return true;
}
int left = _Height(root->_left);
int right = _Height(root->_right);
if ((right - left) != root->_bf || abs(right - left) >= 2)
{
cout << "not balance" << root->_key << endl;
return false;
}
return _IsBalance(root->_left) && _IsBalance(root->_right);
}
int _Height(Node* root)
{
if (root == NULL)
{
return 0;
}
int left = _Height(root->_left);
int right = _Height(root->_right);
if (left > right)
{
return left + 1;
}
else
{
return right + 1;
}
}
protected:
Node* _root;
};
void Test()
{
int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16 ,14};
//int a[] = { 30, 35, 10, 20, 9, 18 };
//int a[] = { 10, 9, 30, 20, 40, 22 };
AVLBSTree
int i = 0;
for (i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
{
t.Insert(a[i], i);
}
t.Remove(15);
t.Inorder();
cout<<"isblance"< //cout << t.Find(50) << endl; } 另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享文章:高度平衡的二叉搜索樹—AVLTree-創(chuàng)新互聯(lián)
分享地址:http://weahome.cn/article/dddgei.html