• <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 那誰 閱讀(8583) 評論(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
            日韩一区二区久久久久久| 热综合一本伊人久久精品| 一本综合久久国产二区| 久久婷婷人人澡人人爽人人爱 | 久久精品免费一区二区| 久久综合狠狠综合久久激情 | 久久久久18| 久久综合亚洲色HEZYO国产| 国色天香久久久久久久小说| 成人久久久观看免费毛片| 精品久久久久一区二区三区| 久久99精品久久久大学生| 久久精品一区二区三区不卡| 久久精品视频一| 国产精品日韩深夜福利久久| 色婷婷综合久久久久中文| 蜜桃麻豆www久久国产精品| 久久精品无码专区免费青青| 久久福利片| 国产精品99久久久久久董美香 | 精品久久久久久久久免费影院| 综合久久精品色| 精品久久无码中文字幕| 婷婷久久综合| 国产福利电影一区二区三区久久老子无码午夜伦不 | 久久人爽人人爽人人片AV| 亚洲午夜精品久久久久久app| 久久夜色tv网站| 大香伊人久久精品一区二区| 久久国产视屏| 久久精品国产99久久久香蕉| 韩国无遮挡三级久久| 国产精品久久久久9999高清| 精品久久无码中文字幕| 国产精品一区二区久久国产| 久久精品国产亚洲AV大全| 久久久精品国产免大香伊| AV无码久久久久不卡蜜桃| 伊人久久大香线蕉亚洲五月天| 久久久久久久综合狠狠综合| 日本久久久久久久久久|