#include
<
iostream
>
using
namespace
std;
![]()
template
<
typename T
>
![]()
class
AVLNode
...
{
public :
![]()
AVLNode() : bal(0), height(0), lch(0), rch(0), par(0) ...{
}
AVLNode(const T& oelm) : bal(0), height(1), elm(oelm),
![]()
lch(0), rch(0), par(0) ...{
}
![]()
~AVLNode() ...{
del(this);
}
![]()
AVLNode* insert(T oelm) .. .{
cout << "插入 " << oelm << endl;
// 树还未创建第一个结点时直接赋值内部成员
![]()
if (0 == height) ...{
++height;
elm = oelm;
return this;
} // 以上if语句保证以下任何函数或操作树非空
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->pa r;
![]()
if (B->par) .. .{
if (B->par ->lch == A)
B->par ->lch = B;
else B->pa r->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 th is;
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->r ch;
// D必然存在, 不必判别存在性
D->par = A->pa r;
![]()
if (D->par) .. .{
if (D->par ->lch == A)
D->par ->lch = D;
else D->pa r->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 th is;
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->r ch;
C->par = A->pa r;
![]()
if (C->par) .. .{
if (C->par ->lch == A)
C->par ->lch = C;
else C->pa r->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 th is;
break;
case RR:
cout << "RR 旋转 " << endl;
A = anc, B = A ->rch, C = B->rch;
D = A->lch, E = B->lch;
B->par = A->pa r;
![]()
if (B->par) .. .{
if (B->par ->lch == A)
B->par ->lch = B;
else B->pa r->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 th is;
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->l ch;
else temp = temp-> rch;
}
if (p->par->elm > p->e lm)
p->par->lch = p;
else p->par->rch = p;
return p;
}
![]()
int update(AVLNode* node, AVLNode **anc) ...{
![]()
while (node->par && !n ode->par->bal) ...{
if
...
AVL代码
最新推荐文章于 2024-02-25 21:59:14 发布