本文首发于我的公众号 Linux云计算网络(id: cloud_dev) ,专注于干货分享,号内有 10T 书籍和视频资源,后台回复 「1024」 即可领取,欢迎大家关注,二维码文末可以扫。

 

 

 

一、AVL树

  AVL树是一种平衡查找树,在前面的两篇文章:二叉搜索树 和 红黑树 中都提到过。由于二叉搜索树在某些特殊情况下是不平衡的(任意一个结点深度过大),因此其一些动态集合操作在最坏情况下的时间复杂度为O(n)。因此提出一些对二叉搜索树效率改进的树结构使最坏时间复杂度降为O(lgn),AVL树和红黑树就是其中的代表,除此之外,还有一些如AA-tree、B-tree、2-3-tree等。使不平衡树变平衡最关键的是找到“平衡条件”,我们已经在前面一篇文章中详述了红黑树的平衡条件是:对节点进行着色,并约束从根节点到任何叶子节点的长度,其中,约定了5条规定,稍显复杂。而AVL树的平衡条件则显得格外简单:只用保证左右子树的高度不超过1即可。

二、AVL树的实现

1、数据结构

节点类:因为需要控制节点的高度,所以高度是一个属性。指针域包括left、right,parent可以包括也可以不要,本文的实现中,我们包括parent。

struct AVLNode {
        AVLNode        *Parent;
        AVLNode        *Left;
        AVLNode        *Right;
        int            m_nHeight;
        int            m_nValue;
    };

2、节点的平衡

当插入新的节点或者删除节点时,会导致树的不平衡,即其中有节点的左右子树的高度相差>1,这个时候就需要调节树使之平衡。可能出现不平衡的情况总共有以下几种:

////////////////////////////////////////

       a   a   a   a
      /    /       \     \
    b     b         b    b
   /       \       /       \
 c         c     c         c
LL         LR    RL        RR 

//////////////////////////////////////

  总共就只会出现这四种情况,对这四种情况的分类很多书上有各自的说法。其中1、4和2、3是对称的,我们用LL、LR、RL、RR来分别表示,要使这几种情况平衡,我们只用做简单的旋转操作就OK了,针对1、4,有的说法是做单旋,有的说法是外旋,而2、3,则做双旋或内旋,不管是哪种说法,其本质是不变的。在我们的实现中,采用单旋和双旋,双旋就是做两次单旋:

//单旋
void _LeftRotation( AVLNode *node );
void _RightRotation( AVLNode *node );

//双旋
void _LeftRightRotation( AVLNode *node );
void _RightLeftRotation( AVLNode *node );

3、平衡的修复

  在插入节点和删除节点的时候,会破坏树的平衡条件,这个时候就需要修复。我们采用尽可能少地改动原有代码的原则来修复,这个原则和红黑树的修复操作是一致的,即插入和删除操作我们依然沿用二叉搜索树的实现,只在后面添加修复的代码即可。

  如何修复?首先,插入和删除会破坏节点的高度,所以,应更新结点的高度;其次,插入和删除破坏了树中某些节点的平衡,所以,应针对上面四种情况分别平衡节点。所以,这里就需要两个函数:一个更新结点高度的函数UpdateHeight( AVLNode *node );一个平衡节点的函数: BalanceNode( AVLNode *node )。

void AVLTree::_UpdateHeight( AVLNode *node )
{
    AVLNode *l = node->Left, *r = node->Right;

    if ( l && r )
        node->m_nHeight = max( l->m_nHeight, r->m_nHeight ) + 1;
    else if ( l )
        node->m_nHeight = l->m_nHeight + 1;
    else if ( r )
        node->m_nHeight = r->m_nHeight + 1;
    else node->m_nHeight = 0;
}

 

 1 //////////////////////////////////////////////////////////////////////////
 2 //        a    a    a    a
 3 //       /   /     \   \
 4 //    b   b       b   b
 5 //   /     \     /     \
 6 //  c       c   c       c
 7 // LL       LR  RL      RR
 8 //////////////////////////////////////////////////////////////////////////
 9 void AVLTree::_BalanceNode( AVLNode *node )
10 {
11     int nBlance = _GetBalanceFactor( node );
12     if ( nBlance > 1 ) { //L
13         //(1)
14         //if ( _GetBalanceFactor( node->Left ) < 0 ) //LR 
15         //    _LeftRightRotation( node ); //双旋
16         //else _RightRotation( node ); //LL //单旋
17 
18         //(2)
19         if ( _GetBalanceFactor( node->Left ) < 0 )
20             _LeftRotation( node->Left );
21         _RightRotation( node );
22     }
23     if ( nBlance < -1 ) { //R
24         if ( _GetBalanceFactor( node ) > 0 ) { //RL
25             _RightRotation( node->Right );
26         }
27         _LeftRotation( node );
28     }
29 }
30 
31 //平衡因子(左右子树的高度差)
32 int AVLTree::_GetBalanceFactor( AVLNode *node )
33 {
34     AVLNode *l = node->Left, *r = node->Right;
35 
36     if ( l && r )
37         return l->m_nHeight - r->m_nHeight;
38     else if ( l )
39         return l->m_nHeight + 1;
40     else if ( r )
41         return -r->m_nHeight - 1;
42     else return 0;
43 }

基本上该注意的点都提到了,下面附上详细代码实现:

 1 #ifndef __AVL_TREE_H_
 2 #define __AVL_TREE_H_
 3 
 4 class AVLTree 
 5 {
 6 private:
 7     struct AVLNode {
 8         AVLNode        *Parent;
 9         AVLNode        *Left;
10         AVLNode        *Right;
11         int            m_nHeight;
12         int            m_nValue;
13     };
14 public:
15     AVLTree(AVLNode *root = NULL):m_pRoot(root) {}
16     ~AVLTree() {
17         _RecursiveDeleteNode(m_pRoot);
18     }
19 
20     bool Search( const int search_value ) const;
21     bool Insert( const int value );
22     bool Delete( const int delete_value );
23 
24     void Print() const;
25 
26 private:
27     void _RecursiveDeleteNode(AVLNode *node) {
28         if ( node ) {
29             _RecursiveDeleteNode( node->Left );
30             _RecursiveDeleteNode( node->Right );
31             delete node;
32         }
33         node = NULL;
34     }
35 
36     void _DeleteNode( AVLNode *delete_node );
37     void _Delete_Transplant( AVLNode *unode, AVLNode *vnode );
38     void _InsertNode( const int insert_value );
39     AVLNode * _SearchNode( AVLNode *node, const int search_value ) const;
40 
41     //单旋
42     void _LeftRotation( AVLNode *node );
43     void _RightRotation( AVLNode *node );
44 
45     //双旋
46     void _LeftRightRotation( AVLNode *node );
47     void _RightLeftRotation( AVLNode *node );
48 
49     AVLNode* Minimum( AVLNode *node );
50 
51     //树高
52     int _Height ( AVLNode *node );
53     void _UpdateHeight( AVLNode *node );
54     //平衡因子
55     int _GetBalanceFactor( AVLNode *node );
56     //平衡失去平衡的节点
57     void _BalanceNode( AVLNode *node );
58 
59     void _Print ( AVLNode *node ) const;
60 
61 private:
62     AVLNode *m_pRoot;
63 
64 };
65 #endif//__AVL_TREE_H_
View Code

相关文章:

  • 2021-09-21
猜你喜欢
  • 2021-05-05
相关资源
相似解决方案