平衡二叉排序树之 A V L AVL AVL树(C语言实现)
A V L AVL AVL树性质
A V L AVL AVL树就是为了解决二叉排序树查找性能退化的问题
A V L AVL AVL树本质上也是二叉排序树,所以具备二叉排序树的所有性质
A V L AVL AVL树的学习重点,在于平衡条件以及平衡调整的相关学习
性质:
∣
H
(
l
e
f
t
)
−
H
(
r
i
g
h
t
)
∣
<
=
1
|\ H(left)\ - \ H(right)\ | \ <= \ 1
∣ H(left) − H(right) ∣ <= 1
左子树和右子树的高度差,不超过1
优点:由于对每个节点的左右子树的树高做了限制,所以整棵树不会退化成一个链表
A V L AVL AVL树,改进的不是上限,而是下限
调整失衡操作
A V L AVL AVL树的插入和删除操作其实和二叉排序树的插入和删除操作是一样的,但是插入或删除节点以后可能破坏 A V L AVL AVL树的平衡条件,一旦破坏了平衡条件, A V L AVL AVL树就要触发平衡调整操作
接下来,就给大家介绍两种调整操作
A V L AVL AVL树——左旋:
我们将 K1
作为旋转中心,当我们将 K1
向左旋转的时候,K1
下去了,K3
就上去了,作为左旋以后的根节点,此时 K1
就成为了 K3
的左孩子,可 K3
原来就有左子树,那么 K3
原来的左子树要去哪?K3
的左子树的值,是一定小于 K3
但是大于 K1
,所以我们可以将 K3
的左子树作为 K1
的右子树
左旋后:
A V L AVL AVL树——右旋:
与左旋相同,我们将 K1
作为旋转中心,将 K1
向右旋转,K1
向下,变成 K2
的右子树,K2
向上,K2
原来的右子树,变成 K1
的左子树。右旋和左旋是对称的
右旋后:
左旋和右旋,本质上影响的是调整树高
失衡类型
A V L AVL AVL树一共有四种失衡类型
K1
节点,是我们回溯过程中,遇到的第一个失衡的节点
LL型:左子树的左子树要更高
LR型:左子树的右子树要更高
RL型:右子树的左子树要更高
RR型:右子树的右子树要更高
失衡类型的调整
A V L AVL AVL树——LL型
假设此时 A
的高度为h1
,B
的高度为 h2
,C
的高度为 h3
,D
的高度为 h4
, 那么此时 h1
, h2
, h3
, h4
之间的关系是怎么样的?
既然是LL型失衡,K1
作为失衡节点,说明 K2
的高度要比 K3
的高度多2,K2
的高度为 h1
+ 1,K3
的高度为 max( h3
, h4
) + 1,所以可得以下等式
h
1
=
m
a
x
(
h
3
,
h
4
)
+
2
=
h
2
+
1
h1 = max(h3,\ h4) + 2 = h2 + 1
h1=max(h3, h4)+2=h2+1
当在
A
V
L
AVL
AVL树发生LL型失衡,就一定会有以上等式。只需对失衡节点进行一次大右旋即可;那么对于RR类型,就是对失衡节点进行一次大左旋即可。
A V L AVL AVL树——LR类型
同样,假设此时 A
的高度为h1
,B
的高度为 h2
,C
的高度为 h3
,D
的高度为 h4
, 那么此时 h1
, h2
, h3
, h4
之间的关系是怎么样的?
既然是LR型失衡,那么 K2
的高度应该比 K3
大1,K3
的高度又是 max( h2
, h3
) + 1,K2
的高度又比 h4
高2,K3
比 h1
高1,所以可得以下等式
m
a
x
(
h
2
,
h
3
)
=
h
4
=
h
1
max(h2,\ h3) = h4 = h1
max(h2, h3)=h4=h1
当发生LR型失衡,则一定有以上等式。先抓住失衡节点的左子树(K2
)进行一次小左旋,左旋之后就变成了LL类型的失衡,最后再抓着 K1
进行大右旋即可;RL型失衡,就是对失衡节点的右子树进行小右旋,再对失衡节点进行大左旋。
C语言代码实现 A V L AVL AVL树
- 插入和删除后,注意重新计算树高字段
- 引入NIL节点,代替NULL,NULL不可访问,NIL是一个实际节点,可以访问
#include <stdio.h>
#include <stdlib.h>
#define L(n) (n->l)
#define R(n) (n->r)
#define H(n) (n->h)
typedef struct Node {
int key, h;
struct Node *l, *r;
} Node;
//__attribute__((constructor)):让该函数在main函数之前执行, NIL:虚拟空节点NIL,让访问假的空节点的操作是合法的
Node __NIL;
#define NIL (&__NIL)
__attribute__((constructor))
void init_NIL() {
NIL->key = 0, NIL->h = 0;
NIL->l = NIL->r = NIL;
return ;
}
Node *getNewNode(int val) {
Node *p = (Node *)malloc(sizeof(Node));
p->key = val;
p->h = 1;
p->l = p->r = NIL;
return p;
}
//树高维护
void update_height(Node *root) {
root->h = (H(L(root)) > H(R(root)) ? H(L(root)) : H(R(root))) + 1;
return ;
}
//左旋
Node *left_rotate(Node *root) {
Node *new_root = root->r;
root->r = new_root->l;
new_root->l = root;
update_height(root);
update_height(new_root);
return new_root;
}
//右旋
Node *right_rotate(Node *root) {
Node *new_root = root->l;
root->l = new_root->r;
new_root->r = root;
update_height(root);
update_height(new_root);
return new_root;
}
//销毁树
void clear(Node* root) {
if (root == NIL) return ;
clear(root->l);
clear(root->r);
free(root);
return ;
}
//失衡调整
Node *maintain(Node *root) {
//如果左右子树高度差不超过1,则未失衡,不用调整
if (abs(H(L(root)) - H(R(root))) <= 1) return root;
//如果是左子树发生了失衡
if (root->l->h > root->r->h) {
//如果是左子树的右子树发生了失衡,则是LR型,否则是LL型
if (root->l->l->h < root->l->r->h) {
root->l = left_rotate(root->l);
}
root = right_rotate(root);
} else { //如果是右子树发生了失衡
//如果是右子树的左子树发生失衡,则是RL型,否则是RR型
if (root->r->r->h < root->r->l->h) {
root->r = right_rotate(root->r);
}
root = left_rotate(root);
}
return root;
}
//插入操作
Node *insert(Node *root, int val) {
if (root == NIL) return getNewNode(val);
if (root->key == val) return root;
if (root->key > val) root->l = insert(root->l, val);
else root->r = insert(root->r, val);
//维护树高
update_height(root);
//对在回溯的时候碰到的第一个失衡节点进行失衡调整
return maintain(root);
}
//删除操作
Node *erase(Node *root, int val) {
if (root == NIL) return root;
if (root->key < val) root->r = erase(root->r, val);
else if (root->key > val) root->l = erase(root->l, val);
else {
if (root->l == NIL || root->r == NIL) {
Node *temp = root->l != NIL ? root->l : root->r;
free(root);
return temp;
} else {
Node *temp = root->l;
while (temp->r != NIL) temp = temp->r;
root->key = temp->key;
root->l = erase(root->l, temp->key);
}
}
//维护树高
update_height(root);
//对在回溯的时候碰到的第一个失衡节点进行失衡调整
return maintain(root);
}
void print_node(Node *root) {
printf("[ %d(%d) | %d, %d]\n",
root->key, root->h,
root->l->key,
root->r->key
);
return ;
}
void output(Node *root) {
if (root == NIL) return ;
print_node(root);
output(root->l);
output(root->r);
return ;
}
int main() {
Node *root = NIL;
int val, op;
printf("输入形式: op val\nop == 1代表插入,op == 0代表删除,op == -1结束\n");
while (~scanf("%d%d", &op, &val)) {
if (op == -1) break;
switch(op) {
case 0: {
root = erase(root, val);
printf("erase %d from AVL Tree\n", val);
} break;
case 1: {
root = insert(root, val);
printf("insert %d to AVL Tree\n", val);
} break;
}
output(root);
printf("\n");
}
clear(root);
return 0;
}