前面几个博客一直都是针对二叉树的基本操作和概念,今天我们是时候上一个硬菜了,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)
{}
};
平衡因子的生成
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树的插入
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 _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 _RotateLR(Node*& parent)
{
_RotateR(parent->_right);
_RotateL(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树:
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);
}
#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();
}