• <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>
            隨筆 - 87  文章 - 279  trackbacks - 0
            <2007年3月>
            25262728123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            潛心看書(shū)研究!

            常用鏈接

            留言簿(19)

            隨筆分類(lèi)(81)

            文章分類(lèi)(89)

            相冊(cè)

            ACM OJ

            My friends

            搜索

            •  

            積分與排名

            • 積分 - 217820
            • 排名 - 117

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            基本測(cè)試沒(méi)問(wèn)題, 有bug請(qǐng)指出:)

            #include? < iostream >
            using ? namespace ?std;

            const ? int ?MAXSIZE? = ? 100 ;
            const ? int ?R? = ? 0 ;
            const ? int ?B???? = ? 1 ;
            const ? int ?NIL? = ? 0 ;

            class ?RBTree
            {
            public :
            ????RBTree();
            ????
            int ?Root();
            ????
            int ?Size();
            ????
            int ?Key( int );
            ????
            int ?Pointer( int );
            ????
            int ?Search( int ,? int );
            ????
            int ?Minimum( int );
            ????
            int ?Maximum( int );
            ????
            int ?successor( int );
            ????
            int ?predecessor( int );
            ????
            void ?LeftRotate( int );
            ????
            void ?RightRotate( int );
            ????
            void ?Insert( int );
            ????
            void ?InsertByPointer( int );
            ????
            void ?InsertFixup( int );
            ????
            void ?Delete( int );
            ????
            void ?DeleteByPointer( int );
            ????
            void ?DeleteFixup( int );
            ????
            void ?Travel( int );
            private :
            ????
            int ?key[MAXSIZE];
            ????
            int ?left[MAXSIZE];
            ????
            int ?right[MAXSIZE];
            ????
            int ?color[MAXSIZE];
            ????
            int ?p[MAXSIZE];
            ????
            int ?stack[MAXSIZE];
            ????
            int ?root;
            ????
            int ?top;
            ????
            int ?size;
            }
            ;

            RBTree::RBTree()
            {
            ????root?
            = ?NIL;? // 默認(rèn)空樹(shù)root為NIL;
            ????top? = ? 0 ;
            ????size?
            = ? 0 ;
            ????memset(right,?NIL,?
            sizeof (right));
            ????memset(left,?NIL,?
            sizeof (left));
            ????memset(p,?NIL,?
            sizeof (p));
            ????color[NIL]?
            = ?B;
            }


            int ?RBTree::Root()
            {
            ????
            return ?root;
            }


            int ?RBTree::Size()
            {
            ????
            return ?size;
            }


            int ?RBTree::Key( int ?x)
            {
            ????
            return ?key[x];
            }


            int ?RBTree::Pointer( int ?k)
            {
            ????
            return ?Search(Root(),?k);
            }


            int ?RBTree::Minimum( int ?x)
            {
            ????
            while ?(left[x]? != ?NIL)
            ????????x?
            = ?left[x];
            ????
            return ?x;
            }


            int ?RBTree::Maximum( int ?x)
            {
            ????
            while ?(right[x]? != ?NIL)
            ????????x?
            = ?right[x];
            ????
            return ?x;
            }


            int ?RBTree::Search( int ?x,? int ?k)
            {
            ????
            while ?(x? != ?NIL? && ?k? != ?key[x])
            ????
            {
            ????????
            if ?(k? < ?key[x])
            ????????????x?
            = ?left[x];
            ????????
            else
            ????????????x?
            = ?right[x];
            ????}

            ????
            return ?x;
            }


            int ?RBTree::successor( int ?x)
            {
            ????
            if ?(right[x]? != ?NIL)
            ????????
            return ?Minimum(right[x]);
            ????
            int ?y? = ?p[x];
            ????
            while ?(y? != ?NIL? && ?x? == ?right[y])
            ????
            {
            ????????x?
            = ?y;
            ????????y?
            = ?p[y];
            ????}

            ????
            return ?y;
            }


            int ?RBTree::predecessor( int ?x)
            {
            ????
            if ?(left[x]? != ?NIL)
            ????????
            return ?Maximum(left[x]);
            ????
            int ?y? = ?p[x];
            ????
            while ?(y? != ?NIL? && ?x? == ?left[y])
            ????
            {
            ????????x?
            = ?y;
            ????????y?
            = ?p[y];
            ????}

            ????
            return ?y;
            }



            void ?RBTree::LeftRotate( int ?x)
            {
            ????
            int ?y? = ?right[x];
            ????right[x]?
            = ?left[y];
            ????p[left[y]]?
            = ?x;
            ????p[y]?
            = ?p[x];
            ????
            if ?(p[x]? == ?NIL)
            ????????root?
            = ?y;
            ????
            else
            ????
            {
            ????????
            if ?(x? == ?left[p[x]])
            ????????????left[p[x]]?
            = ?y;
            ????????
            else
            ????????????right[p[x]]?
            = ?y;
            ????}

            ????left[y]?
            = ?x;
            ????p[x]?
            = ?y;
            }


            void ?RBTree::RightRotate( int ?x)
            {
            ????
            int ?y? = ?left[x];
            ????left[x]?
            = ?right[y];
            ????p[right[y]]?
            = ?x;
            ????p[y]?
            = ?p[x];
            ????
            if ?(p[x]? == ?NIL)
            ????????root?
            = ?y;
            ????
            else
            ????
            {
            ????????
            if ?(x? == ?left[p[x]])
            ????????????left[p[x]]?
            = ?y;
            ????????
            else
            ????????????right[p[x]]?
            = ?y;
            ????}

            ????right[y]?
            = ?x;
            ????p[x]?
            = ?y;
            }


            void ?RBTree::Insert( int ?k)
            {
            ????
            int ?z;
            ????
            if ?(top? > ? 0 )
            ????????z?
            = ?stack[ -- top];
            ????
            else
            ????????z?
            = ? ++ size;
            ????key[z]?
            = ?k;
            ????InsertByPointer(z);
            }


            void ?RBTree::Delete( int ?k)
            {
            ????
            int ?z? = ?Search(Root(),?k);
            ????
            if ?(z? != ?NIL)
            ????
            {
            ????????stack[top
            ++ ]? = ?z;
            ????????size
            -- ;
            ????????DeleteByPointer(z);
            ????}

            }


            void ?RBTree::InsertByPointer( int ?z)
            {
            ????
            int ?y? = ?NIL;
            ????
            int ?x? = ?root;
            ????
            while ?(x? != ?NIL)
            ????
            {
            ????????y?
            = ?x;
            ????????
            if ?(key[z]? < ?key[x])
            ????????????x?
            = ?left[x];
            ????????
            else
            ????????????x?
            = ?right[x];
            ????}

            ????p[z]?
            = ?y;
            ????
            if ?(y? == ?NIL)
            ????????root?
            = ?z;
            ????
            else
            ????
            {
            ????????
            if ?(key[z]? < ?key[y])
            ????????????left[y]?
            = ?z;
            ????????
            else
            ????????????right[y]?
            = ?z;
            ????}

            ????left[z]?
            = ?NIL;
            ????right[z]?
            = ?NIL;
            ????color[z]?
            = ?R;
            ????InsertFixup(z);
            }


            void ?RBTree::InsertFixup( int ?z)
            {
            ????
            int ?y;
            ????
            while ?(color[p[z]]? == ?R)
            ????
            {
            ????????
            if ?(p[z]? == ?left[p[p[z]]])
            ????????
            {
            ????????????y?
            = ?right[p[p[z]]];
            ????????????
            if ?(color[y]? == ?R)
            ????????????
            {
            ????????????????color[p[z]]?
            = ?B;
            ????????????????color[y]?
            = ?B;
            ????????????????color[p[p[z]]]?
            = ?R;
            ????????????????z?
            = ?p[p[z]];
            ????????????}

            ????????????
            else
            ????????????
            {
            ????????????????
            if ?(z? == ?right[p[z]])
            ????????????????
            {
            ????????????????????z?
            = ?p[z];
            ????????????????????LeftRotate(z);
            ????????????????}

            ????????????????color[p[z]]?
            = ?B;
            ????????????????color[p[p[z]]]?
            = ?R;
            ????????????????RightRotate(p[p[z]]);
            ????????????}

            ????????}

            ????????
            else
            ????????
            {
            ????????????y?
            = ?left[p[p[z]]];
            ????????????
            if ?(color[y]? == ?R)
            ????????????
            {
            ????????????????color[p[z]]?
            = ?B;
            ????????????????color[y]?
            = ?B;
            ????????????????color[p[p[z]]]?
            = ?R;
            ????????????????z?
            = ?p[p[z]];
            ????????????}

            ????????????
            else
            ????????????
            {
            ????????????????
            if ?(z? == ?left[p[z]])
            ????????????????
            {
            ????????????????????z?
            = ?p[z];
            ????????????????????RightRotate(z);
            ????????????????}

            ????????????????color[p[z]]?
            = ?B;
            ????????????????color[p[p[z]]]?
            = ?R;
            ????????????????LeftRotate(p[p[z]]);
            ????????????}

            ????????}

            ????}

            ????color[root]?
            = ?B;
            }


            void ?RBTree::DeleteByPointer( int ?z)
            {
            ????
            int ?x,?y;
            ????
            if ?(left[z]? == ?NIL? || ?right[z]? == ?NIL)
            ????????y?
            = ?z;
            ????
            else
            ????????y?
            = ?successor(z);
            ????
            if ?(left[y]? != ?NIL)
            ????????x?
            = ?left[y];
            ????
            else
            ????????x?
            = ?right[y];
            ????p[x]?
            = ?p[y];
            ????
            if ?(p[y]? == ?NIL)
            ????????root?
            = ?x;
            ????
            else
            ????
            {
            ????????
            if ?(y? == ?left[p[y]])
            ????????????left[p[y]]?
            = ?x;
            ????????
            else
            ????????????right[p[y]]?
            = ?x;
            ????}

            ????
            if ?(y? != ?z)
            ????????key[z]?
            = ?key[y];
            ????
            if ?(color[y]? == ?B)
            ????????DeleteFixup(x);
            }


            void ?RBTree::DeleteFixup( int ?x)
            {
            ????
            int ?y,?w;
            ????
            while ?(x? != ?root? && ?color[x]? == ?B)
            ????
            {
            ????????
            if ?(x? == ?left[p[x]])
            ????????
            {
            ????????????w?
            = ?right[p[x]];
            ????????????
            if ?(color[w]? = ?R)
            ????????????
            {
            ????????????????color[w]?
            = ?B;
            ????????????????color[p[x]]?
            = ?R;
            ????????????????LeftRotate(p[x]);
            ????????????????w?
            = ?right[p[x]];
            ????????????}

            ????????????
            if ?(color[left[w]]? == ?B? && ?color[right[w]]? == ?B)
            ????????????
            {
            ????????????????color[w]?
            = ?R;
            ????????????????x?
            = ?p[x];
            ????????????}

            ????????????
            else
            ????????????
            {
            ????????????????
            if ?(color[right[w]]? == ?B)
            ????????????????
            {
            ????????????????????color[left[w]]?
            = ?B;
            ????????????????????color[w]?
            = ?R;
            ????????????????????RightRotate(w);
            ????????????????????w?
            = ?right[p[x]];
            ????????????????}

            ????????????????color[w]?
            = ?color[p[x]];
            ????????????????color[p[x]]?
            = ?B;
            ????????????????color[right[w]]?
            = ?B;
            ????????????????LeftRotate(p[x]);
            ????????????????x?
            = ?root;
            ????????????}

            ????????}

            ????????
            else
            ????????
            {
            ????????????w?
            = ?left[p[x]];
            ????????????
            if ?(color[w]? = ?R)
            ????????????
            {
            ????????????????color[w]?
            = ?B;
            ????????????????color[p[x]]?
            = ?R;
            ????????????????RightRotate(p[x]);
            ????????????????w?
            = ?left[p[x]];
            ????????????}

            ????????????
            if ?(color[right[w]]? == ?B? && ?color[left[w]]? == ?B)
            ????????????
            {
            ????????????????color[w]?
            = ?R;
            ????????????????x?
            = ?p[x];
            ????????????}

            ????????????
            else
            ????????????
            {
            ????????????????
            if ?(color[left[w]]? == ?B)
            ????????????????
            {
            ????????????????????color[right[w]]?
            = ?B;
            ????????????????????color[w]?
            = ?R;
            ????????????????????LeftRotate(w);
            ????????????????????w?
            = ?left[p[x]];
            ????????????????}

            ????????????????color[w]?
            = ?color[p[x]];
            ????????????????color[p[x]]?
            = ?B;
            ????????????????color[left[w]]?
            = ?B;
            ????????????????RightRotate(p[x]);
            ????????????????x?
            = ?root;
            ????????????}

            ????????}

            ????}

            ????color[x]?
            = ?B;
            }

            void ?RBTree::Travel( int ?x)
            {
            ????
            if ?(x? != ?NIL)
            ????
            {
            ????????cout?
            << ? ' ( ' ;
            ????????Travel(left[x]);
            ????????cout?
            << ? ' ? ' ? << ?key[x]? << ? ' ? ' ;
            ????????Travel(right[x]);
            ????????cout?
            << ? ' ) ' ;
            ????}

            }


            int ?main()
            {
            ????RBTree?T;

            ????
            for ?( int ?i = 1 ;?i <= 10 ;?i ++ )
            ????????T.Insert(i
            * 10 );
            ????
            for ?( int ?i = 1 ;?i <= 10 ;?i ++ )
            ????
            {
            ????????T.Delete(i
            * 10 );
            ????????T.Travel(T.Root());
            ????????cout?
            << ?endl;
            ????}

            ????
            return ? 0 ;
            }
            posted on 2006-09-15 01:15 閱讀(1170) 評(píng)論(4)  編輯 收藏 引用 所屬分類(lèi): 算法&ACM

            FeedBack:
            # re: 終于把RBTree敲出來(lái)了, 累. 2006-09-15 10:25 small-fat
            .......呵呵,頂一下,牛牛..  回復(fù)  更多評(píng)論
              
            # re: 終于把RBTree敲出來(lái)了, 累. 2006-09-15 12:48 chenger
            用數(shù)組?感覺(jué)一般只有在做算法題時(shí)才這么干  回復(fù)  更多評(píng)論
              
            # re: 終于把RBTree敲出來(lái)了, 累. 2006-09-15 12:57 
            因?yàn)橛脭?shù)組可以換速度。。  回復(fù)  更多評(píng)論
              
            # re: 終于把RBTree敲出來(lái)了, 累. 2006-09-15 14:29 Optimistic
            偶看了都覺(jué)得累呀。。。。  回復(fù)  更多評(píng)論
              
            久久久久国色AV免费观看| 久久天天躁狠狠躁夜夜av浪潮 | 久久青草国产精品一区| 精品国产青草久久久久福利| 久久婷婷午色综合夜啪| 国产精品久久久久久久久鸭 | 亚洲国产成人久久综合一 | 色婷婷久久久SWAG精品| 亚洲av成人无码久久精品| 国产激情久久久久影院| 亚洲国产精品一区二区久久hs| 一本久久a久久精品综合夜夜| 久久婷婷色综合一区二区| 7国产欧美日韩综合天堂中文久久久久| 一级A毛片免费观看久久精品| 久久天堂电影网| 性欧美丰满熟妇XXXX性久久久 | 亚洲va中文字幕无码久久不卡 | 亚洲午夜久久久影院伊人| 久久婷婷人人澡人人| 色综合久久中文综合网| 久久天堂AV综合合色蜜桃网| 亚洲国产日韩欧美综合久久| 国产精品久久久99| 久久国产高清字幕中文| 国产精品久久网| 99久久成人国产精品免费| 久久综合亚洲欧美成人| 精品综合久久久久久98| 久久久久精品国产亚洲AV无码| 久久精品二区| 久久乐国产精品亚洲综合| 国内精品伊人久久久久影院对白| 久久久久久免费一区二区三区 | 国产69精品久久久久APP下载| 久久久艹| 无码任你躁久久久久久久| 日产久久强奸免费的看| 性做久久久久久久久老女人 | 久久r热这里有精品视频| 狠狠色丁香婷婷综合久久来|