我的AVL树代码实现(欢迎测试)

 

#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->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 == thisreturn 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 
*= 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>? 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[] = {42315,6100241356636770};
    
for (int i=0; ilist[i]; ++i)
        a.insert(ilist[i]), a.scan(), getchar();
    a.sort_and_show();
    getchar();
    
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值