最近学习了二叉平衡树(AVL树),记录如下。
AVL树插入情况汇总
树型 | 判定条件(BF代表平衡因子) | 调整方法 |
---|---|---|
LL | BF(root)=2,BF(root->lchild)=1 | 对root进行右旋 |
LR | BF(root)=2,BF(root->lchild)=-1 | 先对root->lchild进行左旋,再对root进行右旋 |
RR | BF(root)=-2,BF(root->rchild)=-1 | 对root进行左旋 |
RL | BF(root)=-2,BF(root->rchild)=1 | 先对root->rchild进行右旋,再对root进行左旋 |
树的结构体定义
struct tree {
int data; // 数据
tree *lchild, *rchild; // 左右孩子指针
int height; // 当前结点树高
};
新建一个结点
tree* newnode(int data) {
tree* root = new tree;
root->lchild = root->rchild = NULL;
root->data = data;
root->height = 1; // 新插入结点树高必为1
return root;
}
获得树的高度
int getheight(tree* &root) {
if (root == NULL) {
return 0;
} else {
return root->height;
}
}
更新树的高度
int updateheight(tree* &root) {
if (root == NULL) {
return 0;
} else {
root->height = max(updateheight(root->lchild), updateheight(root->rchild)) + 1;
}
}
获得平衡因子
int getbalancefactor(tree* &root) {
return getheight(root->lchild) - getheight(root->rchild);
}
左旋操作
void L(tree* &root) {
tree* temp = root->rchild;
root->rchild = temp->lchild;
temp->lchild = root;
updateheight(root);
updateheight(temp);
root = temp;
}
右旋操作
void R(tree* &root) {
tree* temp = root->lchild;
root->lchild = temp->rchild;
temp->rchild = root;
updateheight(root);
updateheight(temp);
root = temp;
}
插入操作
void insert(tree* &root, int target) {
if (root == NULL) {
root = newnode(target);
return;
}
if (target < root->data) {
insert(root->lchild, target);
updateheight(root);
if (getbalancefactor(root) == 2) {
if (getbalancefactor(root->lchild) == 1) { // LL型
R(root); // 右旋
} else if (getbalancefactor(root->lchild) == -1) { // LR型
L(root->lchild); // 左旋
R(root); // 右旋
}
}
} else if (target >= root->data) {
insert(root->rchild, target);
updateheight(root);
if (getbalancefactor(root) == -2) {
if (getbalancefactor(root->rchild) == 1) { // RL型
R(root->rchild); // 右旋
L(root); // 左旋
} else if (getbalancefactor(root->rchild) == -1) { // RR型
L(root); // 左旋
}
}
}
}
层序遍历
void levelorder(tree* &root) {
queue<tree*> q;
vector<int> order;
if (root == NULL) {
cout << "The tree is empty!";
return;
} else {
q.push(root);
}
while (!q.empty()) {
tree* top = q.front();
order.push_back(top->data);
printf("%d: %d\n", top->data, top->height);
q.pop();
if (top->lchild != NULL) {
q.push(top->lchild);
}
if (top->rchild != NULL) {
q.push(top->rchild);
}
}
int cnt = 0;
int Pow = 1;
int level = 1;
printf("lever%d: ", level);
for (int i = 0; i < order.size(); i++) {
cnt++;
printf("%d", order[i]);
if (cnt == Pow) {
Pow = Pow*2 + Pow;
level++;
cout << endl;
if (cnt != order.size()) {
printf("lever%d: ", level);
}
} else {
cout << " ";
}
}
}
查找操作
void search(tree* root, int target) {
if (root == NULL) {
cout << "find falied!" << endl;
} else if (target < root->data) {
search(root->lchild, target);
} else if (target > root->data) {
search(root->rchild, target);
} else {
printf("find success!\n");
}
}
完整程序代码
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct tree {
int data; // 数据
tree *lchild, *rchild; // 左右孩子指针
int height; // 当前结点树高
};
// 新建一个结点
tree* newnode(int data) {
tree* root = new tree;
root->lchild = root->rchild = NULL;
root->data = data;
root->height = 1; // 新插入结点树高必为1
return root;
}
// 更新树高
int updateheight(tree* &root) {
if (root == NULL) {
return 0;
} else {
root->height = max(updateheight(root->lchild), updateheight(root->rchild)) + 1;
}
}
// 获得树高
int getheight(tree* &root) {
if (root == NULL) {
return 0;
} else {
return root->height;
}
}
// 获得平衡因子
int getbalancefactor(tree* &root) {
return getheight(root->lchild) - getheight(root->rchild);
}
// 左旋
void L(tree* &root) {
tree* temp = root->rchild;
root->rchild = temp->lchild;
temp->lchild = root;
updateheight(root);
updateheight(temp);
root = temp;
}
// 右旋
void R(tree* &root) {
tree* temp = root->lchild;
root->lchild = temp->rchild;
temp->rchild = root;
updateheight(root);
updateheight(temp);
root = temp;
}
// 在二叉查找树的基础上,增加调整平衡操作
void insert(tree* &root, int target) {
if (root == NULL) {
root = newnode(target);
return;
}
if (target < root->data) {
insert(root->lchild, target);
updateheight(root);
if (getbalancefactor(root) == 2) {
if (getbalancefactor(root->lchild) == 1) { // LL型
R(root); // 右旋
} else if (getbalancefactor(root->lchild) == -1) { // LR型
L(root->lchild); // 左旋
R(root); // 右旋
}
}
} else if (target >= root->data) {
insert(root->rchild, target);
updateheight(root);
if (getbalancefactor(root) == -2) {
if (getbalancefactor(root->rchild) == 1) { // RL型
R(root->rchild); // 右旋
L(root); // 左旋
} else if (getbalancefactor(root->rchild) == -1) { // RR型
L(root); // 左旋
}
}
}
}
// 层序遍历
void levelorder(tree* &root) {
queue<tree*> q;
vector<int> order;
if (root == NULL) {
cout << "The tree is empty!";
return;
} else {
q.push(root);
}
while (!q.empty()) {
tree* top = q.front();
order.push_back(top->data);
printf("%d: %d\n", top->data, top->height);
q.pop();
if (top->lchild != NULL) {
q.push(top->lchild);
}
if (top->rchild != NULL) {
q.push(top->rchild);
}
}
int cnt = 0;
int Pow = 1;
int level = 1;
printf("lever%d: ", level);
for (int i = 0; i < order.size(); i++) {
cnt++;
printf("%d", order[i]);
if (cnt == Pow) {
Pow = Pow*2 + Pow;
level++;
cout << endl;
if (cnt != order.size()) {
printf("lever%d: ", level);
}
} else {
cout << " ";
}
}
}
// 查找操作
void search(tree* root, int target) {
if (root == NULL) {
cout << "find falied!" << endl;
} else if (target < root->data) {
search(root->lchild, target);
} else if (target > root->data) {
search(root->rchild, target);
} else {
printf("find success!\n");
}
}
int main() {
int n;
cin >> n;
int data[1000];
tree* root = NULL;
// 一个一个结点插入二叉树
for (int i = 0; i < n; i++) {
cin >> data[i];
insert(root, data[i]);
}
// 层序遍历
levelorder(root);
return 0;
}
注:以上内容参考《算法笔记》。