AVLTree树
AVLTree的性质:
1.左子树和右子树的高度之差的绝对值不超过1
2.树中的每个左子树和右子树都是AVL树
3.每个节点都有一个平衡因子(balance factor--bf),任一节点的平衡因子是-1,0,1。(每个节点的平衡因子等于右子树的高度减去左子树的高度
AVLTree的效率:
一棵AVL树有N个节点,其高度可以保持在log2N,插入/删除/查找的时间复杂度也是log2N。(log2N是表示log以2为底N的对数,evernote不支持公式。)
AVLTree的插入操作:
下面是左旋和右旋的解析图
下面是左右旋转和右左旋转解析图
AVLTree树代码如下:
旋转操作代码如下:
<span style="font-weight: bold;">void _RotateL(Node*& parent) //左旋转
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
{
subRL->_parent = parent;
}
subR->_left = parent;
subR->_parent = parent->_parent;
parent->_parent = subR;
parent->_bf = subR->_bf = 0;
parent = subR;
}
void _RotateR(Node*& parent) //右旋转
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
{
subLR->_parent = parent;
}
subL->_right = parent;
subL->_parent = parent->_parent;
parent->_parent = subL;
parent->_bf = subL->_bf = 0;
parent = subL;
}
void _RotateLR(Node*& parent) //先左后右旋转
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
// 左单旋
subL->_right = subLR->_left;
if (subLR->_left)
{
subLR->_left->_parent = subL;
}
subLR->_left = subL;
subLR->_parent = subL->_parent;
subL->_parent = subLR;
if (subLR->_bf == 0 || subLR->_bf == -1)
{
subL->_bf = 0;
}
else // subLR->_bf == 1
{
subL->_bf = -1;
}
// 右单旋
parent->_left = subLR->_right;
if (subLR->_right)
{
subLR->_right->_parent = parent;
}
subLR->_right = parent;
subLR->_parent = parent->_parent;
parent->_parent = subLR;
if (subLR->_bf == 0 || subLR->_bf == 1)
{
parent->_bf = 0;
}
else // subLR->_bf == -1
{
parent->_bf = 1;
}
subLR->_bf = 0;
parent = subLR;
}
void _RotateRL(Node*& parent) //先右后左旋转
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
subR->_left = subRL->_right;
if (subRL->_right)
{
subRL->_right->_parent = subR;
}
subRL->_right = subR;
<span style="font-family:SimSun;">subR->_parent = subRL;</span>
if (subRL->_bf == 0 || subRL->_bf == 1)
{
subR->_bf = 0;
}
else
{
subR->_bf = 1;
}
parent->_right = subRL->_left;
if (subRL->_left)
{
subRL->_left->_parent = parent;
}
</span>subRL->_left = parent;<strong>
subRL->_parent = parent->_parent;
parent->_parent = subRL;
if (subRL->_bf == 0 || subRL->_bf == -1)
{
parent->_bf = 0;
}
else
{
parent->_bf = -1;
}
subRL->_bf = 0;
parent = subRL;
}</strong>
ABLTree树的结构如下:
template<class K, class V>
struct AVLTreeNode
{
AVLTreeNode<K, V>* _left;
AVLTreeNode<K, V>* _right;
AVLTreeNode<K, V>* _parent;
K _key;
V _value;
int _bf; // 平衡因子
AVLTreeNode(const K& key, const V& value)
:_key(key)
, _value(value)
, _left(NULL)
, _right(NULL)
, _parent(NULL)
, _bf(0)
{
}
};
AVLTree树的插入操作
<span style="font-family:SimSun;font-size:14px;"><strong>template<class K, class V>
class AVLTree
{
typedef AVLTreeNode<K, V> Node;
public:
AVLTree()
:_root(NULL)
{}
bool Insert(const K& key, const V& value)
{
//1.空树
if (_root == NULL)
{
_root = new Node(key, value);
return true;
}
//2.查找插入的位置
Node* parent = NULL;
Node* 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;
}
}
cur = new Node(key, value);
if (parent->_key > key)
{
parent->_left = cur;
cur->_parent = parent;
}
else
{
parent->_right = cur;
cur->_parent = parent;
}
bool isRotate = false;
// 3.调整树
while (parent)
{
if (parent->_left == cur)
parent->_bf--;
else
parent->_bf++;
if (parent->_bf == 0)
{
break;
}
else if (parent->_bf == 1 || parent->_bf == -1)
{
cur = parent;
parent = cur->_parent;
}
else // 当平衡因子是 2 或 -2 时
{
isRotate = true;
if (parent->_bf == 2)
{
if (cur->_bf == 1)
{
_RotateL(parent);
}
else
{
_RotateRL(parent);
}
}
else // 平衡因子是-2时
{
if (cur->_bf == -1)
{
_RotateR(parent);
}
else
{
_RotateLR(parent);
}
}
break;
}
}
if (isRotate)
{
Node* ppNode = parent->_parent;
if (ppNode == NULL)
{
_root = parent;
}
else
{
if (ppNode->_key < parent->_key)
{
ppNode->_right = parent;
}
else
{
ppNode->_left = parent;
}
}
}
return true;
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
bool IsBlance()
{
return _IsBlance(_root);
}
protected:
bool _IsBlance(Node* root) //通过平衡因子判断AVLTree树是否正确
{
if (root == NULL)
{
return true;
}
int left = _Height(root->_left);
int right = _Height(root->_right);
int bf = abs(right - left);
if (bf > 1)
{
return false;
}
if (bf != abs(root->_bf))
{
cout << root->_key << ":平衡因子有问题" << endl;
return false;
}
return _IsBlance(root->_left) && _IsBlance(root->_right);
}
int _Height(Node* root) //返回AVLTree树的高度
{
if (root == NULL)
{
return 0;
}
int left = _Height(root->_left) + 1;
int right = _Height(root->_right) + 1;
return left > right ? left : right;
}
void _InOrder(Node* root) //以中序遍历打印AVLTree树
{
if (root == NULL)
{
return;
}
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
protected:
Node* _root;
};</strong></span>
测试代码如下:
<strong><span style="font-size:14px;">void Test()
{
AVLTree<int, int> t1;
int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
for (size_t i = 0; i < sizeof(a) / sizeof(int); ++i)
{
t1.Insert(a[i], i);
}
t1.InOrder();
cout << "IsBlance?" << t1.IsBlance() << endl;
}
int main()
{
Test();
system("pause");
return 0;
}</span></strong>
测试结果如下: