大二的时候数据结构课死活没看懂的一个东东,看了2小时,敲了2小时,调了2小时。。。

平衡树某一节点的左右子树高度相差大于1的时候即需要调整,调整可分为四中情况 ll,rr,lr,rl其中lr,rl是由不同顺序的ll,rr来实现的,代码比想象的简短。

一棵平衡的树只有在插入和删除节点的时候,才会变的不平衡,所以掌握好时机,判断平衡是否破坏及不平衡的种类,再纠正即可

代码如下。。

avl.h

struct AVLNode {
struct AVLNode *left;
struct AVLNode *right;
int data;
int height;
}; static const int AVL_STATUS_OK = 0;
static const int AVL_STATUS_FOUNDED = 1;
static const int AVL_STATUS_NOTFOUNDED = 2;
static const int AVL_STATUS_FAILED = -1; int avl_insert(struct AVLNode **root, int data);
int avl_find(struct AVLNode *root, int data);
int avl_del(struct AVLNode **root, int data);
int avl_del_tree(struct AVLNode *root);
void print_avl_tree(struct AVLNode *root);
int _get_height(struct AVLNode *node);
int _max(int a, int b);
void _single_rotate_left(struct AVLNode **node);
void _single_rotate_right(struct AVLNode **node);
void _double_rotate_left_right(struct AVLNode **node);
void _double_rotate_right_left(struct AVLNode **node);

 

avl.c

#include <stdio.h>
#include <stdlib.h>
#include <assert.h> #include "avl.h" int avl_insert(struct AVLNode **ptr_root, int data) {
int ret;
if(NULL == *ptr_root) {
*ptr_root = malloc(sizeof(struct AVLNode));
if(NULL == *ptr_root) {
return AVL_STATUS_FAILED;
}
(*ptr_root)->data = data;
(*ptr_root)->left = NULL;
(*ptr_root)->right = NULL;
(*ptr_root)->height = 0;
return AVL_STATUS_OK;
} if(data == (*ptr_root)->data) {
return AVL_STATUS_OK;
}
else if(data < (*ptr_root)->data) {
ret = avl_insert(&((*ptr_root)->left), data);
if(_get_height((*ptr_root)->left) - _get_height((*ptr_root)->right) > 1){
if(data < (*ptr_root)->left->data){
_single_rotate_left(ptr_root);
}
else {
_double_rotate_left_right(ptr_root);
}
}
}
else {
ret = avl_insert(&((*ptr_root)->right), data);
if(_get_height((*ptr_root)->right) - _get_height((*ptr_root)->left) > 1){
if(data > (*ptr_root)->right->data) {
_single_rotate_right(ptr_root);
}
else {
_double_rotate_right_left(ptr_root);
}
}
}
(*ptr_root)->height = _max(_get_height((*ptr_root)->left), _get_height((*ptr_root)->right)) + 1;
return ret;
} int avl_find(struct AVLNode *root, int data) {
if(NULL == root) {
return AVL_STATUS_NOTFOUNDED;
}
if(data == root->data) {
return AVL_STATUS_FOUNDED;
}
if(data < root->data) {
return avl_find(root->left, data);
}
else {
return avl_find(root->right, data);
}
} int avl_del(struct AVLNode **root, int data) {
struct AVLNode *t;
if(*root == NULL) {
return AVL_STATUS_OK;
}
if(data < (*root)->data) {
avl_del(&((*root)->left), data);
if(_get_height((*root)->right) - _get_height((*root)->left) > 1){
if((*root)->right->left != NULL && (_get_height((*root)->right->left) > _get_height((*root)->right->right))) {
_double_rotate_right_left(root);
}
else {
_single_rotate_right(root);
}
}
}
else if(data > (*root)->data) {
avl_del(&((*root)->right), data);
if(_get_height((*root)->left) - _get_height((*root)->right) > 1){
if((*root)->left->right != NULL && (_get_height((*root)->left->right) > _get_height((*root)->left->left))) {
_double_rotate_left_right(root);
}
else {
_single_rotate_left(root);
}
}
}
else {
if(NULL != (*root)->left && NULL != (*root)->right) {
t = (*root) -> right;
while(t->left != NULL) {
t = t->left;
}
(*root) -> data = t->data;
avl_del(&((*root)->right), t->data);
if(_get_height((*root)->left) - _get_height((*root)->right) > 1){
if((*root)->left->right != NULL && (_get_height((*root)->left->right) > _get_height((*root)->left->left))) {
_double_rotate_left_right(root);
}
else {
_single_rotate_left(root);
}
}
}
else {
t = *root;
if((*root)->left == NULL) {
*root = (*root) -> right;
}
else if((*root)->right == NULL) {
*root = (*root) -> left;
}
free(t);
}
}
return AVL_STATUS_OK;
} int avl_del_tree(struct AVLNode *root) {
if(NULL == root) {
return AVL_STATUS_OK;
}
avl_del_tree(root->left);
avl_del_tree(root->right);
free(root);
return AVL_STATUS_OK;
} int _get_height(struct AVLNode *node) {
if(NULL == node){
return -1;
}
else{
return node->height;
}
} int _max(int a, int b) {
return a > b ? a:b;
} void _single_rotate_left(struct AVLNode **node) {
struct AVLNode* root = *node;
*node = root->left;
root->left = (*node)->right;
(*node)->right = root;
root->height = _max(_get_height(root->left), _get_height(root->right)) + 1;
(*node)->height = _max(_get_height((*node)->left), _get_height((*node)->right)) + 1;
} void _single_rotate_right(struct AVLNode **node) {
struct AVLNode* root = *node;
*node = root->right;
root->right = (*node)->left;
(*node)->left = root;
root->height = _max(_get_height(root->left), _get_height(root->right)) + 1;
(*node)->height = _max(_get_height((*node)->left), _get_height((*node)->right)) + 1;
} void _double_rotate_left_right(struct AVLNode **node) {
struct AVLNode* root = *node;
_single_rotate_right(&(root->left));
_single_rotate_left(node);
} void _double_rotate_right_left(struct AVLNode **node) {
struct AVLNode* root = *node;
_single_rotate_left(&(root->right));
_single_rotate_right(node);
} void print_avl_tree(struct AVLNode *node) {
if(NULL == node) {
return;
}
print_avl_tree(node->left);
printf("%d\t%d\n", node->data, node->height);
print_avl_tree(node->right);
}

  

