参考链接:点击打开链接
创建AVL树的过程,主要是在构建二叉树插入每个结点时都要调用一次平衡操作balance函数,而调用balance函数的过程中涉及到了求结点高度,求结点的平衡因子,LL、LR、RR、RL旋转操作。(注意每次调用旋转操作时要将旋转后子树总结点temp返回,再将旋转前的结点改变指点,即root = balance(root)还有如root->m_pLeft = RR_Rotation(root->m_pLeft);
实现代码如下:
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
template <typename keyType>//之后的类名一定要记得加上<typename>
class BinaryTreeNode{
public:
keyType value;
BinaryTreeNode *m_pLeft;
BinaryTreeNode *m_pRight;
BinaryTreeNode(keyType v):value(v),m_pLeft(nullptr),m_pRight(nullptr){}
};
template <typename keyType>
class BT{
typedef BinaryTreeNode<keyType> BTNode;//给结点定义别名
public:
BTNode * insertBTNode(BTNode *&root,keyType val);
void createBT(keyType arr[],int n);//创建二叉排序树,通过*&来改变指针的值
void midorder_showBT();//中序遍历
void midorder_showBT_core(const BTNode *root);
void level_showBT();//中序遍历
void level_showBT_core(BTNode *root);
~BT();//析构函数
void release_BT_core(BTNode *root);
int _getHeight(BTNode *root);
int getHeight();//获取高度
int diff(BTNode *root);//计算平衡因子
BTNode *balance(BTNode *root);//平衡操作
BTNode *LL_Rotation(BTNode *root);
BTNode *LR_Rotation(BTNode *root);
BTNode *RL_Rotation(BTNode *root);
BTNode *RR_Rotation(BTNode *root);
BTNode *root;
};
int main(){
BT<float> bt;
float arr[] = {16,3,7,11,9,26,18,14,15};
bt.createBT(arr,sizeof(arr)/sizeof(arr[0]));//构建二叉排序树
cout<<"中序遍历:"<<endl;
bt.midorder_showBT();//中序遍历打印
cout<<endl<<endl;
cout<<"层次遍历:"<<endl;
bt.level_showBT();//前序遍历打印
cout<<endl<<endl;
cout<<"树高:";
cout<<bt.getHeight()<<endl<<endl;
cout<<"树的根结点平衡因子:";
cout<<bt.diff(bt.root)<<endl<<endl;//求平衡因子
return 0;
}
template <typename keyType>
void BT<keyType>::createBT(keyType arr[],int n){
root = nullptr;
for(int i=0;i<n;i++)
insertBTNode(root,arr[i]);
}
template <typename keyType>
BinaryTreeNode<keyType> * BT<keyType>::insertBTNode(BTNode *&root,keyType val){
if(root == nullptr){
root = new BTNode(val);
return root;
}
if(val == root->value){
return root;
}
else if(val < root->value){
//root->m_pLeft = insertBTNode(root->m_pLeft,val);
insertBTNode(root->m_pLeft,val);
root = balance(root); //注意这里是把平衡后的返回置temp赋值给root
return root;
}
else{
//root->m_pRight = insertBTNode(root->m_pRight,val);
insertBTNode(root->m_pRight,val);
root = balance(root);
return root;
}
}
template <typename keyType>
BinaryTreeNode<keyType> *BT<keyType>::balance(BTNode *root){
int dis = diff(root);//计算结点的平衡因子
if(dis > 1){//左
if( diff(root->m_pLeft) > 0)
return LL_Rotation(root);
else
return LR_Rotation(root);
}
else if(dis < -1){//右
if( diff(root->m_pRight) < 0)
return RR_Rotation(root);
else
return RL_Rotation(root);
}
return root;//无需转换时记得返回root
}
template <typename keyType>
BinaryTreeNode<keyType> *BT<keyType>::LL_Rotation(BTNode *root){
BTNode *temp = root->m_pLeft;
root->m_pLeft = temp->m_pRight;
temp->m_pRight = root;
return temp;//返回要旋转子树的主结点
}
template <typename keyType>
BinaryTreeNode<keyType> *BT<keyType>::RR_Rotation(BTNode *root){
BTNode *temp = root->m_pRight;
root->m_pRight = temp->m_pLeft;
temp->m_pLeft = root;
return temp;
}
template <typename keyType>
BinaryTreeNode<keyType> *BT<keyType>::LR_Rotation(BTNode *root){//先进行RR操作,再进行LL操作
//注意这里一定要对root->m_Pleft重新赋值
root->m_pLeft = RR_Rotation(root->m_pLeft);//先对root后的左结点进行RR操作
return LL_Rotation(root);//再对root进行LL操作
}
template <typename keyType>
BinaryTreeNode<keyType> *BT<keyType>::RL_Rotation(BTNode *root){//先进行LL操作,再进行RR操作
//注意这里一定要对root->m_pRight重新赋值
root->m_pRight = LL_Rotation(root->m_pRight);//先对root后的右结点进行LL操作
return RR_Rotation(root);//再对root进行RR操作
}
template <typename keyType>
void BT<keyType>::midorder_showBT(){
midorder_showBT_core(root);
}
template <typename keyType>
void BT<keyType>::midorder_showBT_core(const BTNode *root){
if(root == nullptr){
return;
}
midorder_showBT_core(root->m_pLeft);
cout<<root->value<<" ";
midorder_showBT_core(root->m_pRight);
}
template <typename keyType>
void BT<keyType>::level_showBT()//中序遍历
{
level_showBT_core(root);
}
template <typename keyType>
void BT<keyType>::level_showBT_core(BTNode *root){
if(root == nullptr)
return;
queue<BinaryTreeNode<keyType> *> que;
que.push(root);
while(!que.empty()){
if( que.front()->m_pLeft != nullptr)
que.push(que.front()->m_pLeft);
if(que.front()->m_pRight != nullptr)
que.push(que.front()->m_pRight);
cout<<que.front()->value<<" ";
que.pop();
}
}
template <typename keyType>
BT<keyType>::~BT()//释放二叉树
{
release_BT_core(root);
}
template <typename keyType>
void BT<keyType>::release_BT_core(BTNode *root)//释放二叉树
{
if(root == nullptr)
return;
release_BT_core(root->m_pLeft);
release_BT_core(root->m_pRight);
delete root;
root = nullptr;
return ;
}
template <typename keyType>
int BT<keyType>::getHeight(){
_getHeight(root);
}
template <typename keyType>
int BT<keyType>::_getHeight(BTNode *root){
if(root == nullptr)
return 0;
return max(_getHeight(root->m_pLeft),_getHeight(root->m_pRight))+1;
}
template <typename keyType>
int BT<keyType>::diff(BTNode *root){
if(root == nullptr)
return 0;
return _getHeight(root->m_pLeft) - _getHeight(root->m_pRight);
}
运行结果如下: