平衡二叉排序树(AVL树)
定义
- 平衡二叉树
- 任意一个节点其左右子树高度不超过1
- 二叉排序树插入、删除、查找平均时间复杂度O(logn)
二叉排序树 | 插入 | 删除 | 查找 |
---|
平均时间复杂度 | O(logn) | O(logn) | O(logn) |
最差时间复杂度 | O(n) | O(n) | O(n) |
- 当二叉排序树的节点只有左子树或者只有右子树,二叉排序树退化为单链表
- 为了避免二叉排序树退化为单链表,所以要求二叉树平衡的(太苛刻了)所以在实际应用中都是用红黑树来取代(大致平衡) 查找的效率都能维持在O(logn)
- AVL 插入和二叉排序树的插入是一样的,如果插入之后失去平衡,就需要修复
- AVL 删除和二叉排序树的删除是一样的,如果删除之后失去平衡,就需要修复
平衡二叉树排序树修复
- LL 左子树高于右子树 并且 左子树的左儿子高于右儿子
- LR 左子树高于右子树 并且 左子树的左儿子低于右儿子
- RR 左子树低于右子树 并且 右子树的左儿子低于右儿子
- RL 左子树低于右子树 并且 右子树的左儿子高于右儿子
二叉平衡树实现
结构体定义
#define SUCCESS 0
#define FAILURE -1
typedef int ElemType;
struct AVLNode {
ElemType elem;
struct AVLNode *lchild, *rchild;
size_t height;
};
主要功能声明
int insert_avltree(AVLTree *ptree, ElemType elem);
int remove_avltree(AVLTree *ptree, ElemType elem);
AVL树基本操作
struct AVLNode* LL_rotation(struct AVLNode *node);
struct AVLNode* RR_rotation(struct AVLNode *node);
struct AVLNode* LR_rotation(struct AVLNode *node);
struct AVLNode* RL_rotation(struct AVLNode *node);
static void avltree_repaire(AVLTree *ptree)
宏函数
#define HEIGHT(node) (((node) == NULL) ? 0 : (node)->height)
#define BAANCE(node) ((HEIGHT((node)->lchild)) - (HEIGHT((node)->rchild)))
#define REHEIGHT(node) (HEIGHT((node)->lchild) > HEIGHT((node)->rchild) ? \
(HEIGHT((node)->lchild) + 1) : (HEIGHT((node)->rchild) + 1))
功能和操作实现
右旋 LL_rotation
struct AVLNode* LL_rotation(struct AVLNode *node)
{
struct AVLNode *left = node->lchild;
node->lchild = left->rchild;
left->rchild = node;
node->height = REHEIGHT(node);
return left;
}
左旋 RR_rotation
struct AVLNode* RR_rotation(struct AVLNode *node)
{
struct AVLNode *right = node->rchild;
node->rchild = right->lchild;
right->lchild = node;
node->height = REHEIGHT(node);
return right;
}
LR LR_rotation
struct AVLNode* LR_rotation(struct AVLNode *node)
{
node->lchild = RR_rotation(node->lchild);
return LL_rotation(node);
}
RL RL_rotation
struct AVLNode* RL_rotation(struct AVLNode *node)
{
node->rchild = LL_rotation(node->rchild);
return RR_rotation(node);
}
对*ptree节点进行修复 avltree_repaire
static void avltree_repaire(AVLTree *ptree)
{
int lh = HEIGHT((*ptree)->lchild);
int rh = HEIGHT((*ptree)->rchild);
if (lh - rh == 2) {
struct AVLNode *left = (*ptree)->lchild;
if (HEIGHT(left->lchild) > HEIGHT(left->rchild)) {
*ptree = LL_rotation(*ptree);
} else {
*ptree = LR_rotation(*ptree);
}
} else if (rh - lh == 2) {
struct AVLNode *right = (*ptree)->rchild;
if (HEIGHT(right->rchild) > HEIGHT(right->lchild)) {
*ptree = RR_rotation(*ptree);
} else {
*ptree = RL_rotation(*ptree);
}
}
(*ptree)->height = REHEIGHT(*ptree);
}
插入元素 insert_avltree
int insert_avltree(AVLTree *ptree, ElemType elem)
{
assert(ptree != NULL);
if (*ptree == NULL) {
struct AVLNode *node = (struct AVLNode*)malloc(sizeof(struct AVLNode));
if (node == NULL) {
return FAILURE;
}
node->elem = elem;
node->lchild = node->rchild = NULL;
node->height = 1;
*ptree = node;
return SUCCESS;
}
int ret = FAILURE;
if (elem < (*ptree)->elem) {
ret = insert_avltree(&(*ptree)->lchild, elem);
} else if ((*ptree)->elem < elem) {
ret = insert_avltree(&(*ptree)->rchild, elem);
}
if (ret == SUCCESS) {
avltree_repaire(ptree);
}
return ret;
}
删除元素 remove_avltree
int remove_avltree(AVLTree *ptree, ElemType elem)
{
assert(ptree != NULL);
if (*ptree == NULL) {
return FAILURE;
}
int ret = FAILURE;
if (elem < (*ptree)->elem) {
ret = remove_avltree(&(*ptree)->lchild, elem);
} else if ((*ptree)->elem < elem) {
ret = remove_avltree(&(*ptree)->rchild, elem);
} else {
struct AVLNode *node = *ptree;
if (node->lchild != NULL && node->rchild != NULL) {
for (node = node->lchild; node->rchild != NULL; node = node->rchild);
(*ptree)->elem = node->elem;
ret = remove_avltree(&(*ptree)->lchild, node->elem);
} else {
*ptree = node->lchild != NULL ? node->lchild : node->rchild;
free(node);
return SUCCESS;
}
}
if (ret == SUCCESS) {
avltree_repaire(ptree);
}
return ret;
}