AVL树
前面几个博客一直都是针对二叉树的基本操作和概念,今天我们是时候上一个硬菜了,AVL树是有难度的,但是当你掌握你它带给你的不仅仅是掌握它
的算法,他这里还有红黑树的前身,还有它的旋转操作这是一个很经典的算法,所以我们有理由仔仔细细的掌握它。 好的开始!!!
AVL树是根据它的发明者G.M. Adelson-Velsky和E.M. Landis命名的。它是最先发明的自平衡二叉查找树,也被称为高度平衡树。相比于"二叉查找
树",它的特点是:AVL树中任何节点的两个子树的高度最大差别为1。然而AVL树有什么用 ? 它的好处是什么? 答案是两个字 效率
上面的两张图片,左边的是AVL树,它的任何节点的两个子树的高度差别都<=1;而右边的不是AVL树,因为7的两颗子树的高度相差为2(以2为根节点的
树的高度是3,而以8为根节点的树的高度是1)。
AVL树的查找、插入和删除在平均和最坏情况下都是O(logn)。AVL树的效率就是高在这个地方。如果在AVL树中插入或删除节点后,使得高度之差大于
1。此时,AVL树的平衡状态就被破坏,它就不再是一棵二叉树;为了让它重新维持在一个平衡状态,就需要对其进行旋转处理。学AVL树,重点的地方
也就是它的旋转算法; 现在我们先看看它的每一个节点是什么样?
AVL树的结点:
template<class K>
struct AVLTreeNode
{
K _key;
AVLTreeNode<K>* _left;
AVLTreeNode<K>* _right;
AVLTreeNode<K>* _parent;
int _bf;
AVLTreeNode(const K& key)
:_left(NULL)
, _right(NULL)
, _parent(NULL)
, _key(key)
, _bf(0)
{}
};
首先这里的_key就是我们结点里面存储的数据, 这里着重的说明 _bf 它就是AVL树的关键也是最不好掌控的的东西->平衡因子
平衡因子有什么用?
可以说平衡因子控制着AVL的平衡,每一个结点的平衡因子的值就是该节点 右子树的高度减去左子树的高度
。由于AVL树的概念:
AVL树中任何节点的两
个子树的高度
最大差别为1。 所以平衡因子正常的值只有 0,-1,1 当你的平衡因子不等
于这三个数字时,就要开始进行AVL树的旋转调整了,这个一会
会介绍到。 我们接下来看 平衡因子如何生成。
平衡因子的生成
树的高度:
size_t _Depth(Node* root)
{
if (root == NULL)
{
return 0;
}
else
{
size_t i = _Depth(root->_left);
size_t j = _Depth(root->_right);
if (i > j)
{
return i + 1;
}
else
{
return j + 1;
}
}
}
生成平衡因子:
void _calcBalance(Node* root)
{
calcBalance(_root);
}
void calcBalance(Node* root)
{
if (root == NULL)
return;
root->_bf = _Depth(root->_right) - _Depth(root->_left);
calcBalance(root->_left);
calcBalance(root->_right);
}
平衡因子的值无非就是该节点右子树高度减去左子树的高度,这里使用递归可以很轻易解决这个问题。
AVL树的查找
Node* Find(const K& key)
{
if (_root == NULL)
{
return NULL;
}
Node* cur = _root;
while (cur)
{
if (cur->_key < key)
{
cur = cur->_right;
}
else if (cur->_key > key)
{
cur = cur->_left;
}
else
{
return cur;
}
}
return NULL;
}
这里这里我们就可以看出来AVL树和平常的二叉树的查找的区别和效率.
AVL树的插入
等了这么久硬菜来了,我们首先思考一个问题刚刚我说过当平衡因子超出了它的范围后,AVL会做出对自己进行调整,可能大家都知
道会进行旋转,但
是旋转是发生了什么呢? 这里的旋转会出现4种情况,分别是左单旋,右单旋,左右双旋和右左双旋,接下来我
分别介绍他们。
左单旋:
当圆形结点的平衡因子为2. 触发AVL调整机制,发生左单旋,在图中我们可以看到整个过程。
这里发生了什么情况我们一目了然:
把subRL给parent的右,然后让subR取代parent的位置,最后让parent为subR的左。
当旋转发生过后,我们发现高度降下来了,平衡因子也回到自己的范围里。
这个就是左旋的具体操作是不是很简单~
void _RotateL(Node*& parent)
{
Node* subR = parent->_right;
Node* subRL = NULL;
if (subR)
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;
_root->_parent = NULL;
}
else
{
if (ppNode->_left == parent)
ppNode->_left = subR;
else
ppNode->_right = subR;
subR->_parent = ppNode;
}
}
右单旋:
注意:这里的圆形结点是主要的结点,方形结点只是辅助结点可有可无,可有可无,可有可无!! 这里只是帮助理解。
当圆形结点的平衡因子为-2. 触发AVL调整机制,发生右单旋,在图中我们可以看到整个过程。
这里发生了什么情况我们一目了然:
把subLR给parent的左,然后让subL取代parent的位置,最后让parent为subL的右。
当旋转发生过后,我们发现高度降下来了,平衡因子也回到自己的范围里。
void _RotateR(Node*& parent)
{
Node* subL = parent->_left;
Node* subLR = NULL;
if (subL)
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;
_root->_parent = NULL;
}
else
{
if (ppNode->_left == parent)
ppNode->_left = subL;
else
ppNode->_right = subL;
subL->_parent = ppNode;
}
}
左右双旋:
注意:这里的圆形结点是主要的结点,方形结点只是辅助结点可有可无,可有可无,可有可无!! 这里只是帮助理解。
我们可以发现其实所谓的双旋其实就是两次单旋的先后使用.
我们先对parent进行右单旋,让这个子树满足左单旋的条件,再然后调用左单旋进行调节高度。
具体过程就是上面两种情况的先后执行.
void _RotateLR(Node*& parent)
{
_RotateR(parent->_right);
_RotateL(parent);
}
右左双旋:
注意:这里的圆形结点是主要的结点,方形结点只是辅助结点可有可无,可有可无,可有可无!! 这里只是帮助理解。
我们可以发现其实所谓的双旋其实就是两次单旋的先后使用.
我们先对parent进行左单旋,让这个子树满足右单旋的条件,再然后调用右单旋进行调节
高度。
具体过程就是上面两种情况的先手执行.
void _RotateRL(Node*& parent)
{
_RotateL(parent->_left);
_RotateR(parent);
}
总结:其实无论是哪种旋,他们的目的都是降低树的高度,让平衡因子得以平衡,所以我们在每次插入后都要调节平衡因子,在
旋转后也要调节平衡因
子.既然明白了原理,那么我们来看看代码:
bool Insert(const K& key)
{
//1.空树
if (_root == NULL)
{
_root = new Node(key);
}
//2.查找位置
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;
}
//3.插入节点
if (parent->_key > key)
{
parent->_left = new Node(key);
parent->_left->_parent = parent;
}
else
{
parent->_right = new Node(key);
parent->_right->_parent = parent;
}
_calcBalance(_root);
Node* ppNode = parent->_parent;
if (ppNode && abs(ppNode->_bf) > 1)
{
if (ppNode->_bf < -1)
{
if (parent->_left)
_RotateR(ppNode);
else
_RotateRL(ppNode);
}
if (ppNode->_bf > 1)
{
if (parent->_right)
_RotateL(ppNode);
else
_RotateLR(ppNode);
}
}
_calcBalance(ppNode);
return true;
}
AVL树的删除:
暂时还没有搞定,以后会补充。
判断一个树是否为AVL树:
我们成功的写完代码,如何判断我们到底写的对不对呢? 当结点树目只有几个的时候,我们直接调出来监视窗口看看
就可以完成,
但是结点是几百个
呢? 这时候我们需要一个函数来判断这个AVL树到底有没有问题.
bool IsBIanceTree(Node* root)
{
if (root == NULL)
return true;
size_t left = _Depth(root->_left);
size_t right = _Depth(root->_right);
int mid = right - left;
if (abs(mid) > 2)
{
cout << "这里有问题:" << root->_key << endl;
}
return abs(mid) < 2
&& IsBIanceTree(root->_left)
&& IsBIanceTree(root->_right);
}
AVL树的遍历:
void _Inreder(Node* root)
{
if (root == NULL)
return;
_Inreder(root->_left);
cout << root->_key << " ";
_Inreder(root->_right);
}
最后最后我测试一下:
这里我选择插入这几个数据:{ 2, 4, 3, 1, 0, 6, 5 };
我们来看看最终运行结果:
实现代码:
#pragma once
#include<iostream>
#include<Windows.h>
#include<math.h>
using namespace std;
template<class K, class V>
struct AVLTreeNode
{
K _key;
V _value;
AVLTreeNode<K, V>* _left;
AVLTreeNode<K, V>* _right;
AVLTreeNode<K, V>* _parent;
int _bf;
AVLTreeNode(const K& key)
:_left(NULL)
, _right(NULL)
, _parent(NULL)
, _key(key)
, _value(0)
, _bf(0)
{}
};
template<class K,class V>
class AVLTree
{
typedef AVLTreeNode<K, V> Node;
public:
AVLTree()
:_root(NULL)
{}
~AVLTree()
{}
public:
bool Insert(const K& key)
{
//1.空树
if (_root == NULL)
{
_root = new Node(key);
}
//2.查找位置
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;
}
//3.插入节点
if (parent->_key > key)
{
parent->_left = new Node(key);
parent->_left->_parent = parent;
}
else
{
parent->_right = new Node(key);
parent->_right->_parent = parent;
}
_calcBalance(_root);
Node* ppNode = parent->_parent;
if (ppNode && abs(ppNode->_bf) > 1)
{
if (ppNode->_bf < -1)
{
if (parent->_left)
_RotateR(ppNode);
else
_RotateRL(ppNode);
}
if (ppNode->_bf > 1)
{
if (parent->_right)
_RotateL(ppNode);
else
_RotateLR(ppNode);
}
}
_calcBalance(_root);
return true;
}
bool _IsBIanceTree()
{
return IsBIanceTree(_root);
}
Node* Find(const K& key)
{
if (_root == NULL)
{
return NULL;
}
Node* cur = _root;
while (cur)
{
if (cur->_key < key)
{
cur = cur->_right;
}
else if (cur->_key > key)
{
cur = cur->_left;
}
else
{
return cur;
}
}
return NULL;
}
void _calcBalance(Node* root)
{
calcBalance(_root);
}
size_t _Depth(Node* root)
{
if (root == NULL)
{
return 0;
}
else
{
size_t i = _Depth(root->_left);
size_t j = _Depth(root->_right);
if (i > j)
{
return i + 1;
}
else
{
return j + 1;
}
}
}
void Inreder()
{
_Inreder(_root);
}
protected:
bool IsBIanceTree(Node* root)
{
if (root == NULL)
return true;
size_t left = _Depth(root->_left);
size_t right = _Depth(root->_right);
int mid = right - left;
if (abs(mid) > 2)
{
cout << "这里有问题:" << root->_key << endl;
}
return abs(mid) < 2
&& IsBIanceTree(root->_left)
&& IsBIanceTree(root->_right);
}
void _Inreder(Node* root)
{
if (root == NULL)
return;
_Inreder(root->_left);
cout << root->_key << " ";
_Inreder(root->_right);
}
void calcBalance(Node* root)
{
if (root == NULL)
return;
root->_bf = _Depth(root->_right) - _Depth(root->_left);
calcBalance(root->_left);
calcBalance(root->_right);
}
void _RotateR(Node*& parent)
{
Node* subL = parent->_left;
Node* subLR = NULL;
if (subL)
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;
_root->_parent = NULL;
}
else
{
if (ppNode->_left == parent)
ppNode->_left = subL;
else
ppNode->_right = subL;
subL->_parent = ppNode;
}
}
void _RotateL(Node*& parent)
{
Node* subR = parent->_right;
Node* subRL = NULL;
if (subR)
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;
_root->_parent = NULL;
}
else
{
if (ppNode->_left == parent)
ppNode->_left = subR;
else
ppNode->_right = subR;
subR->_parent = ppNode;
}
}
void _RotateLR(Node*& parent)
{
_RotateR(parent->_right);
_RotateL(parent);
}
void _RotateRL(Node*& parent)
{
_RotateL(parent->_left);
_RotateR(parent);
}
protected:
Node* _root;
};
void TestPHSBTree1()
{
AVLTree<int, int> t1;
int a[] = { 2, 4, 3, 1, 0, 6, 5 };
for (size_t i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
{
t1.Insert(a[i]);
}
if (t1._IsBIanceTree())
cout << "构建AVL树正确" << endl;
else
cout << "构架AVL数出现错误" << endl;
t1.Inreder();
}