main.c

#include <stdio.h>

#include "avl.h"

int main() {
struct AVLNode *root = NULL;
int ret = avl_insert(&root, 7);
printf("hello, world\n");
printf("root:address %ld\n", (long)root);
printf("after:%d\n", 7);
print_avl_tree(root);
avl_insert(&root, 6);
printf("after:%d\n", 6);
print_avl_tree(root);
avl_insert(&root, 5);
printf("after:%d\n", 5);
print_avl_tree(root);
avl_insert(&root, 7);
printf("after:%d\n", 7);
print_avl_tree(root);
avl_insert(&root, 1);
printf("after:%d\n", 1);
print_avl_tree(root);
avl_insert(&root, 0);
printf("after:%d\n", 0);
print_avl_tree(root);
avl_insert(&root, 9);
printf("after:%d\n", 9);
print_avl_tree(root); ret = avl_find(root, 7);
printf("find 7 result is %d\n", ret); ret = avl_find(root, 17);
printf("find 17 result is %d\n", ret); ret = avl_del(&root, 7);
printf("del 7 result is %d\n", ret);
print_avl_tree(root); ret = avl_del(&root, 17);
printf("del 17 result is %d\n", ret);
print_avl_tree(root); avl_del_tree(root);
return 0;
}

  

#gcc -g main.c avl.c -o main

#./main

hello, world
root:address 27951120
after:7
7 0
after:6
6 0
7 1
after:5
5 0
6 1
7 0
after:7
5 0
6 1
7 0
after:1
1 0
5 1
6 2
7 0
after:0
0 0
1 1
5 0
6 2
7 0
after:9
0 0
1 1
5 0
6 2
7 1
9 0
find 7 result is 1
find 17 result is 2
del 7 result is 0
0 0
1 1
5 0
6 2
9 0
del 17 result is 0
0 0
1 1
5 0
6 2
9 0

应该是正确的。

 

