AVL树基于二叉搜索树,为了解决快速查找出现次数最多的数,引入了键值对key value
二叉搜索树参考:点击打开链接
1.0 AVL树定义:
2.0 思路:
注: 红圈为插入的节点
3.0 代码:
AVL.h
#pragma once
#include<iostream>
using namespace std;
template<class k,class v>
struct AVLtreeNode
{
AVLtreeNode<k, v>* _left;
AVLtreeNode<k, v>* _right;
AVLtreeNode<k, v>* _parent;
k _key;
v _value;
int _bf;
AVLtreeNode(const k& key, const v& value)
:_left(NULL)
,_right(NULL)
,_parent(NULL)
, _key(key)
, _value(value)
, _bf(0) //平衡因子
{}
};
template<class k, class v>
class AVLtree
{
typedef AVLtreeNode<k, v> Node;
typedef AVLtreeNode<k, v>* PNode;
public:
AVLtree()
:_root(NULL)
{}
bool Insert(const k& key, const v& value)
{
return _Insert(_root, key, value);
}
void InOrderReverse()
{
return _InOrderReverse(_root);
}
void showbf()
{
_showbf(_root);
}
bool IsBalanceTree()
{
return _IsBalanceTree(_root);
}
protected:
void _rotateL(PNode& parent)
{
PNode PSubR = parent->_right;
PNode PSubRL = PSubR->_left;
parent->_right = PSubRL;
if (PSubRL)
{
PSubRL->_parent = parent;
}
PNode pparent = parent->_parent; //记录parent的双亲节点必须在更改它的双亲节点之前
PSubR->_left = parent;
parent->_parent = PSubR;
PSubR->_parent = pparent;
if (NULL == pparent)
{
_root = PSubR;
PSubR->_parent = NULL;
}
else
{
if (pparent->_left == parent)
pparent->_left = PSubR;
else
pparent->_right = PSubR;
}
//更新平衡因子
PSubR->_bf= parent->_bf = 0;
}
void _rotateR(PNode& parent)
{
PNode PSubL = parent->_left;
PNode PSubLR = PSubL->_right;
parent->_left = PSubLR;
if (PSubLR)
PSubLR->_parent = parent;
PSubL->_right = parent;
PNode pparent = parent->_parent; //记录parent的双亲节点必须在更改它的双亲节点之前
parent->_parent = PSubL;
PSubL->_parent = pparent;
if (NULL == pparent)
{
_root = PSubL;
PSubL->_parent = NULL;
}
else
{
if (pparent->_left == parent) //pparent记录着parent原来的左右孩子
pparent->_left = PSubL;
else
pparent->_right = PSubL;
}
//更新平衡因子
PSubL->_bf = parent->_bf = 0;
}
void _rotateLR(PNode& parent)
{
PNode PSubL = parent->_left;
PNode PSubLR = PSubL->_right;
int bf = PSubLR->_bf;
_rotateL(parent->_left);
_rotateR(parent);
//更新平衡因子
if (bf == 1)
PSubL->_bf = -1;
else if (bf == -1)
parent->_bf = 1;
else
parent->_bf = PSubL->_bf = 0;
}
void _rotateRL(PNode& parent)
{
PNode PSubR = parent->_right;
PNode PSubRL= PSubR->_left;
int bf = PSubRL->_bf;
_rotateR(parent->_right);
_rotateL(parent);
//更新平衡因子
if (bf == 1)
parent->_bf = -1;
else if (bf == -1)
PSubR->_bf = 1;
else
parent->_bf = PSubR->_bf = 0;
}
bool _Insert(PNode& root, const k& key, const v& value)
{
if (NULL == root) //树为空
{
_root = new Node(key, value);
return true;
}
//找待插入节点的位置
PNode PCur = root;
PNode parent = NULL;
while (PCur)
{
if (key > PCur->_key)
{
parent = PCur;
PCur = PCur->_right;
}
else if (key < PCur->_key)
{
parent = PCur;
PCur = PCur->_left;
}
else
return false;
}
//插入节点
PCur = new Node(key, value);
if (key>parent->_key)
parent->_right = PCur;
else
parent->_left = PCur;
PCur->_parent = parent;
//更新平衡因子
while (parent)
{
if (parent->_left ==PCur)
parent->_bf--;
else
parent->_bf++;
if (parent->_bf == 0)
return true;
else if (parent->_bf == -1 || parent->_bf == 1)
{
PCur = parent;
parent = PCur->_parent;
}
else //parent->_bf == -2 || parent->_nf == 2
{
if (parent->_bf == 2)
{
if (PCur->_bf == 1)
_rotateL(parent);
else
_rotateRL(parent);
}
else //parent->_bf == -2
{
if (PCur->_bf == -1)
{
_rotateR(parent);
}
else
_rotateLR(parent);
}
break;
}
}
return true;
}
void _InOrderReverse(PNode root)
{
if (root)
{
_InOrderReverse(root->_left);
cout << root->_key << " ";
_InOrderReverse(root->_right);
}
}
//打印节点平衡因子(中序)
void _showbf(PNode root)
{
if (root)
{
_showbf(root->_left);
cout << root->_bf << " ";
_showbf(root->_right);
}
}
/*bool _IsBalanceTree(PNode root)
{
if (NULL == root)
return true;
if (!(root->_bf >= -1 && root->_bf <= 1))
return false;
return _IsBalanceTree(root->_left) && _IsBalanceTree(root->_right);
}*/
//求以该节点为根的树的高度
size_t _Height(PNode root)
{
if (NULL == root)
return 0;
size_t LeftHeight = _Height(root->_left);
size_t RightHeight = _Height(root->_right);
return (LeftHeight > RightHeight ? LeftHeight + 1 : RightHeight + 1);
}
bool _IsBalanceTree(PNode root)
{
if (NULL == root)
return true;
size_t LeftHeight = _Height(root->_left);
size_t RightHeight = _Height(root->_right);
int distance = LeftHeight - LeftHeight;
if (abs(distance)>1&&root->_bf!=distance)
return false;
return _IsBalanceTree(root->_left) && _IsBalanceTree(root->_right);
}
private:
PNode _root;
};
void AVL_Test()
{
//int arr[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
int arr[] = { 2,8,5,7,4,63,31};
int size = sizeof(arr) / sizeof(arr[0]);
AVLtree<int, int> at;
for (int i = 0; i < size; ++i)
at.Insert(arr[i], i);
cout <<"中序遍历结果:"<< endl;
at.InOrderReverse();
cout << endl;
cout<< at.IsBalanceTree() << endl;
at.showbf();
cout << endl;
}
main.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include"AVL.h"
#include<iostream>
using namespace std;
int main()
{
AVL_Test();
system("pause");
return 0;
}
4.0 结果: