• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            #include <iostream>

            using namespace std;

            typedef 
            int Type;

            struct Node{
                Type key;
                
            int  height;
                Node
            * lchild, *rchild;
                Node(){}
                Node( 
            int k, int h, Node* l, Node* r ):
                    key(k), height(h), lchild(l), rchild(r) {}
            };

            class AVLTree{
                
            private:
                    Node
            * _null;    //   規(guī)定的空結(jié)點(diǎn)
                    Node* root;     //   根結(jié)點(diǎn) 
                    
                    
            void left_single_rotate( Node*& r );     //  左單旋轉(zhuǎn) 
                    void left_double_rotate( Node*& r );     //  左雙旋轉(zhuǎn) 
                    void right_single_rotate( Node*& r );    //  右單旋轉(zhuǎn) 
                    void right_double_rotate( Node*& r );    //  右雙旋轉(zhuǎn) 
                    
                    
            void erase_rotate_help( Node*& r );      //  刪除操作中的旋轉(zhuǎn) 
                    
                    
            void travel( int, Node* root );          //  遍歷樹(shù) 
                    
                    
            void insert_help( Node*& r, Type key );  //  插入 
                    void erase_help ( Node*& r, Type key );  //  刪除 
                
                
            public:
                    AVLTree(){ _null
            = new Node( 00, NULL, NULL ); root= _null; }
                    
                    
            void travel();                           //  遍歷樹(shù) 
                    void insert( Type key );                 //  插入數(shù)據(jù) 
                    void erase ( Type key );                 //  刪除數(shù)據(jù) 
            };


            //   左單旋轉(zhuǎn)實(shí)現(xiàn) 
            void AVLTree::left_single_rotate( Node*& r ){
                Node
            * tmp= r->rchild;
                r
            ->rchild= tmp->lchild; 
                r
            ->height= max( r->lchild->height, r->rchild->height )+ 1;
                
                tmp
            ->lchild= r; r= tmp;
                r
            ->height= max( r->lchild->height, r->rchild->height )+ 1;
            }

            //  右單旋轉(zhuǎn)實(shí)現(xiàn) 
            void AVLTree::right_single_rotate( Node*& r ){
                Node
            * tmp= r->lchild;
                r
            ->lchild= tmp->rchild;
                r
            ->height= max( r->lchild->height, r->rchild->height )+ 1;
                
                tmp
            ->rchild= r; r= tmp;
                r
            ->height= max( r->lchild->height, r->rchild->height )+ 1;
            }

            //  左雙旋轉(zhuǎn)實(shí)現(xiàn)
            void AVLTree::left_double_rotate( Node*& r ){
                right_single_rotate( r
            ->rchild );
                left_single_rotate( r );
            }

            //  右雙旋轉(zhuǎn)實(shí)現(xiàn)
            void AVLTree::right_double_rotate( Node*& r ){
                left_single_rotate( r
            ->lchild );
                right_single_rotate( r );
            }

            void AVLTree::travel( int parent, Node* r ){
                
            if( r!= _null ){
                    travel( r
            ->key, r->lchild );
                    cout 
            << parent << ' ' << r->key << ' ' << r->height << endl;
                    travel( r
            ->key, r->rchild ); }
            }

            void AVLTree::travel(){
                travel( 
            -1, root ); }

            void AVLTree::insert_help( Node*& r, Type key ){
                
            if( r== _null ){
                    r
            = new Node( key, 1, _null, _null ); 
                    
            return;  }
                    
                
            if( key< r->key )  insert_help( r->lchild, key );
                
            else               insert_help( r->rchild, key );
                
                
            //  這上面一部分同二叉查找樹(shù)的代碼 ,AVL樹(shù)只是在其基礎(chǔ)上
                
            //  增加了旋轉(zhuǎn)保持平衡的操作 
                
                
                
            //  調(diào)整根結(jié)點(diǎn)的高度,因?yàn)樵黾咏Y(jié)點(diǎn)后,高度可能增加 
                r->height= max( r->lchild->height, r->rchild->height )+ 1;
                
                
            //  如果右子樹(shù)不為空 
                if( r->rchild!= _null ){
                    
            // 左子樹(shù)的高度加上1等于右子樹(shù)的右子樹(shù)的高度,則左單旋轉(zhuǎn)
                    if( r->lchild->height+ 1== r->rchild->rchild->height )
                    left_single_rotate( r );
                    
            // 左子樹(shù)的高度加上1等于右子樹(shù)的左子樹(shù)的高度,則左雙旋轉(zhuǎn)
                    else if( r->lchild->height+ 1== r->rchild->lchild->height )
                    left_double_rotate( r );
                }
                
            //  如果左子樹(shù)不為空 
                if( r->lchild!= _null ){
                    
            // 右子樹(shù)的高度加上1等于左子樹(shù)的左子樹(shù)的高度,則右單旋轉(zhuǎn)
                    if( r->rchild->height+ 1== r->lchild->lchild->height )
                    right_single_rotate( r );
                    
            // 右子樹(shù)的高度加上1等于左子樹(shù)的右子樹(shù)的高度,則右雙旋轉(zhuǎn)
                    else if( r->rchild->height+ 1== r->lchild->rchild->height )
                    right_double_rotate( r );
                }
            }

            void AVLTree::insert( Type key ){
                insert_help( root, key ); }
                
            void AVLTree::erase_rotate_help( Node*& r ){
                
            //  右子樹(shù)高度比左子樹(shù)高度大2
                if( r->rchild->height- r->lchild->height== 2  ){
                    
            // 如果右子樹(shù)的左子樹(shù)的高度不大于右子樹(shù)的右子樹(shù)的高度則對(duì)根左單旋轉(zhuǎn)
                    if(    r->rchild->lchild->height<= r->rchild->rchild->height )
                    left_single_rotate( r );
                    
            // 如果右子樹(shù)的左子樹(shù)的高度大于右子樹(shù)的右子樹(shù)的高度則對(duì)根左雙旋轉(zhuǎn)
                    else 
                    left_double_rotate( r );
                }
                
            //  左子樹(shù)高度比右子樹(shù)高度大2 
                else if( r->lchild->height- r->rchild->height== 2  ){
                    
            //  如果左子樹(shù)的右子樹(shù)的高度不大于左子樹(shù)的左子樹(shù)的高度則對(duì)根右單旋轉(zhuǎn)
                    if( r->lchild->rchild->height<= r->lchild->lchild->height )
                    right_single_rotate( r );
                    
            //  如果左子樹(shù)的右子樹(shù)的高度大于左子樹(shù)的左子樹(shù)的高度則對(duì)根右雙旋轉(zhuǎn)
                    else 
                    right_double_rotate( r );
                }
            }
                
            void AVLTree::erase_help( Node*& r, Type key ){
                
            if( r== _null ) return;  //  沒(méi)有找到要?jiǎng)h除的結(jié)點(diǎn)則直接退出 
                
                
            if( r->key== key ){
                    
            if( r->rchild== _null ){
                        Node
            * tmp= r;
                        r
            = r->lchild; delete tmp;
                    }
                    
            else {
                        Node
            * tmp= r->rchild;
                        
            while( tmp->lchild!= _null ) tmp= tmp->lchild;
                        
                        r
            ->key= tmp->key;
                        erase_help( r
            ->rchild, tmp->key );
                        r
            ->height= max( r->lchild->height, r->rchild->height )+ 1;
                    }
                    
            return ;  }
                
                
            if( key< r->key ) erase_help( r->lchild, key );
                
            else              erase_help( r->rchild, key );

                
            //  同樣,這上面一部分同二叉查找樹(shù)的代碼 ,AVL樹(shù)只是在其基礎(chǔ)上
                
            //  增加了旋轉(zhuǎn)保持平衡的操作 
                
                
            //  調(diào)整高度 
                r->height= max( r->lchild->height, r->rchild->height )+ 1;
                
                
            //  有可能在左子樹(shù)中把結(jié)點(diǎn)刪除導(dǎo)致左子樹(shù)不平衡
                
            //  故先檢測(cè)左子樹(shù)是否已經(jīng)平衡,否 則調(diào)整 
                if( r->lchild!= _null ) erase_rotate_help( r->lchild );
                
                
            //  同樣右子樹(shù)中也有可能不平衡,檢測(cè)右子樹(shù)并調(diào)整 
                if( r->rchild!= _null ) erase_rotate_help( r->rchild );
                
                
            //  檢測(cè)根并調(diào)整 
                erase_rotate_help( r );
            }

            void AVLTree::erase( Type key ){
                erase_help( root, key ); }

            int main(){
                AVLTree test;
                freopen(
            "a.txt","w",stdout);
                
            forint i= 0; i< 100000++i )
                
            if( i& 1 ) test.insert( rand() );
                
            else       test.erase ( rand() );
                
                test.travel();

                
            return 0;
            }

            posted on 2009-05-07 22:03 Darren 閱讀(2612) 評(píng)論(6)  編輯 收藏 引用

            評(píng)論:
            # re: AVL樹(shù)的插入和刪除操作 2009-05-10 23:53 | 打醬油
            傳說(shuō)中的手寫(xiě)AVL樹(shù)?Orz。。。  回復(fù)  更多評(píng)論
              
            # re: AVL樹(shù)的插入和刪除操作 2009-05-22 11:42 | superlong
            拿走當(dāng)模板  回復(fù)  更多評(píng)論
              
            # re: AVL樹(shù)的插入和刪除操作 2009-05-22 12:02 | Darren
            @superlong
            只寫(xiě)了個(gè)插入和刪除操作
            估計(jì)實(shí)際應(yīng)用還得增加些其它函數(shù)
              回復(fù)  更多評(píng)論
              
            # re: AVL樹(shù)的插入和刪除操作 2009-07-30 13:13 | 打醬油
            再給個(gè)返回最接近的值的操作吧。。。  回復(fù)  更多評(píng)論
              
            # re: AVL樹(shù)的插入和刪除操作[未登錄](méi) 2012-02-28 10:53 | 小強(qiáng)
            代碼有錯(cuò),erase_help操作導(dǎo)致右兒子指針可能指向一個(gè)沒(méi)有分配存儲(chǔ)的地址  回復(fù)  更多評(píng)論
              
            # re: AVL樹(shù)的插入和刪除操作 2013-12-11 23:28 | jasonkent27@163.com
            樓主,請(qǐng)問(wèn)一下,你的程序沒(méi)有報(bào)空指針異常嗎?
            程序中你這樣寫(xiě):
            if( r->rchild!= _null ){

            if( r->lchild->height+ 1== r->rchild->rchild->height )
            left_single_rotate( r );

            else if( r->lchild->height+ 1== r->rchild->lchild->height )
            left_double_rotate( r );
            }

            有沒(méi)有想過(guò)r->lchild為空 或者r->rchild-lchild為空的情況,為什么你的程序忽略的這些判斷也可以運(yùn)行而不報(bào)錯(cuò)?

            非常開(kāi)心能和樓主交流!
            by jasonkent27@163.com  回復(fù)  更多評(píng)論
              

            只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            日日躁夜夜躁狠狠久久AV| 久久国产乱子精品免费女| 亚洲а∨天堂久久精品| 伊人 久久 精品| 91精品国产9l久久久久| 久久久噜噜噜久久中文字幕色伊伊| 亚洲精品tv久久久久| 精品久久久久久无码中文字幕一区| 国产精品99久久久久久猫咪 | 国产精品久久99| 人妻中文久久久久| 久久免费高清视频| 久久久噜噜噜久久中文字幕色伊伊| 99久久免费国产精精品| 尹人香蕉久久99天天拍| 国产精品99久久久久久董美香| 久久精品国产亚洲AV麻豆网站| 午夜视频久久久久一区| 99久久精品国内| 国产精品久久波多野结衣| 亚洲国产视频久久| 日韩欧美亚洲综合久久影院Ds| 2022年国产精品久久久久| 久久久久免费看成人影片| 久久婷婷色香五月综合激情| 国产精品久久久99| 99久久99久久精品国产片| 九九99精品久久久久久| 久久99国产综合精品| 亚洲精品国产字幕久久不卡| 久久AV无码精品人妻糸列| 久久人人青草97香蕉| 精品国产日韩久久亚洲| 亚洲国产精品狼友中文久久久 | 亚洲一区中文字幕久久| 久久人与动人物a级毛片| 久久91精品久久91综合| 久久国产成人精品麻豆| 一级做a爰片久久毛片人呢| 99久久99久久精品国产| 久久亚洲国产成人影院网站|