1 概念
1.1 定义
AVL树又称为高度平衡的二叉搜索树,是1962年有俄罗斯的数学家G.M.Adel’son-Vel’skii和E.M.Landis提出来的。它能保持二叉树的高度平衡,尽量降低二叉树的高度,减少树的平均搜索长度
1.2 性质
- 左子树和右子树的高度之差的绝对值不超过1
树中的每个左子树和右子树都是AVL树
注实现平衡树可采用的方法之一:
每个节点都有一个平衡因子(balance factor–bf),任一节点的平衡因子是-1,0,1。(每个节点的平衡因子等于右子树的高度减去左子树的高度 )
1.3 AVL树效率
一棵AVL树有N个节点,其高度可以保持在lgN,插入/删除/查找的时间复杂度也是lgN。
2 分析
AVL树操作包括:插入,删除等。
由于AVL树要求左右子树高度差的绝对值不超过1,因此AVL树必须通过旋转调平衡因子保持平衡。
插入:先插入节点再调平衡因子。
2.1插入节点
- 若AVL树为空则new一个节点作为根节点;
- 若要插入的节点等于根节点,返回false;
- 若要插入的节点大于根节点,递归右子树插入节点,反之递归左子树插入节点。
注:若插入节点在其父节点的左侧,其父节点的平衡因子减1;若插入节点在其父节点的右侧,其父节点的平衡因子加1;
2.2 调平衡因子(通过旋转)
- 如果parent->_bf==0,表示parent所在子树的高度不变,则不需要继续向上更新;
- 若更新后|parent->_bf|=1,向上继续更新平衡因子
- 若|parent->_bf|=2,不再更新,旋转使之平衡
2.2.1 单旋
2.2.2 双旋
1)左右旋
1)右左旋
3 模拟代码实现
//AVLtree.h
#pragma once
#include<iostream>
#include<stdlib.h>
#include<math.h>
template<class K, class V>//AVL树节点的定义
struct AVLtreeNode
{
K _key;
V _value;
AVLtreeNode<K, V>* _left;
AVLtreeNode<K, V>* _right;
AVLtreeNode<K, V>* _parent;
int _bf; // 平衡因子即左右高度差,-1,0,1
AVLtreeNode(const K& key, const V& value)
: _key(key)
, _value(value)
,_left(NULL)
, _right(NULL)
, _parent(NULL)
, _bf(0)
{}
};
template<class K, class V>
class AVLtree
{
typedef AVLtreeNode<K, V> Node;
public:
AVLtree()
:_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->_right;
}
else if (cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else
{
return false;
}
}
cur = new Node(key, value);//cur为新增节点,parent为父节点
if (parent->_key < key)//三叉链
{
parent->_right = cur;
cur->_parent = parent;
}
else
{
parent->_left = cur;
cur->_parent = parent;
}
//插入数据后,更新平衡因子
while (parent)
{
if (parent->_right == cur)
{
parent->_bf++;//cur在parent右,parent._bf++
}
else
{
parent->_bf--;//cur在parent左,parent._bf--
}
//1.如果parent->_bf==0,表示parent所在子树的高度不变,则不需要继续向上更新;
//2.若更新后|parent->_bf|=1,向上继续更新平衡因子;
//3.若|parent->_bf|=2,不再更新,旋转使之平衡
if (parent->_bf == 0)
{
break;
}
else if (parent->_bf == 1 || parent->_bf == -1)
{
cur = parent;
parent = cur->_parent;
}
else if (parent->_bf == 2 || parent->_bf == -2)//旋转
{
if (parent->_bf == 2)
{
if (cur->_bf == 1)//左旋
{
RotateL(parent);
}
else//双旋
{
RotateRL(parent);
}
}
else//parent->_bf==-2
{
if (cur->_bf == -1)//右旋
{
RotateR(parent);
}
else//双旋
{
RotateLR(parent);
}
}
break;
}
}
return true;
}
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 (ppNode == NULL)
{
_root = subL;
subL->_parent = NULL;
}
else if (ppNode->_left == parent)
{
ppNode->_left = subL;
}
else
{
ppNode->_right = subL;
}
subL->_parent = ppNode;
parent->_bf = subL->_bf = 0;//更新平衡因子
}
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 (ppNode == NULL)
{
_root = subR;
subR->_parent = NULL;
}
else if (ppNode->_left == parent)
{
ppNode->_left = subR;
}
else
{
ppNode->_right = subR;
}
subR->_parent = ppNode;
parent->_bf = subR->_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)
{
parent->_bf = 1;
subL->_bf = 0;
}
else if (bf == 1)
{
parent->_bf = 0;
subL->_bf = -1;
}
else
{
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)
{
parent->_bf = -1;
subR->_bf = 0;
}
else if (bf == -1)
{
parent->_bf = 0;
subR->_bf = 1;
}
else
{
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);
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
bool _IsBalance(Node* root, int height)
{
if (root == NULL)
{
height = 0;
return true;
}
int leftheight = 0;
int rightheight = 0;
if (_IsBalance(root->_left, leftheight) == false)
return false;
if (_IsBalance(root->_right, rightheight) == false)
return false;
//if (rightheight - leftheight != root->_bf)
//{
// cout << "平衡因子异常:" << root->_key << endl;
// //return false;
//}
height = leftheight > rightheight ? leftheight + 1 : rightheight + 1;
return abs(leftheight - rightheight) < 2;
}
bool IsBalance()//判断是否为平衡树,根据树的两个子树的高度差
{
//1.当前树;2.左子树;3.右子树
int H = 0;
return _IsBalance(_root, H);
}
private:
Node* _root;
};
//test.cpp
#include"AVLtree.h"
using namespace std;
void Test()
{
//int a[] = {16, 3, 7, 11, 9, 26, 18, 14, 15};
int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16,14};
AVLtree<int, int> t;
for (size_t i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
{
t.Insert(a[i], i);
cout << a[i] << "是否平衡?" << t.IsBalance() << endl;
}
t.InOrder();
cout << "是否平衡?" << t.IsBalance() << endl;
}
int main()
{
Test();
system("pause");
return 0;
}
运行结果如下: