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 == 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 
*= 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
...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的AVL树的Python实现代码示例: ```python # 定义AVL树节点类 class AVLNode: def __init__(self, key): self.key = key self.left = None self.right = None self.height = 1 # AVL树类 class AVLTree: def __init__(self): self.root = None # 获取节点的高度 def get_height(self, node): if node is None: return 0 return node.height # 获取节点的平衡因子 def get_balance(self, node): if node is None: return 0 return self.get_height(node.left) - self.get_height(node.right) # 更新节点的高度 def update_height(self, node): if node is None: return node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 # 执行左旋转操作 def left_rotate(self, z): y = z.right T2 = y.left y.left = z z.right = T2 self.update_height(z) self.update_height(y) return y # 执行右旋转操作 def right_rotate(self, z): y = z.left T3 = y.right y.right = z z.left = T3 self.update_height(z) self.update_height(y) return y # 插入节点 def insert(self, key): self.root = self._insert(self.root, key) def _insert(self, root, key): if root is None: return AVLNode(key) elif key < root.key: root.left = self._insert(root.left, key) else: root.right = self._insert(root.right, key) self.update_height(root) balance = self.get_balance(root) # 左旋 if balance > 1 and key < root.left.key: return self.right_rotate(root) # 右旋 if balance < -1 and key > root.right.key: return self.left_rotate(root) # 左右旋 if balance > 1 and key > root.left.key: root.left = self.left_rotate(root.left) return self.right_rotate(root) # 右左旋 if balance < -1 and key < root.right.key: root.right = self.right_rotate(root.right) return self.left_rotate(root) return root # 中序遍历 def inorder_traversal(self): self._inorder_traversal(self.root) def _inorder_traversal(self, root): if root: self._inorder_traversal(root.left) print(root.key, end=" ") self._inorder_traversal(root.right) ``` 这只是一个简单的AVL树实现,还可以根据需要进一步完善和扩展。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值