• <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>

            那誰的技術博客

            感興趣領域:高性能服務器編程,存儲,算法,Linux內(nèi)核
            隨筆 - 210, 文章 - 0, 評論 - 1183, 引用 - 0
            數(shù)據(jù)加載中……

            AVL樹的實現(xiàn)代碼

            /********************************************************************
            created:    
            2007/08/28
            filename:     avltree.c
            author:        Lichuang

            purpose:    AVL樹的實現(xiàn)代碼, 
                        參考資料
            <<數(shù)據(jù)結構與算法分析-C語言描述>>, 作者Allen Weiss
            *********************************************************************/

            #include 
            <stdio.h>
            #include 
            <stdlib.h>
            #include 
            <time.h>

            typedef struct AVLTree
            {
                
            int nData;
                struct AVLTree
            * pLeft;
                struct AVLTree
            * pRight;
                
            int nHeight;
            }AVLTree;

            int Max(int a, int b);
            int Height(AVLTree* pNode);
            AVLTree
            * Insert(int nData, AVLTree* pNode);
            AVLTree
            * SingleRotateWithLeft(AVLTree* pNode);
            AVLTree
            * SingleRotateWithRight(AVLTree* pNode);
            AVLTree
            * DoubleRotateWithLeft(AVLTree* pNode);
            AVLTree
            * DoubleRotateWithRight(AVLTree* pNode);
            void DeleteTree(AVLTree
            ** ppRoot);
            void PrintTree(AVLTree
            * pRoot);

            int main()
            {
                
            int i;
                AVLTree
            * pRoot = NULL;

                srand((unsigned 
            int)::time(NULL));
                
                
            for (i = 0; i < 100000000++i)
                {
                    pRoot 
            = Insert(::rand(), pRoot);
                }

                
            //PrintTree(pRoot);

                DeleteTree(
            &pRoot);

                return 
            0;
            }

            int Max(int a, int b)
            {
                return (a 
            > b ? a : b);
            }

            int Height(AVLTree* pNode)
            {
                
            if (NULL == pNode)
                    return 
            -1;

                return pNode
            ->nHeight;
            }

            AVLTree
            * Insert(int nData, AVLTree* pNode)
            {
                
            if (NULL == pNode)
                {
                    pNode 
            = (AVLTree*)malloc(sizeof(AVLTree));
                    pNode
            ->nData = nData;
                    pNode
            ->nHeight = 0;
                    pNode
            ->pLeft = pNode->pRight = NULL;
                }
                
            else if (nData < pNode->nData)          // 插入到左子樹中
                {
                    pNode
            ->pLeft = Insert(nData, pNode->pLeft);
                    
            if (Height(pNode->pLeft) - Height(pNode->pRight) == 2)    // AVL樹不平衡
                    {
                        
            if (nData < pNode->pLeft->nData)
                        {
                            
            // 插入到了左子樹左邊, 做單旋轉
                            pNode 
            = SingleRotateWithLeft(pNode);
                        }
                        
            else 
                        {
                            
            // 插入到了左子樹右邊, 做雙旋轉
                            pNode 
            = DoubleRotateWithLeft(pNode);
                        }
                    }
                }
                
            else if (nData > pNode->nData)          // 插入到右子樹中
                {
                    pNode
            ->pRight = Insert(nData, pNode->pRight);
                    
            if (Height(pNode->pRight) - Height(pNode->pLeft) == 2)    // AVL樹不平衡
                    {
                        
            if (nData > pNode->pRight->nData)
                        {
                            
            // 插入到了右子樹右邊, 做單旋轉
                            pNode 
            = SingleRotateWithRight(pNode);
                        }
                        
            else 
                        {
                            
            // 插入到了右子樹左邊, 做雙旋轉
                            pNode 
            = DoubleRotateWithRight(pNode);
                        }
                    }
                }

                pNode
            ->nHeight = Max(Height(pNode->pLeft), Height(pNode->pRight)) + 1;

                return pNode;
            }

            /********************************************************************
                  pNode                                pNode
            ->pLeft 
                  
            /                                             \
            pNode
            ->pLeft                      ==>              pNode
                       
            \                                       /
                      pNode
            ->pLeft->pRight                   pNode->pLeft->pRight
            *********************************************************************/
            AVLTree
            * SingleRotateWithLeft(AVLTree* pNode)
            {
                AVLTree
            * pNode1;

                pNode1 
            = pNode->pLeft;
                pNode
            ->pLeft = pNode1->pRight;
                pNode1
            ->pRight = pNode;

                
            // 結點的位置變了, 要更新結點的高度值
                pNode
            ->nHeight = Max(Height(pNode->pLeft), Height(pNode->pRight)) + 1;
                pNode1
            ->nHeight = Max(Height(pNode1->pLeft), pNode->nHeight) + 1;

                return pNode1;
            }

            /********************************************************************
            pNode                                   pNode
            ->pRight
                 
            \                                  /
                 pNode
            ->pRight           ==>    pNode 
                 
            /                                   \
            pNode
            ->pRight->pLeft                     pNode->pRight->pLeft
            *********************************************************************/
            AVLTree
            * SingleRotateWithRight(AVLTree* pNode)
            {
                AVLTree
            * pNode1;

                pNode1 
            = pNode->pRight;
                pNode
            ->pRight = pNode1->pLeft;
                pNode1
            ->pLeft = pNode;

                
            // 結點的位置變了, 要更新結點的高度值
                pNode
            ->nHeight = Max(Height(pNode->pLeft), Height(pNode->pRight)) + 1;
                pNode1
            ->nHeight = Max(Height(pNode1->pRight), pNode->nHeight) + 1;

                return pNode1;
            }

            AVLTree
            * DoubleRotateWithLeft(AVLTree* pNode)
            {
                pNode
            ->pLeft = SingleRotateWithRight(pNode->pLeft);

                return SingleRotateWithLeft(pNode);
            }

            AVLTree
            * DoubleRotateWithRight(AVLTree* pNode)
            {
                pNode
            ->pRight = SingleRotateWithLeft(pNode->pRight);

                return SingleRotateWithRight(pNode);
            }

            // 后序遍歷樹以刪除樹
            void DeleteTree(AVLTree
            ** ppRoot)
            {
                
            if (NULL == ppRoot || NULL == *ppRoot)
                    return;

                DeleteTree(
            &((*ppRoot)->pLeft));
                DeleteTree(
            &((*ppRoot)->pRight));
                free(
            *ppRoot);
                
            *ppRoot = NULL;
            }

            // 中序遍歷打印樹的所有結點, 因為左結點 < 父結點 < 右結點, 因此打印出來數(shù)據(jù)的大小是遞增的
            void PrintTree(AVLTree
            * pRoot)
            {
                
            if (NULL == pRoot)
                    return;

                static 
            int n = 0;

                PrintTree(pRoot
            ->pLeft);
                printf(
            "[%d]nData = %d\n"++n, pRoot->nData);
                PrintTree(pRoot
            ->pRight);
            }

            另外,關于AVL樹,chinaunix的win_hate有一段精彩的講述,在這個地址:
            http://bbs.chinaunix.net/viewthread.php?tid=692071

            posted on 2007-08-29 22:06 那誰 閱讀(8578) 評論(7)  編輯 收藏 引用 所屬分類: 算法與數(shù)據(jù)結構

            評論

            # re: AVL樹的實現(xiàn)代碼  回復  更多評論   

            不能刪除單個結點?
            2008-03-29 21:17 | BaluWu

            # re: AVL樹的實現(xiàn)代碼  回復  更多評論   

            有刪除的實現(xiàn)部分嗎?
            http://blog.chinaunix.net/u1/53908/
            2008-06-02 14:32 | linfengfeiye

            # re: AVL樹的實現(xiàn)代碼  回復  更多評論   

            學習了!多謝!
            2008-10-23 20:09 | ashizl

            # re: AVL樹的實現(xiàn)代碼  回復  更多評論   

            for (i = 0; i < 100000000; ++i)
            {
            pRoot = Insert(::rand(), pRoot);
            }
            我是一個算法初學者,這里有的問題啊:pRoot應該是指向新插入節(jié)點,那么這樣的話,每次插入都是從新節(jié)點開始,而不是從根節(jié)點開始吧??
            2011-04-06 00:33 | 劉學金

            # re: AVL樹的實現(xiàn)代碼  回復  更多評論   

            insert之中沒有處理插入已經(jīng)存在的節(jié)點的情況啊!
            2012-08-06 13:39 | lz

            # re: AVL樹的實現(xiàn)代碼  回復  更多評論   

            我覺得里面有一處是有問題,請您看看是不是,在執(zhí)行插入之后,那個節(jié)點的高度是0才對,但是在下面執(zhí)行的時候又賦值為1了。我覺得應該在最后插入的時候應該有一個return,這樣葉子節(jié)點的高度為0.
            2013-10-27 12:17 | qq471876425

            # re: AVL樹的實現(xiàn)代碼  回復  更多評論   

            謝了
            2014-01-04 00:22 | sicily
            日产精品久久久一区二区| 91精品婷婷国产综合久久| 久久久91人妻无码精品蜜桃HD| 久久人爽人人爽人人片AV| 欧美亚洲国产精品久久高清| 青青青青久久精品国产h久久精品五福影院1421| 久久福利青草精品资源站免费| 香蕉久久夜色精品升级完成| 看久久久久久a级毛片| 久久久一本精品99久久精品66| 精品无码久久久久久尤物| 青青草国产精品久久| 国内精品久久久久久久影视麻豆| 国产精久久一区二区三区| 久久亚洲AV无码西西人体| 亚洲欧洲中文日韩久久AV乱码| 国产69精品久久久久9999APGF| 亚洲中文字幕无码久久精品1 | 欧美综合天天夜夜久久| 国产精自产拍久久久久久蜜| 天天综合久久一二三区| 久久精品综合网| 青青草国产精品久久| 亚洲国产成人久久综合一区77| 久久亚洲私人国产精品vA | 久久久受www免费人成| 久久午夜无码鲁丝片秋霞| 国产麻豆精品久久一二三| 亚洲国产成人久久精品99 | 精品久久久久久久无码| 久久九九免费高清视频| 久久久婷婷五月亚洲97号色| 精品久久久久久久久久久久久久久| 一级女性全黄久久生活片免费| 国产精品久久久久久久久| 香蕉aa三级久久毛片| 热久久国产精品| 亚洲国产精品无码久久98| 久久久久久噜噜精品免费直播| 狠狠色婷婷久久一区二区三区| 中文字幕久久精品|