有了上一篇的二级指针优化,平衡二叉树的左旋,右旋,左平衡和右平衡就会简单一些。强烈建议你先看一下:
http://blog.csdn.net/wsrspirit/article/details/51374117
目前代码只有创建,删除还没有加入。没有注释,不好意思。你可以找其他的博客了解~C实现
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LH 1
#define EH 0
#define RH -1
typedef struct tree_struct
{
int value;
int balance;
struct tree_struct *lchild;
struct tree_struct *rchild;
}tree_t;
void for_each(tree_t *bs_tree)
{
if (bs_tree != NULL)
{
printf("value %d balance %d\n", bs_tree->value,bs_tree->balance);
for_each(bs_tree->lchild);
for_each(bs_tree->rchild);
}
}
void right_rotate(tree_t **root)
{
tree_t *lchild = (*root)->lchild;
(*root)->lchild = lchild->rchild;
lchild->rchild = (*root);
(*root) = lchild;
}
void left_rotate(tree_t **root)
{
tree_t *rchild = (*root)->rchild;
(*root)->rchild = rchild->lchild;
rchild->lchild = (*root);
(*root) = rchild;
}
void left_balance(tree_t **root)
{
tree_t *lchild = (*root)->lchild;
tree_t *lr = lchild->rchild;
switch(lchild->balance)
{
case LH:
(*root)->balance = lchild->balance = EH;
right_rotate(root);
break;
case RH:
switch(lr->balance)
{
case LH:
lchild->balance = EH;
(*root)->balance = RH;
break;
case EH:
lchild->balance = (*root)->balance = EH;
break;
case RH:
lchild->balance = LH;
(*root)->balance = EH;
break;
}
lr->balance = EH;
// printf("before left_rotate\n");
// for_each(*root);
left_rotate(&(*root)->lchild); // printf("before right_rotate\n"); // for_each(*root); right_rotate(root); } } void right_balance(tree_t **root) { tree_t *rchild = (*root)->rchild;
tree_t *rl = rchild->lchild;
switch(rchild->balance)
{
case RH:
(*root)->balance = rchild->balance = EH;
left_rotate(root);
break;
case LH:
switch(rl->balance)
{
case RH:
rchild->balance = EH;
(*root)->balance = LH;
break;
case EH:
rchild->balance = (*root)->balance = EH;
break;
case LH:
rchild->balance = RH;
(*root)->balance = EH;
break;
}
rl->balance = EH;
// printf("before left_rotate\n");
// for_each(*root);
right_rotate(&(*root)->rchild); // printf("before right_rotate\n"); // for_each(*root); left_rotate(root); } } int insert_val(tree_t **root,int key,int *taller) { if (!(*root)) { *root = malloc(sizeof(tree_t)); (*root)->balance = EH;
(*root)->lchild = (*root)->rchild = NULL;
(*root)->value = key;
*taller = 1;
return 1;
}
else
{
if ((*root)->value == key) { *taller = 0; return 0; } else { if ((*root)->value > key) { if(insert_val(&(*root)->lchild,key,taller)) { if (*taller) { switch((*root)->balance) { case LH: printf("ll\n"); left_balance(root); *taller = 0; break; case EH: printf("le\n"); (*root)->balance = LH;
*taller = 1;
break;
case RH:
printf("lr\n");
(*root)->balance = EH;
*taller = 0;
break;
}
}
return 1;
}
else
return 0;
}
else
{
if(insert_val(&(*root)->rchild,key,taller)) { if (*taller) { switch((*root)->balance) { case LH: printf("rl\n"); (*root)->balance = EH;
*taller = 0;
break;
case EH:
printf("re\n");
(*root)->balance = RH;
*taller = 1;
break;
case RH:
printf("rr\n");
right_balance(root);
*taller = 0;
break;
}
}
return 1;
}
else
return 0;
}
}
}
}
void init_val(int *a,int n,tree_t **avl_tree)
{
int i = 0;
int taller;
while(i < n)
{
insert_val(avl_tree,a[i],&taller);
i++;
}
}
int main(int argc, char const *argv[])
{
int a[10] = {62,70,20,65,66,80,30,25,87,50};
tree_t *avl_tree;
avl_tree = NULL;
init_val(a,10,&avl_tree);
printf("after\n");
for_each(avl_tree);
return 0;
}