



























AVLNode* leaf = setPos(oelm);
AVLNode* anc;
int rotf = update(leaf, &anc);
AVLNode *A, *B, *C, *D, *E, *F, *G;
if (anc)
cout << "对 " << anc->elm << " 做";

switch (rotf) ...{
case LL:
cout << "LL旋转" << endl;
A = anc, B = A->lch, C = B->lch;
D = B->rch, E = A->rch;
B->par = A->par;

if (B->par) ...{
if (B->par->lch == A)
B->par->lch = B;
else B->par->rch = B;
}
A->par = B, B->rch = A;
C->par = B, B->lch = C;
if (D) D->par = A;
A->lch = D;
if (E) E->par = A;
A->rch = E;
// 更改A, B的高度和平衡因子
reset(A);
reset(B);
// 根结点有变化,需改动
if (A == this) return B;
else return this;
break;
case LR:
cout << "LR 旋转" << endl;
A = anc, B = A->lch, C = B->lch;
D = B->rch, E = A->rch, F = D->lch, G = D->rch;
// D必然存在,不必判别存在性
D->par = A->par;

if (D->par) ...{
if (D->par->lch == A)
D->par->lch = D;
else D->par->rch = D;
}
A->par = D, D->rch = A;
B->par = D, D->lch = B;
// F未必存在结点,可能为空
// 以下结点同理
if (F)
F->par = B;
B->rch = F;
if (G)
G->par = A;
A->lch = G;
if (E)
E->par = A;
A->rch = E;
// 更改A, B, D的高度和平衡因子
reset(A), reset(B), reset(D);
// 如果结点A是根,那么根已被D替代
if (A == this)
return D;
else return this;
break;
case RL:
cout << "RL 旋转" << endl;
A = anc, B = A->rch, C = B->lch;
D = A->lch, E = C->lch, F = C->rch, G = B->rch;
C->par = A->par;

if (C->par) ...{
if (C->par->lch == A)
C->par->lch = C;
else C->par->rch = C;
}
C->lch = A, A->par = C;
C->rch = B, B->par = C;
if (E) E->par = A;
A->rch = E;
if (F) F->par = B;
B->lch = F;
// 更改A, B, C的高度和平衡因子
reset(A), reset(B), reset(C);
if (A == this)
return C;
else return this;
break;
case RR:
cout << "RR 旋转" << endl;
A = anc, B = A->rch, C = B->rch;
D = A->lch, E = B->lch;
B->par = A->par;

if (B->par) ...{
if (B->par->lch == A)
B->par->lch = B;
else B->par->rch = B;
}
A->par = B, B->lch = A;
C->par = B, B->rch = C;
if (D) D->par = A;
A->lch = D;
if (E) E->par = A;
A->rch = E;
// 更改A, B的高度和平衡因子
reset(A), reset(B);
if (A == this)
return B;
else return this;
break;
}
return this;
}

void del(AVLNode* node) ...{

if (node) ...{
if (node->lch) delete node->lch;
if (node->rch) delete node->rch;
}
}
AVLNode& remove(T);
// 以下均测试代码

void scan(const AVLNode* node) ...{

if (node) ...{
cout << "元素: " << node->elm;
cout << " 高度: " << node->height;
cout << "平衡因子: " << node->bal;
if (node->lch)
cout << " 左儿子: " << node->lch->elm;
if (node->rch)
cout << " 右儿子: " << node->rch->elm;
cout << endl;
scan(node->lch);
scan(node->rch);
}
}

void sort_and_show(const AVLNode* node) ...{

if (node) ...{
sort_and_show(node->lch);
cout << node->elm << " ";
sort_and_show(node->rch);
}
}
private :
short int bal; // 平衡因子
unsigned int height;
T elm; // 元素
AVLNode *lch, *rch, *par; // 左右子树指针和父亲指针
static const int LL = 0; // 00
static const int LR = 1; // 01
static const int RL = 2; // 10
static const int RR = 3; // 11

AVLNode* setPos(const T& oelm) ...{
AVLNode *temp = this;
AVLNode *p = new AVLNode(oelm);

while (temp) ...{
p->par = temp;
if (p->elm < temp->elm)
temp = temp->lch;
else temp = temp->rch;
}
if (p->par->elm > p->elm)
p->par->lch = p;
else p->par->rch = p;
return p;
}

int update(AVLNode* node, AVLNode **anc) ...{

while (node->par && !node->par->bal) ...{
if (node->par->lch == node)
++(node->par->bal);
else --(node->par->bal);
cout << node->par->elm << "高度增加" << endl;
++(node->par->height);
node = node->par;
}
*anc = node->par;
int rotf = 0;

if (node->par) ...{
if (node->par->lch == node)
++(node->par->bal);
else --(node->par->bal);

if (2 == node->par->bal || -2 == node->par->bal) ...{
cout << node->par->elm << "高度增加" << endl;
++(node->par->height);
if (-1 == node->bal)
rotf += 1;
if (-2 == node->par->bal)
rotf += 2;
return rotf;
}
}
return -1;
}
// 重算结点的平衡因子和高度值(确信左右儿子相应数字正确)

void reset(AVLNode *node) ...{

if (node) ...{
cout << "更新" << node->elm << "高度" << endl;
int l = node->lch ? node->lch->height : 0;
int r = node->rch ? node->rch->height : 0;
node->height = l>r ? l+1 : r+1;
node->bal = l - r;
}
return ;
}
}
;

/**/
/* AVL树类,包裹着AVLNode类 */
template
<
typename T
>

class
AVLTree
...
{
public :

AVLTree() ...{
p_node = new AVLNode<T>;
}

AVLTree(T elm) ...{
p_node = new AVLNode<T>(elm);
}

~AVLTree() ...{
delete p_node;
}

AVLTree& insert(T elm) ...{
// 之所以采取赋值方式
// 是因为旋转后可能根结点被改变了位置,要赋值还原
p_node = p_node->insert(elm);
}

void scan() ...{
p_node->scan(p_node);
}

void sort_and_show() ...{
p_node->sort_and_show(p_node);
}
private :
AVLNode<T>* p_node;
}
;


int
main()
...
{
AVLTree<int> a;

int ilist[] = ...{4, 2, 3, 1, 5,6, 100, 24, 135, 663, 677, 0};
for (int i=0; ilist[i]; ++i)
a.insert(ilist[i]), a.scan(), getchar();
a.sort_and_show();
getchar();
return 0;
}








































































































































































































































































