AVL树
向二叉搜索树中插入新节点后,保证每个节点的左右子树高度差的绝对值不超过1(对树中的节点进行调整),降低树的高度,减少平均搜索长度
AVL树具有以下特点:
- 左右子树都是AVL树
- 左右子树高度只差(平衡因子)的绝对值不超过1(1/0/-1)
如果一颗二叉搜索树是高度平衡的,就是AVL树,如果其存在n个结点,其高度可保持在O(log N),搜索时时间复杂度(O (longN))
AVL树结点的定义
tmplete <class T>
struct AVLTree
{
AVLTreeNode(const T& data)
: _pleft(nullptr)
, _pright(nullptr)
, _pParent(nullptr)
{}
AVLTreeNode<T>* _pLeft;//
AVLTreeNode<T>* _pRight;//该节点的右孩子
AVLTreeNode<T>* _pParent;//该节点的双亲
T _data;
int _bf; //该节点的平衡因子
};
AVL树的插入
1.按照二叉搜索树的方式插入新节点
2.新节点插入后,平衡可能遭到破坏,需要重新更新平衡因子,并检测是否破坏AVL树的平衡性
cur插入后,parent的平衡因子一定需要调整,此时:
a. cur插入在parent的左侧,给parent的平衡因子-1
b. cur插入在parent的右侧,给parent的平衡因子+1
在更新完成后parent的平衡因子可能为: 0;正负1;正负2
a. 如果parent的平衡因子为0,说明插入之前parent的平衡因子为正负1,插入后被调整成0,此时满足AVL树的性质,插入成功
b. 如果parent的平衡因子为正负1,说明插入前parent的平衡因子一定为0插入后被更新成正负1,此时以parent为根的树的高度增加,需要继续向上更新
c. 如果parent的平衡因子为正负2,则parent的平衡因子违反平衡树的性质,需要对其进行旋转处理
AVL树的旋转
假如以parent为根的子树不平衡,即parent的平衡因子为2或者-2,分以下情况考虑
- parent的平衡因子为2,说明parent的右子树高,设pParent的右子树的根为subR
当subR的平衡因子为1时,执行左单旋
当subR的平衡因子为-1时,执行右左双旋 - parent的平衡因子为-2,说明parent的左子树高,设parent的左子树的根为subL
当subL的平衡因子为-1是,执行右单旋
当subL的平衡因子为1时,执行左右双旋
旋转完成后,原parent为根的子树个高度降低,已经平衡,不需要再向上更新。
#pragma once
#include <iostream>
#include <cassert>
#include <utility>
using std::pair;
using std::cout;
using std::endl;
template <class K,class V>
struct AVLTreeNode
{
pair<K, V> _kv;
AVLTreeNode<K, V>* _left;
AVLTreeNode<K, V>* _right;
AVLTreeNode<K, V>* _parent;
int _bf; //balance factor
AVLTreeNode(const pair<K, V>& kv)
: _kv(kv)
, _left(nullptr)
, _right(nullptr)
, _parent(nullptr)
, _bf(0)
{}
};
template <class K, class V>
class AVLTree
{
typedef AVLTreeNode<K, V> Node;
public:
bool Insert(const pair<K, V>& kv)
{
//插入节点
if (_root == nullptr)
{
_root = new Node(kv);
_root->_bf = 0;
return true;
}
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
//插入节点
if (cur->_kv.first < kv.first)
{
parent = cur;
cur = cur->_right;
}
else if (cur->_kv.first > kv.first)
{
parent = cur;
cur = cur->_left;
}
else
{
return false;
}
}
cur = new Node(kv);
if (parent->_kv.first < kv.first)
{
parent->_right = cur;
cur->_parent = parent;
}
else
{
parent->_left = cur;
cur->_parent = parent;
}
//调平衡
//1.更新平衡因子
while (parent)
{
if (cur == parent->_right)
{
parent->_bf++;
}
else
{
parent->_bf--;
}
if (parent->_bf == 0)//高度不变,更新完成
{
break;
}
else if (abs(parent->_bf) == 1)//高度变,继续完成
{
cur = parent;
parent = parent->_parent;
}
else if (abs(parent->_bf) == 2)//旋转
{
if (parent->_bf == 2)
{
if (cur->_bf == 1)
{
RotateL(parent);
}
else if (cur->_bf == -1)
{
RotateRL(parent);
}
}
else if (parent->_bf == -2)
{
if (cur->_bf == -1)
{
RotateR(parent);
}
else if (cur->_bf == 1)
{
RotateLR(parent);
}
}
break;
}
else
{
assert(false);
}
}
return true;
}
//左单旋
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 (parent == _root)
{
_root = subR;
_root->_parent = nullptr;
}
else
{
if (ppNode->_left == parent)
{
ppNode->_left = subR;
}
else
{
ppNode->_right = subR;
}
subR->_parent = ppNode;
}
parent->_bf = subR->_bf = 0;
}
//右单旋
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 == nullptr)
{
_root = subL;
_root->_parent = nullptr;
}
else
{
if (ppNode->_left == parent)
{
ppNode->_left = subL;
}
else
{
ppNode->_right = subL;
}
subL->_parent = ppNode;
}
parent->_bf = subL->_bf = 0;
}
//新节点插入较高左子树的右侧--先左单旋再右单旋
void RotateLR(Node* parent)
{
RotateL(parent->_left);
RotateR(parent);
}
//新节点插入较高右子树的左侧--先右单旋再左单旋
void RotateRL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
int bf = subRL->_bf;
RotateR(parent->_right);
RotateL(parent);
if (bf == 0)
{
parent->_bf = subRL->_bf = subR->_bf = 0;
}
else if (bf == -1)
{
subR->_bf = 1;
subRL->_bf = 0;
parent->_bf = 0;
}
else if (bf == 1)
{
subR->_bf = 0;
subRL->_bf = 0;
parent->_bf = -1;
}
}
//中序遍历
void InOrder()
{
_Inorder(_root);
cout << endl;
}
void _Inorder(Node* root)
{
if (root == nullptr)
{
return;
}
_Inorder(root->_left);
cout << root->_kv.first << " ";
_Inorder(root->_right);
}
private:
Node* _root = nullptr;
};
AVL的性能分析
在左右子树高度之差绝对值不大于1的情况下可以保证高效的时间复杂度:logN 但与此同时AVL树在插入和删除的时候旋转次数较多,尤其是在删除的情况下,可能需要旋转到根.由此可知,AVL适用于数据个数为静态的情况下.