AVL树的介绍
二叉查找树的深度越小,那么查找所需要的运算时间越小。一个深度为log(n)的二叉查找树,查找算法的时间复杂度也是log(n)。然而,我们在二叉查找树中已经实现的插入和删除操作并不能让保持log(n)的深度。如果我们按照8,7,6,5,4,3,2,1的顺序插入节点,那么就是一个深度为n的二叉树,即二叉查找树退化成单向链表。那么,查找算法的时间复杂度为n。
AVL树是高度平衡的查找二叉树。它的特点是:AVL树中任何节点的两个子树的最大高度差为1,这样的平衡特性能保证查找、插入、删除三大操作的平均或最坏情况下的算法复杂度维持在log(n)。
上面的两张图片,左边的是AVL树,它的任何节点的两个子树的高度差别都<=1;而右边的不是AVL树,因为7的两颗子树的高度相差为2(以2为根节点的树的高度是3,而以8为根节点的树的高度是1)。
AVL树的C++实现
1. AVL树节点
#pragma once
/* ***AVLTreeNode: AVLTree的节点 ***key: 关键字,节点根据关键字排序,用来搜索 ***height: 以节点在根的子树的高度 ***leftChild: 节点的左孩子 ***rightChild: 节点的右孩子 */
template <typename T>
struct AVLTreeNode
{
T key;
int height;
AVLTreeNode<T> *leftChild,
*rightChild;
AVLTreeNode(const T &theKey) : key(theKey)
{
height = 0;
leftChild = rightChild = nullptr;
}
AVLTreeNode(const T &theKey, AVLTreeNode<T>* leftChild,
AVLTreeNode<T>* rightChild) : key(theKey)
{
height = 0;
this->leftChild = leftChild;
this->rightChild = rightChild;;
}
};
2. AVL树接口
template <typename T>
class AVLTree { public: AVLTree(); ~AVLTree(); // 获取AVL树的高度 int height(); // 前序、中序、后序遍历"AVL树" void preOrder() { preOrder(root); } void inOrder() { inOrder(root); } void postOrder() { postOrder(root); } // 查找"AVL树"中键值为key的节点 AVLTreeNode<T>* find(const T& key) const; // 查找最小和最大结点:返回结点的键值指针。 T* minimum(); T* maximum(); // 将结点插入到AVL树中 void insert(const T& theKey); // 删除结点 void erase(const T& theKey); // 销毁AVL树 void destroy(); void output() { inOrder(root); cout << endl; } private: AVLTreeNode<T> *root; // 根结点 int height(AVLTreeNode<T>* theRoot); // 前序、中序、后序遍历"AVL树" void preOrder(AVLTreeNode<T>* theRoot) const; void inOrder(AVLTreeNode<T>* theRoot) const; void postOrder(AVLTreeNode<T>* theRoot) const; // 查找最小和最大结点,返回节点指针。 AVLTreeNode<T>* minimum(AVLTreeNode<T>* theRoot) const; AVLTreeNode<T>* maximum(AVLTreeNode<T>* theRoot) const; // LL:左左对应的情况(左单旋转)。 AVLTreeNode<T>* LLRotation(AVLTreeNode<T>* aNode); // RR:右右对应的情况(右单旋转)。 AVLTreeNode<T>* RRRotation(AVLTreeNode<T>* aNode); // LR:左右对应的情况(左双旋转)。 AVLTreeNode<T>* LRRotation(AVLTreeNode<T>* aNode); // RL:右左对应的情况(右双旋转)。 AVLTreeNode<T>* RLRotation(AVLTreeNode<T>* aNode); // 将结点(z)插入到AVL树中 AVLTreeNode<T>* insert(AVLTreeNode<T>* &theRoot, const T& theKey); // 删除AVL树中的结点(p),并返回被删除的结点 AVLTreeNode<T>* erase(AVLTreeNode<T>* &theRoot, AVLTreeNode<T>* p); // 销毁AVL树 void destroy(AVLTreeNode<T>* &theRoot); };
AVLTree是AVL树对应的类。它包含AVL树的根节点root和AVL树的基本操作接口。需要说明的是:AVLTree中重载了许多函数。重载的目的是区分内部接口和外部接口,内部接口主要用于外部接口的递归实现,而外部接口供用户使用;例如insert()函数而言,insert(root, theKey)是内部接口,而insert(theKey)是外部接口。
3. 旋转操作(核心)
如果在AVL树中进行插入或删除节点后,可能导致AVL树失去平衡。这种失去平衡的可以概括为4种姿态:LL(左左),LR(左右),RR(右右)和RL(右左)。下面给出它们的示意图:
上图中的4棵树都是”失去平衡的AVL树”,从左往右的情况依次是:LL、LR、RL、RR。除了上面的情况之外,还有其它的失去平衡的AVL树,如下图:
上面的两张图都是为了便于理解,而列举的关于”失去平衡的AVL树”的例子。总的来说,AVL树失去平衡时的情况一定是LL、LR、RL、RR这4种之一,它们都由各自的定义:
(1) LL:LeftLeft,也称为”左左”。插入或删除一个节点后,根节点的左子树的左子树还有非空子节点,导致”根的左子树的高度”比”根的右子树的高度”大2,导致AVL树失去了平衡。
例如,在上面LL情况中,由于”根节点(8)的左子树(4)的左子树(2)还有非空子节点”,而”根节点(8)的右子树(12)没有子节点”;导致”根节点(8)的左子树(4)高度”比”根节点(8)的右子树(12)”高2。
(2) LR:LeftRight,也称为”左右”。插入或删除一个节点后,根节点的左子树的右子树还有非空子节点,导致”根的左子树的高度”比”根的右子树的高度”大2,导致AVL树失去了平衡。
例如,在上面LR情况中,由于”根节点(8)的左子树(4)的左子树(6)还有非空子节点”,而”根节点(8)的右子树(12)没有子节点”;导致”根节点(8)的左子树(4)高度”比”根节点(8)的右子树(12)”高2。
(3) RL:RightLeft,称为”右左”。插入或删除一个节点后,根节点的右子树的左子树还有非空子节点,导致”根的右子树的高度”比”根的左子树的高度”大2,导致AVL树失去了平衡。
例如,在上面RL情况中,由于”根节点(8)的右子树(12)的左子树(10)还有非空子节点”,而”根节点(8)的左子树(4)没有子节点”;导致”根节点(8)的右子树(12)高度”比”根节点(8)的左子树(4)”高2。
(4) RR:RightRight,称为”右右”。插入或删除一个节点后,根节点的右子树的右子树还有非空子节点,导致”根的右子树的高度”比”根的左子树的高度”大2,导致AVL树失去了平衡。
例如,在上面RR情况中,由于”根节点(8)的右子树(12)的右子树(14)还有非空子节点”,而”根节点(8)的左子树(4)没有子节点”;导致”根节点(8)的右子树(12)高度”比”根节点(8)的左子树(4)”高2。
前面说过,如果在AVL树中进行插入或删除节点后,可能导致AVL树失去平衡。AVL失去平衡之后,可以通过旋转使其恢复平衡,下面分别介绍”LL(左左),LR(左右),RR(右右)和RL(右左)”这4种情况对应的旋转方法。
LL的旋转
LL失去平衡的情况,可以通过一次旋转让AVL树恢复平衡。如下图:
图中左边是旋转之前的树,右边是旋转之后的树。从中可以发现,旋转之后的树又变成了AVL树,而且该旋转只需要一次即可完成。
对于LL旋转,你可以这样理解为:LL旋转是围绕”失去平衡的AVL根节点”进行的,也就是节点B;而且由于是LL情况,即左左情况,使A绕着B顺时针旋转。将B变成根节点,A变成B的右子树,”B的右子树”变成”A的左子树”。
LLRotation代码:
// LL型:左左对应的情况(左单旋转)
template <typename T>
AVLTreeNode<T>* AVLTree<T>::LLRotation(AVLTreeNode<T>* aNode)
{
AVLTreeNode<T>* bNode = aNode->leftChild;
aNode->leftChild = bNode->rightChild;
bNode->rightChild = aNode;
aNode->height = max(height(aNode->leftChild), height(aNode->rightChild)) + 1;
bNode->height = max(height(bNode->leftChild), height(bNode->rightChild)) + 1;
return bNode;
}
RR的旋转
理解了LL之后,RR就相当容易理解了。RR是与LL对称的情况!RR恢复平衡的旋转方法如下:
RR情况下,A节点绕着B节点逆时针旋转,使得A成为B的左孩子,B的左孩子成为A的右孩子。图中左边是旋转之前的树,右边是旋转之后的树。RR旋转也只需要一次即可完成。
RRRotation代码:
// RR型:右右对应的情况(右单旋转)。
template <typename T>
AVLTreeNode<T>* AVLTree<T>::RRRotation(AVLTreeNode<T>* aNode)
{
AVLTreeNode<T>* bNode = aNode->rightChild;
aNode->rightChild = bNode->leftChild;
bNode->leftChild = aNode;
aNode->height = max(height(aNode->leftChild), height(aNode->rightChild)) + 1;
bNode->height = max(height(bNode->leftChild), height(bNode->rightChild)) + 1;
return bNode;
}
LR的旋转
LR失去平衡的情况,需要经过两次旋转才能让AVL树恢复平衡。如下图:
第一次旋转是围绕”B”进行的”RR旋转”,第二次是围绕”C”进行的”LL旋转”。
LRRotation代码:
// LR型:左右对应的情况(左双旋转)。
template <typename T>
AVLTreeNode<T>* AVLTree<T>::LRRotation(AVLTreeNode<T>* aNode)
{
aNode->leftChild = RRRotation(aNode->leftChild);
return LLRotation(aNode);
}
RL的旋转
RL是与LR的对称情况!RL恢复平衡的旋转方法如下:
第一次旋转是围绕”B”进行的”LL旋转”,第二次是围绕”C”进行的”RR旋转”。
RLRotation代码:
// RL型:右左对应的情况(右双旋转)。
template <typename T>
AVLTreeNode<T>* AVLTree<T>::RLRotation(AVLTreeNode<T>* aNode)
{
aNode->rightChild = LLRotation(aNode->rightChild);
return RRRotation(aNode);
}
3. 完整的实现代码
AVL树的实现文件(AVRTree.h)
#pragma once
#include<iostream>
#include<algorithm>
#include"AVLTreeNode.h"
using namespace std;
template <typename T>
class AVLTree
{
public:
AVLTree() : root(nullptr) { }
~AVLTree() { destroy(); }
// 获取AVL树的高度
int height();
// 前序、中序、后序遍历"AVL树"
void preOrder() { preOrder(root); }
void inOrder() { inOrder(root); }
void postOrder() { postOrder(root); }
// 查找"AVL树"中键值为key的节点
AVLTreeNode<T>* find(const T& key) const;
// 查找最小和最大结点:返回结点的键值指针。
T* minimum();
T* maximum();
// 将结点插入到AVL树中
void insert(const T& theKey);
// 删除结点
void erase(const T& theKey);
// 销毁AVL树
void destroy();
void output() { inOrder(root); cout << endl; }
private:
AVLTreeNode<T> *root; // 根结点
int height(AVLTreeNode<T>* theRoot);
// 前序、中序、后序遍历"AVL树"
void preOrder(AVLTreeNode<T>* theRoot) const;
void inOrder(AVLTreeNode<T>* theRoot) const;
void postOrder(AVLTreeNode<T>* theRoot) const;
// 查找最小和最大结点,返回节点指针。
AVLTreeNode<T>* minimum(AVLTreeNode<T>* theRoot) const;
AVLTreeNode<T>* maximum(AVLTreeNode<T>* theRoot) const;
// LL:左左对应的情况(左单旋转)。
AVLTreeNode<T>* LLRotation(AVLTreeNode<T>* aNode);
// RR:右右对应的情况(右单旋转)。
AVLTreeNode<T>* RRRotation(AVLTreeNode<T>* aNode);
// LR:左右对应的情况(左双旋转)。
AVLTreeNode<T>* LRRotation(AVLTreeNode<T>* aNode);
// RL:右左对应的情况(右双旋转)。
AVLTreeNode<T>* RLRotation(AVLTreeNode<T>* aNode);
// 将结点(z)插入到AVL树中
AVLTreeNode<T>* insert(AVLTreeNode<T>* &theRoot, const T& theKey);
// 删除AVL树中的结点(p),并返回被删除的结点
AVLTreeNode<T>* erase(AVLTreeNode<T>* &theRoot, AVLTreeNode<T>* p);
// 销毁AVL树
void destroy(AVLTreeNode<T>* &theRoot);
};
template <typename T>
int AVLTree<T>::height(AVLTreeNode<T>* theRoot)
{
if(theRoot != nullptr)
return theRoot->height;
return 0;
}
template <typename T>
int AVLTree<T>::height()
{
return height(root);
}
template <typename T>
void AVLTree<T>::preOrder(AVLTreeNode<T>* theRoot) const
{
if (theRoot != nullptr)
{
cout << theRoot->key << " ";
preOrder(theRoot->leftChild);
preOrder(theRoot->rightChild);
}
}
template <typename T>
void AVLTree<T>::inOrder(AVLTreeNode<T>* theRoot) const
{
if (theRoot != nullptr)
{
inOrder(theRoot->leftChild);
cout << theRoot->key << " ";
inOrder(theRoot->rightChild);
}
}
template <typename T>
void AVLTree<T>::postOrder(AVLTreeNode<T>* theRoot) const
{
if (theRoot != nullptr)
{
postOrder(theRoot->leftChild);
postOrder(theRoot->rightChild);
cout << theRoot->key << " ";
}
}
template <typename T>
AVLTreeNode<T>* AVLTree<T>::find(const T& theKey) const
{
AVLTreeNode<T>* p = root;
while (p != nullptr)
{
if (theKey < p->key)
p = p->leftChild;
else if (theKey > p->key)
p = p->rightChild;
else
return p;
}
return nullptr;
}
template <typename T>
T* AVLTree<T>::minimum()
{
AVLTreeNode<T>* min = minimum(root);
if (min != nullptr)
return &min->key;
return nullptr;
}
template <typename T>
AVLTreeNode<T>* AVLTree<T>::minimum(AVLTreeNode<T>* theRoot) const
{
AVLTreeNode<T>* p = theRoot,
pp = nullptr;
while (p != nullptr)
{
pp = p;
p = p->leftChild;
}
return pp;
}
template <typename T>
T* AVLTree<T>::maximum()
{
AVLTreeNode<T>* max = maximum(root);
if (max != nullptr)
return &max->key;
return nullptr;
}
template <typename T>
AVLTreeNode<T>* AVLTree<T>::maximum(AVLTreeNode<T>* theRoot) const
{
AVLTreeNode<T>* p = theRoot,
pp = nullptr;
while (p != nullptr)
{
pp = p;
p = p->rightChild;
}
return pp;
}
// LL型:左左对应的情况(左单旋转)
template <typename T>
AVLTreeNode<T>* AVLTree<T>::LLRotation(AVLTreeNode<T>* aNode)
{
AVLTreeNode<T>* bNode = aNode->leftChild;
aNode->leftChild = bNode->rightChild;
bNode->rightChild = aNode;
aNode->height = max(height(aNode->leftChild), height(aNode->rightChild)) + 1;
bNode->height = max(height(bNode->leftChild), height(bNode->rightChild)) + 1;
return bNode;
}
// RR型:右右对应的情况(右单旋转)。
template <typename T>
AVLTreeNode<T>* AVLTree<T>::RRRotation(AVLTreeNode<T>* aNode)
{
AVLTreeNode<T>* bNode = aNode->rightChild;
aNode->rightChild = bNode->leftChild;
bNode->leftChild = aNode;
aNode->height = max(height(aNode->leftChild), height(aNode->rightChild)) + 1;
bNode->height = max(height(bNode->leftChild), height(bNode->rightChild)) + 1;
return bNode;
}
// LR型:左右对应的情况(左双旋转)。
template <typename T>
AVLTreeNode<T>* AVLTree<T>::LRRotation(AVLTreeNode<T>* aNode)
{
aNode->leftChild = RRRotation(aNode->leftChild);
return LLRotation(aNode);
}
// RL型:右左对应的情况(右双旋转)。
template <typename T>
AVLTreeNode<T>* AVLTree<T>::RLRotation(AVLTreeNode<T>* aNode)
{
aNode->rightChild = LLRotation(aNode->rightChild);
return RRRotation(aNode);
}
template <typename T>
void AVLTree<T>::insert(const T& theKey)
{
insert(root, theKey);
}
//用递归将theKey插入到AVL树中,并维持树的平衡
template <typename T>
AVLTreeNode<T>* AVLTree<T>::insert(AVLTreeNode<T>* &theRoot, const T& theKey)
{
if (theRoot == nullptr)
{
theRoot = new AVLTreeNode<T>(theKey);
if (theRoot == nullptr)
{
cerr << "ERROR: create avltree node failed!" << endl;
return nullptr;
}
}
else if (theKey < theRoot->key)
{//左子树一侧插入节点
theRoot->leftChild = insert(theRoot->leftChild, theKey);
if ( height(theRoot->leftChild) - height(theRoot->rightChild) == 2)
{ //插入节点后,AVL树失去平衡,平衡因子为2
if (theKey < theRoot->leftChild->key) //LL型
theRoot = LLRotation(theRoot);
else //LR型
theRoot = LRRotation(theRoot);
}
}
else if (theKey > theRoot->key)
{ //右子树一侧插入节点
theRoot->rightChild = insert(theRoot->rightChild, theKey);
if (height(theRoot->rightChild) - height(theRoot->leftChild) == 2)
{ //插入节点后,AVL树失去平衡,平衡因子为-2
if (theKey > theRoot->rightChild->key) //RR型
theRoot = RRRotation(theRoot);
else //RL型
theRoot = RLRotation(theRoot);
}
}
else //theKey == tree->key
cerr << "添加失败:不允许添加相同的节点!" << endl;
theRoot->height = max(height(theRoot->leftChild), height(theRoot->rightChild));
return theRoot;
}
template <typename T>
void AVLTree<T>::erase(const T& theKey)
{
AVLTreeNode<T>* P = find(theKey); //找到theKey对应的节点
if(p != nullptr)
root = erase(root, p);
}
template <typename T>
AVLTreeNode<T>* AVLTree<T>::erase(AVLTreeNode<T>* &theRoot, AVLTreeNode<T>* p)
{
if (theRoot == nullptr || p == nullptr)
return nullptr;
if (p->key < theRoot->key)
{ //待删除节点在左子树一侧
theRoot->leftChild = erase(theRoot->leftChild, p);
if (height(theRoot->right) - height(theRoot->leftChild) = 2)
{ //删除节点后,AVL树失去平衡,平衡因子为-2
AVLTreeNode<T>* r = theRoot->rightChild;
if (height(r->leftChild) > height(r->rightChild))
theRoot = RLRotation(theRoot);
else
theRoot = RRRotation(theRoot);
}
}
else if (p->key > theRoot->key)
{ //待删除节点在右子树一侧
theRoot->rightChild = erase(theRoot->rightChild, p);
if (height(theRoot->leftChild) - height(theRoot->rightChild) == 2)
{ //删除节点后,AVL树失去平衡,平衡因子为2
AVLTreeNode<T>* l = theRoot->leftChild;
if (l->rightChild > l->leftChild)
theRoot = LRRotation(theRoot);
else
theRoot = LLRotation(theRoot);
}
}
else
{ //待删除的节点就是theRoot
if ((theRoot->leftChild != nullptr) && (theRoot->rightChild != nullptr))
{ //待删除节点有两个孩子
if (height(theRoot->leftChild) > height(theRoot->rightChild))
{ //从theRoot的左子树找到最大节点填补到删除的节点位置
AVLTreeNode<T>* max = maximum(theRoot->leftChild);
theRoot->key = max->key;
theRoot->leftChild = erase(theRoot->leftChild, max);
}
else
{//从theRoot的左子树找到最大节点填补到删除的节点位置
AVLTreeNode<T>* min = minimum(theRoot->rightChild);
theRoot->key = min->key;
theRoot->rightChild = erase(theRoot->rightChild, min);
}
}
else
{ //待删除的节点最多有一个孩子
AVLTreeNode<T>* tmp = theRoot;
theRoot = (theRoot->leftChild != nullptr) ? theRoot->leftChild : theRoot->rightChild;
delete tmp;
}
}
return theRoot;
}
template <typename T>
void AVLTree<T>::destroy(AVLTreeNode<T>* &theRoot)
{
if (theRoot == nullptr)
return;
destroy(theRoot->leftChild);
destroy(theRoot->rightChild);
delete theRoot;
theRoot = nullptr;
}
template <typename T>
void AVLTree<T>::destroy()
{
destroy(root);
}