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

            Just enjoy programming

            AVL樹(轉載)

            轉載:http://m.shnenglu.com/converse/archive/2007/08/29/31179.html

            /********************************************************************
            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);
            }

            posted on 2011-05-11 10:25 周強 閱讀(277) 評論(0)  編輯 收藏 引用 所屬分類: 算法

            久久久免费精品re6| 99久久免费国产精品| 久久精品国产99久久无毒不卡 | 亚洲欧美一区二区三区久久| 97视频久久久| 一本久久久久久久| 久久久精品国产| 国产精品gz久久久| 伊人色综合久久天天人手人婷 | 久久精品国产99久久丝袜| 7777精品久久久大香线蕉| 91久久精品视频| 色狠狠久久AV五月综合| 久久99国产精品成人欧美| 久久婷婷是五月综合色狠狠| 久久er热视频在这里精品| 99久久这里只精品国产免费| 亚洲嫩草影院久久精品| 久久精品国产久精国产思思| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 久久久久久久精品成人热色戒| 久久国产精品国产自线拍免费| 亚洲午夜无码久久久久小说| 99久久人人爽亚洲精品美女| 国产∨亚洲V天堂无码久久久| 人妻无码精品久久亚瑟影视 | 国内精品久久久久久不卡影院| 久久久久亚洲AV无码专区首JN | 一本一道久久精品综合| 久久精品99久久香蕉国产色戒 | 国产女人aaa级久久久级| 色综合久久综合中文综合网| 欧美一级久久久久久久大片| 久久久久99精品成人片三人毛片 | 国内精品久久久久久99| 久久精品无码专区免费青青| 人妻少妇久久中文字幕一区二区 | 国产精品美女久久久久| 久久精品中文无码资源站| 日日噜噜夜夜狠狠久久丁香五月| 久久精品国产亚洲av麻豆图片|