(保留版权,欢迎转载。请注明原始链接:http://blog.csdn.net/markcnsc/article/details/8570233)
1. 图解4种分形
1.1 LL
1.2. RR
1.3. LR
1.4. RL
2. 插入和删除算法
#include <stdio.h>
#include <stdlib.h>
typedef struct _BBST {
struct _BBST *lc, *rc;
int k, bf;
}BBST;
void R_Rotate (BBST** p)
{
BBST* q;
q = (*p)->lc;
(*p)->lc = q->rc;
q->rc = *p;
*p = q;
}
void L_Rotate (BBST** p)
{
BBST* q;
q = (*p)->rc;
(*p)->rc = q->lc;
q->lc = *p;
*p = q;
}
void LeftBalance (BBST** p)
{
BBST *q, *s;
q = (*p)->lc;
switch (q->bf) {
case 1:
q->bf = (*p)->bf = 0;
R_Rotate (p);
break;
case -1:
s = q->rc;
switch (s->bf) {
case 1: q->bf = 0; (*p)->bf = -1; break;
case -1: q->bf = 1; (*p)->bf = 0; break;
case 0: q->bf = (*p)->bf = 0; break;
}
s->bf = 0;
L_Rotate (&(*p)->lc);//Here, &(*p)->lc != &q
R_Rotate (p);
break;
}
}
void RightBalance (BBST** p)
{
BBST *q, *s;
q = (*p)->rc;
switch (q->bf) {
case -1:
q->bf = (*p)->bf = 0;
L_Rotate (p);
break;
case 1:
s = q->lc;
switch (s->bf) {
case -1: (*p)->bf = 1; q->bf = 0; break;
case 1: (*p)->bf = 0; q->bf = -1; break;
case 0: (*p)->bf = q->bf = 0; break;
}
s->bf = 0;
R_Rotate (&(*p)->rc);//Here, &(*p)->rc != &q
L_Rotate (p);
break;
}
}
int InsertAVL (BBST** t, int v)
{
BBST* p = *t;
int c = 1; //-1:Found, 0:Inserted (Height +0), 1: Inserted (Height +1)
if (!p) {
if(!(p = *t = malloc (sizeof(BBST)))) exit (-1);
p->lc = p->rc = 0; p->k = v; p->bf = 0;
return c;
}
if (p->k > v) {
if ((c = InsertAVL (&p->lc, v)) <= 0) return c;
switch (p->bf) {
case -1: p->bf = 0; c = 0; break;
case 0: p->bf = 1; c = 1; break;
case 1: LeftBalance (t); c = 0; break;
}
}
else if (p->k < v) {
if ((c = InsertAVL (&p->rc, v)) <= 0) return c;
switch (p->bf) {
case 1: p->bf = 0; c = 0; break;
case 0: p->bf = -1; c = 1; break;
case -1: RightBalance (t); c = 0; break;
}
}
else { c = -1; }
return c;
}
int DeleteAVL (BBST** t, int v)
{
BBST* p = *t;
int c = 1; //1: Not Found, 0: Removed (Height -0), -1: Removed (Height -1)
if (!p) return c;
if (p->k > v) {
if ((c = DeleteAVL (&p->lc, v)) >= 0) return c;
switch (p->bf) {
case 1: p->bf = 0; c = -1; break;
case 0: p->bf = -1; c = 0; break;
case -1: RightBalance (t); c = 0; break;
}
}
else if (p->k < v) {
if ((c = DeleteAVL (&p->rc, v)) >= 0) return c;
switch (p->bf) {
case -1: p->bf = 0; c = -1; break;
case 0: p->bf = 1; c = 0; break;
case 1: LeftBalance (t); c = 0; break;
}
}
else {
if (!p->lc && !p->rc) {
*t = 0; c = -1;
free (p);
}
else {
if (p->lc)
for (p = p->lc; p->rc; p = p->rc);
else
for (p = p->rc; p->lc; p = p->lc);
v = p->k;
c = DeleteAVL (t, p->k);
(*t)->k = v;
}
}
return c;
}
BBST* Search (BBST *p, int v)
{
while (p && p->k != v) {
printf ("%d =>", p->k);
p = p->k > v ? p->lc : p->rc;
}
printf ("\n");
return p;
}
void Dump (BBST *p)
{
if (!p) return;
Dump (p->lc);
printf ("%d ", p->k);
Dump (p->rc);
return;
}
int main (int argc, char** argv)
{
BBST* t = NULL;
int d[] = {10, 88, 55, 32, 97, 33, 62, 19};
int i, c, n;
for (i = 0; i < sizeof (d)/sizeof(d[0]); i ++) {
InsertAVL (&t, d[i]);
//Search (t, d[i]);
}
for (i = 0, c = 0; i != 101; i = 0, c = 0) {
printf ("input opercode(-1,0,1) and operand:\n");
scanf ("%d,%d", &i, &n);
if (i == 0) c = !!Search (t, n);
else if (i == 1) c = InsertAVL (&t, n);
else if (i == -1) c = DeleteAVL (&t, n);
else if (i == 2) Dump (t);
printf ("i=%d,n=%d: Result=%d\n", i, n, c);
}
return 0;
}