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

             

            #include <iostream>

            using namespace std; 

            template
            <typename Type> 
            struct BTNode{
                Type elem;
                BTNode
            <Type>* lchild, *rchild;
                BTNode(){}
                BTNode( BTNode
            <Type>* l, BTNode<Type>* r, Type k ):
                    lchild(l), rchild(r), elem(k) {}
            };

            template
            <typename Type>
            class BSTree{
                
            private:
                    BTNode
            <Type>* root;    
                    BTNode
            <Type>* get_min( BTNode<Type>* );
                    BTNode
            <Type>* get_max( BTNode<Type>* );
                    BTNode
            <Type>* insert ( BTNode<Type>*, Type key );
                    BTNode
            <Type>* erase  ( BTNode<Type>*, Type key );
                    
            void          travel ( BTNode<Type>* root );
                    
            void          clear  ( BTNode<Type>* root );
                    
            bool          search ( BTNode<Type>*, Type key );
                
                
            public:
                    BSTree();
                    
            ~BSTree();
                    BTNode
            <Type>* get_min();
                    BTNode
            <Type>* get_max();
                    
            void          insert( Type key );
                    
            void          travel();
                    
            bool          is_empty();
                    
            void          clear();
                    
            void          erase( Type key );
                    
            bool          search( Type key );
            };

            template
            <typename Type>
            bool BSTree<Type>::search( BTNode<Type>* root, Type key ){
                
            if!root ) return false;
                
            if( root->elem== key ) return true;
                
                
            if( key< root->elem ) return search( root->lchild, key );
                
            else                  return search( root->rchild, key );
            }

            template
            <typename Type>
            bool BSTree<Type>::search( Type key ){
                
            return search( root, key ); }

            template
            <typename Type>
            BTNode
            <Type>* BSTree<Type>::erase( BTNode<Type>* root, Type key ){
                
            if( root->elem== key ){
                    
            if!root->lchild && !root->rchild ){
                        delete root;
                        
            return NULL; }
                    
            else if( root->lchild && !root->rchild ){
                        BTNode
            <Type>* tmp= root->lchild;
                        delete root; 
            return tmp; }
                    
            else {
                        BTNode
            <Type>* tmp= get_min( root->rchild );
                        root
            ->elem= tmp->elem;
                        root
            ->rchild= erase( root->rchild, tmp->elem );
                        
            return root; }
                }
                
            if( key< root->elem ) root->lchild= erase( root->lchild, key );
                
            else                  root->rchild= erase( root->rchild, key );
                
            return root;
            }

            template
            <typename Type>
            void BSTree<Type>::erase( Type key ){
                
            if!search( key ) ) return;
                root
            = erase( root, key );
            }

            template
            <typename Type>
            void BSTree<Type>::clear( BTNode<Type>* root ){
                
            if( root ){
                    clear( root
            ->lchild );
                    clear( root
            ->rchild );
                    delete root; }
            }

            template
            <typename Type>
            void BSTree<Type>::clear(){
                clear( root ); }

            template
            <typename Type>
            bool BSTree<Type>::is_empty(){
                
            return root== NULL; }

            template
            <typename Type>
            BSTree
            <Type>::BSTree(){ root= NULL; }

            template
            <typename Type>
            BSTree
            <Type>::~BSTree(){
                clear(); }

            template
            <typename Type>
            void BSTree<Type>::travel( BTNode<Type>* root ){
                
            if( root ){
                    travel( root
            ->lchild );
                    cout 
            << root->elem << endl;
                    travel( root
            ->rchild ); }
            }

            template
            <typename Type>
            void BSTree<Type>::travel(){
                BTNode
            <Type>* r= root;
                travel( r ); }

            template
            <typename Type>
            BTNode
            <Type>* BSTree<Type>::insert(BTNode<Type>* root,Type key){
                
            if( root== NULL )
                    
            return new BTNode<Type>( NULL, NULL, key );
                
            if( root->lchild== NULL && key< root->elem ){
                    root
            ->lchild= new BTNode<Type>( NULL, NULL, key );
                    
            return root; }
                
            else if( root->rchild== NULL && key> root->elem ){
                    root
            ->rchild= new BTNode<Type>( NULL, NULL, key );
                    
            return root; }
                
            if( root->elem> key ) root->lchild= insert( root->lchild, key );
                
            else                  root->rchild= insert( root->rchild, key );
                
            return root;
            }

            template
            <typename Type>
            void BSTree<Type>::insert( Type key ){
                root
            = insert( root, key ); }

            template
            <typename Type>
            BTNode
            <Type>* BSTree<Type>::get_min( BTNode<Type>* root ){
                
            if( root== NULL ) return NULL;
                
            while( root->lchild ) root= root->lchild;
                
            return root;}

            template
            <typename Type>
            BTNode
            <Type>* BSTree<Type>::get_min(){
                BTNode
            <Type>* r= root;
                
            return get_min( r );}

            template
            <typename Type>
            BTNode
            <Type>* BSTree<Type>::get_max( BTNode<Type>* root ){
                
            if( root== NULL ) return NULL;
                
            while( root->rchild ) root= root->rchild;
                
            return root;}

            template
            <typename Type>
            BTNode
            <Type>* BSTree<Type>::get_max(){
                BTNode
            <Type>* r= root;
                
            return get_max( r );}
                
            int main()
            {
                freopen( 
            "test.txt""w", stdout );
                BSTree
            <int>  test;
                
                
            forint i= 0; i< 100000++i )
                test.insert( rand() );
                
            forint i= 0; i< 100000++i )
                test.erase( rand() );
                
                test.travel();
                
                
            return 0;
            }
            posted on 2009-04-11 19:29 Darren 閱讀(1263) 評論(2)  編輯 收藏 引用

            評論:
            # re: Binary Search Tree c++ 模板實現(xiàn) 2010-10-24 15:34 | 周潤生
            在VC++里編程出現(xiàn)error的話。去掉main函數(shù)里第二個for循環(huán)的int可以解決。  回復(fù)  更多評論
              
            # re: Binary Search Tree c++ 模板實現(xiàn) 2010-12-17 00:23 | aba
            @周潤生
            這個是早期C++的一個遺留問題,在VC++6.0中可以用
            #define for if(false);else{for解決。  回復(fù)  更多評論
              

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            无遮挡粉嫩小泬久久久久久久| 91久久精品91久久性色| 久久综合九色综合网站| 久久久青草久久久青草| 亚洲精品美女久久久久99小说| 国产精品美女久久久m| 色欲综合久久躁天天躁| 久久91亚洲人成电影网站| 久久久久亚洲国产| 成人亚洲欧美久久久久| 久久综合久久自在自线精品自| 久久久久久av无码免费看大片| 久久青青草原精品国产| 亚洲欧美成人久久综合中文网| 久久99国产精一区二区三区| 99精品久久精品一区二区| 激情久久久久久久久久| 99久久精品费精品国产一区二区| 久久乐国产综合亚洲精品| 久久国产成人亚洲精品影院| 潮喷大喷水系列无码久久精品| 三级三级久久三级久久| 久久久精品日本一区二区三区| 久久免费视频观看| 国产亚洲综合久久系列| 天天躁日日躁狠狠久久| 久久精品卫校国产小美女| 一级做a爰片久久毛片毛片| 久久精品国产亚洲Aⅴ香蕉| 国产综合精品久久亚洲| 四虎国产精品免费久久久| 国内精品久久人妻互换| 国产精品久久久久久吹潮| 男女久久久国产一区二区三区| 久久人妻无码中文字幕| 无码任你躁久久久久久老妇App| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 久久亚洲欧美国产精品| 国产成人精品综合久久久久 | 久久综合亚洲鲁鲁五月天| 亚洲午夜久久久|