AVL树的原理挺复杂的,推荐一个博客,原理写得非常详细。
https://blog.csdn.net/howardemily/article/details/79543892
在这我就直接上 简化版的AVL代码吧, 希望对后来者有用。
对于LL
AVLtree* opLL() {
AVLtree* pa = this, *pb = left, *pc = ((left)->left);
pa->left = pb->right;
pb->right = pa;
return pb;
}
对于RR
AVLtree* opRR() {
AVLtree* pa = this, *pb = right, *pc = (right->right);
pa->right = pb->left;
pb->left = pa;
return pb;
}
对于LR
AVLtree* opLR() {
AVLtree* pa = this, *pb = left, *pc = (left->right);
pb->right = pc->left;
pc->left = pb;
pa->left = pc->right;
pc->right = pa;
return pc;
}
对于RL
AVLtree* opRL() {
AVLtree* pa = this, *pb = right, *pc = (right->left);
pb->left = pc->right;
pc->right = pb;
pa->right = pc->left;
pc->left = pa;
return pc;
}
需要俩个函数 获取 树高的函数 和 获取平衡因子(决定选用哪个平衡方法)的函数
int getheight(AVLtree* p) {
if (p == NULL) return 0;
return max(getheight(p->left), getheight(p->right)) + 1;
}
int getbf(AVLtree* p) {
return p->bf = (p->left == NULL ? 0 : getheight(p->left)) - (p->right == NULL ? 0 : getheight(p->right));
}
平衡函数
AVLtree* blance(AVLtree* p) {
if (p == NULL) return NULL;
p->left = blance(p->left);
p->right = blance(p->right);
if (getbf(p) > 1) {
if (getbf(p->left) > 0) p = p->opLL();
else p = p->opLR();
}
else if (getbf(p) < -1) {
if (getbf(p->right) > 0) p = p->opRL();
else p = p->opRR();
}
return p;
}
总代码:
#pragma warning(disable:4996)
#include <iostream>
#include <algorithm>
using namespace std;
class AVLtree {
public:
int value=0, bf=0, height = 0;
AVLtree *left = NULL, *right = NULL;
AVLtree* opLL() {
AVLtree* pa = this, *pb = left, *pc = ((left)->left);
pa->left = pb->right;
pb->right = pa;
return pb;
}
AVLtree* opRR() {
AVLtree* pa = this, *pb = right, *pc = (right->right);
pa->right = pb->left;
pb->left = pa;
return pb;
}
AVLtree* opRL() {
AVLtree* pa = this, *pb = right, *pc = (right->left);
pb->left = pc->right;
pc->right = pb;
pa->right = pc->left;
pc->left = pa;
return pc;
}
AVLtree* opLR() {
AVLtree* pa = this, *pb = left, *pc = (left->right);
pb->right = pc->left;
pc->left = pb;
pa->left = pc->right;
pc->right = pa;
return pc;
}
};
int getheight(AVLtree* p);
int getbf(AVLtree* p);
AVLtree* insert(AVLtree* p, int x) {
if (p == NULL) {
p = new AVLtree;
p->value = x;
return p;
}
if (x >= p->value) p->right = insert(p->right, x);
else p->left = insert(p->left, x);
return p;
}
AVLtree* blance(AVLtree* p) {
if (p == NULL) return NULL;
p->left = blance(p->left);
p->right = blance(p->right);
if (getbf(p) > 1) {
if (getbf(p->left) > 0) p = p->opLL();
else p = p->opLR();
}
else if (getbf(p) < -1) {
if (getbf(p->right) > 0) p = p->opRL();
else p = p->opRR();
}
return p;
}
int getheight(AVLtree* p) {
if (p == NULL) return 0;
return max(getheight(p->left), getheight(p->right)) + 1;
}
int getbf(AVLtree* p) {
return p->bf = (p->left == NULL ? 0 : getheight(p->left)) - (p->right == NULL ? 0 : getheight(p->right));
}
void print(AVLtree* p) {
if (p == NULL) return;
cout << p->value << '-' <<getbf(p)<< ' ';
print(p->left);
print(p->right);
}
int main() {
int n, x;
cin >> n;
AVLtree *p=NULL;
while (n--) {
cin >> x;
p = insert(p, x);
//print(p);
p = blance(p);
//print(p);
}
cout << p->value << endl;
system("pause");
return 0;
}