• <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內核
            隨筆 - 210, 文章 - 0, 評論 - 1183, 引用 - 0
            數據加載中……

            AVL樹的實現代碼

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

            purpose:    AVL樹的實現代碼, 
                        參考資料
            <<數據結構與算法分析-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;
            }

            // 中序遍歷打印樹的所有結點, 因為左結點 < 父結點 < 右結點, 因此打印出來數據的大小是遞增的
            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)  編輯 收藏 引用 所屬分類: 算法與數據結構

            評論

            # re: AVL樹的實現代碼  回復  更多評論   

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

            # re: AVL樹的實現代碼  回復  更多評論   

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

            # re: AVL樹的實現代碼  回復  更多評論   

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

            # re: AVL樹的實現代碼  回復  更多評論   

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

            # re: AVL樹的實現代碼  回復  更多評論   

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

            # re: AVL樹的實現代碼  回復  更多評論   

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

            # re: AVL樹的實現代碼  回復  更多評論   

            謝了
            2014-01-04 00:22 | sicily
            精品国产婷婷久久久| 国产精品一区二区久久精品涩爱 | 久久精品国产第一区二区| 久久综合给久久狠狠97色| 日韩人妻无码精品久久久不卡| 亚洲国产精品无码久久久久久曰| 久久久久亚洲?V成人无码| 国产免费久久久久久无码| 精品久久久久中文字| 欧美激情精品久久久久久久| 人人狠狠综合88综合久久| 思思久久精品在热线热| 久久久久se色偷偷亚洲精品av| 中文字幕乱码人妻无码久久 | 国产成人综合久久精品红| 久久无码AV一区二区三区| 国产精品99久久久精品无码| 久久久一本精品99久久精品66| 97久久精品无码一区二区 | 国产精品成人久久久久三级午夜电影| 国产高潮国产高潮久久久91| 四虎影视久久久免费观看| 精品国产青草久久久久福利| 久久精品国产亚洲77777| 国产精品免费久久| 77777亚洲午夜久久多人| 精品一区二区久久久久久久网站| 成人国内精品久久久久影院VR| 久久天天躁狠狠躁夜夜av浪潮| 亚洲精品国产美女久久久| 26uuu久久五月天| 日韩精品久久无码人妻中文字幕| 亚洲国产精品久久| 亚洲午夜无码AV毛片久久| 国产亚洲精品自在久久| 久久电影网| 成人久久综合网| 99久久夜色精品国产网站| 久久91精品综合国产首页| 青青草原精品99久久精品66| 中文精品99久久国产 |