[算法] avl树实现的更多相关文章

  1. 数据结构与算法——AVL树类的C++实现

    关于AVL树的简单介绍能够參考:数据结构与算法--AVL树简单介绍 关于二叉搜索树(也称为二叉查找树)能够參考:数据结构与算法--二叉查找树类的C++实现 AVL-tree是一个"加上了额外 ...

  2. 数据结构和算法(Golang实现)(28)查找算法-AVL树

    AVL树 二叉查找树的树高度影响了查找的效率,需要尽量减小树的高度,AVL树正是这样的树. 一.AVL树介绍 AVL树是一棵严格自平衡的二叉查找树,1962年,发明者Adelson-Velsky和La ...

  3. [数据结构与算法] : AVL树

    头文件 typedef int ElementType; #ifndef _AVLTREE_H_ #define _AVLTREE_H_ struct AvlNode; typedef struct ...

  4. 数据结构和算法(Golang实现)(29)查找算法-2-3树和左倾红黑树

    某些教程不区分普通红黑树和左倾红黑树的区别,直接将左倾红黑树拿来教学,并且称其为红黑树,因为左倾红黑树与普通的红黑树相比,实现起来较为简单,容易教学.在这里,我们区分开左倾红黑树和普通红黑树. 红黑树 ...

  5. 算法与数据结构(十一) 平衡二叉树(AVL树)

    今天的博客是在上一篇博客的基础上进行的延伸.上一篇博客我们主要聊了二叉排序树,详情请戳<二叉排序树的查找.插入与删除>.本篇博客我们就在二叉排序树的基础上来聊聊平衡二叉树,也叫AVL树,A ...

  6. AVL树的平衡算法(JAVA实现)

      1.概念: AVL树本质上还是一个二叉搜索树,不过比二叉搜索树多了一个平衡条件:每个节点的左右子树的高度差不大于1. 二叉树的应用是为了弥补链表的查询效率问题,但是极端情况下,二叉搜索树会无限接近 ...

  7. [算法] 数据结构之AVL树

    1 .基本概念 AVL树的复杂程度真是比二叉搜索树高了整整一个数量级——它的原理并不难弄懂,但要把它用代码实现出来还真的有点费脑筋.下面我们来看看: 1.1  AVL树是什么? AVL树本质上还是一棵 ...

  8. AVL树的算法思路整理

    http://www.cnblogs.com/heqile/archive/2011/11/28/2265713.html 看完了<数据结构与算法分析(C++描述)>的4.4节AVL树,做 ...

  9. 红黑树和AVL树的实现与比较-----算法导论

    一.问题描述 实现3种树中的两种:红黑树,AVL树,Treap树 二.算法原理 (1)红黑树 红黑树是一种二叉查找树,但在每个结点上增加一个存储位表示结点的颜色,可以是red或black.红黑树满足以 ...

随机推荐

  1. Nginx配置IP白名单和黑名单

    白名单设置,访问根目录 location / { allow 123.34.22.155; allow ; deny all; } 黑名单设置,访问根目录 location / { deny 123. ...

  2. 多项目中SVN权限管理精辟解析

    本节和大家讨论一下多项目SVN权限管理,主要包括建立版本库,修改版本库配置文件,配置允许访问的用户,设置用户访问权限.下面我们就来看一下SVN权限管理.svn权限管理svn的权限管理涉及到一下文件:p ...

  3. hosts.deny 和hosts.allow 配置不生效

    需求:需要用hosts.deny限制用户通过ssh登录 在/etc/hosts.deny中加入 sshd: all 在/etc/hosts.allow中加入 sshd:all #拒绝所有的ip链接ss ...

  4. 无线手柄+步进电机——控制方向

    今天测试了一下无线手柄控制电机转向的改变 1: #include <PS2X_lib.h> //for v1.6 2: #include <Stepper.h> 3:  4: ...

  5. combo扩展:禁止手工改变输入框的值

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  6. ntp-keygen.c

    这个程序产生加密数据文件使用的的密码,遵循Autokey security protocol和NTPv4.文件名被名字和创建时间组成的头部当做前缀,后面跟有一个类型定义的描述符标签和PEM加密的数据结 ...

  7. java int和Integer的区别

    今天偶然和同学讨论到int和Integer的区别是,发现自己对这个问题了解的并不是很清楚,而且有些概念还是错的,所以在这对int和Integer的区别做一个总结. int与integer的区别从大的方 ...

  8. android5.0----SVG

    SVG ----scalable vector Graphics 可缩放矢量图形 android L 即android 5.0的新特性. 1,SVG是干什么的? 可缩放矢量图形是基于可扩展标记语言(标 ...

  9. hdu_5738_Eureka(脑洞)

    题目链接:hdu_5738_Eureka 题意: 这题感觉说不清楚,坑点有点坑,一不小心就会推出错误的公式,然后最重要的是你还不知道你推错了 题解: 这里贴一个官方的题解 Eureka xjb推导一下 ...

  10. 参数修饰符ref,out ,params的区别

    参数修饰符ref,out ,params的区别 C#中有三个关键字-ref,out ,params,可是这三个之间的区别你都明白了吗? 那么我们就来认识一下参数修饰符ref,out ,params吧, ...