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

            Problem Solving using C++

            Algorithm Study using C++

            二叉搜索樹操作(2)--其他

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

            #ifndef NULL
            #define NULL 
            0
            #endif

            #ifndef MAXSIZE
            #define MAXSIZE    
            10
            #endif

            typedef struct BST
            //Binary Search Tree
            {
                
            int key;
                
            //maybe there are some other satellites
                
                struct BST
            * left;
                struct BST
            * right;
                struct BST
            * parent;
            } BST;

            BST
            * root=NULL;

            void BST_Insert(int key)//add value key to the Binary Search Tree
            {
                BST
            * y=NULL;//y records the parent node
                BST* x=root;//x records the current node
                
                BST
            * node = new BST();
                node
            ->key = key;
                node
            ->left = NULL;
                node
            ->right = NULL;
                node
            ->parent = NULL;
                
                
            while(x!=NULL)
                {
                    y
            =x;
                    
                    
            if(key<x->key)
                        x
            =x->left;
                    
            else
                        x
            =x->right;
                }
                
                node
            ->parent=y;
                
                
            if(y==NULL)
                    root 
            = node;
                
            else
                {
                    
            if(key<y->key)
                        y
            ->left=node;
                    
            else
                        y
            ->right=node;
                }
            }

            void BST_Delete(BST* p)
            {
                
            if(p)
                {
                    BST_Delete(p
            ->left);
                    BST_Delete(p
            ->right);
                    delete p;
                }
            }

            void BST_Build()
            {
                
            int temp;
                
                cout
            <<"Original Input:"<<endl;
                
            for(int i=0;i<MAXSIZE;i++)
                {
                    temp
            =rand()%MAXSIZE;
                    cout
            <<temp<<" ";
                    BST_Insert(temp);
                }
                cout
            <<endl;
            }

            void BST_Inorder_Walk(BST* p)
            {
                
                
            if(p)
                {
                    BST_Inorder_Walk(p
            ->left);
                    cout
            <<p->key<<" ";
                    BST_Inorder_Walk(p
            ->right);
                }
            }

            void BST_Preorder_Walk(BST* p)
            {
                
                
            if(p)
                {
                    cout
            <<p->key<<" ";
                    BST_Preorder_Walk(p
            ->left);
                    BST_Preorder_Walk(p
            ->right);
                }
            }

            void BST_Postorder_Walk(BST* p)
            {
                
            if(p)
                {
                    BST_Postorder_Walk(p
            ->left);
                    BST_Postorder_Walk(p
            ->right);
                    cout
            <<p->key<<" ";
                }
            }

            BST
            * BST_Search(int key)
            {
                BST
            * x=root;
                
                
            while(x)
                {
                    
            if(x->key==key)
                        
            return x;
                    
            else
                        
            if(x->key>key)
                            x
            =x->left;
                        
            else
                            x
            =x->right;
                }
                
                
            return NULL;    
            }

            BST
            * BST_Min(BST* p=root)
            {
                
            //BST* p = root;
                
                
            while(p->left)
                {
                    p
            =p->left;
                }
                
                
            return p;
            }

            BST
            * BST_Max(BST* p=root)
            {
                
            //BST* p = root;
                
                
            while(p->right)
                {
                    p
            =p->right;
                }
                
                
            return p;
            }

            BST
            * BST_Successor(int key)
            {
                BST
            * p = BST_Search(key);
                BST
            * y;
                
                
            if(p)
                {
                    
            if(p->right)
                    {
                        
            return BST_Min(p->right);
                    }
                    
                    y
            =p->parent;
                    
            while(y&&(y->right==p))
                    {
                        p
            =y;
                        y
            =y->parent;
                    }
                    
                    
            return y;
                }
                
                
            return NULL;
            }

            BST
            * BST_Predecessor(int key)
            {
                BST
            * p = BST_Search(key);
                BST
            * y;
                
                
            if(p)
                {
                    
            if(p->left)
                        
            return BST_Max(p->left);
                    
                    y
            =p->parent;
                    
            while(y&&(y->left==p))
                    {
                        p
            =y;
                        y
            =y->parent;
                    }
                    
                    
            return y;
                }
                
                
            return p;
            }

            BST
            * BST_Delete(int key)
            {
                BST
            * p = BST_Search(key);
                BST
            * y,*x;
                
                
            if(p)
                {
                    
            if(!p->left||!p->right)
                    {
                        y
            =p;
                    }
                    
            else
                        y
            =BST_Successor(key);
                        
                    
            if(y->left)
                        x
            =y->left;
                    
            else
                        x
            =y->right;
                    
                    
            if(x!=NULL)
                        x
            ->parent=y->parent;
                        
                    
            if(!y->parent)
                        root
            =x;
                    
            else
                    {
                        
            if(y==y->parent->left)
                            y
            ->parent->left=x;
                        
            else
                            y
            ->parent->right=x;
                    }
                    
                    
            if(y!=p)
                        p
            ->key=y->key;
                    
                    
            return y;
                }
                
                
            return p;
            }

            int main(int argc,char* argv[])
            {
                
            int input;
                
                BST_Build();
                
                BST_Delete(
            7);
                
                cout
            <<"Inorder Walk:"<<endl;
                BST_Inorder_Walk(root);
                cout
            <<endl;
                
                cout
            <<"Preorder Walk:"<<endl;
                BST_Preorder_Walk(root);
                cout
            <<endl;
                
                cout
            <<"Postorder Walk:"<<endl;
                BST_Postorder_Walk(root);
                cout
            <<endl;
                
                cout
            <<"Min: "<<BST_Min()->key<<endl;
                cout
            <<"Max: "<<BST_Max()->key<<endl;
                
                
            while(1)
                {
                    cin
            >>input;
                    
                    
            if(input==-1)
                        
            break;
                    
                    BST
            * p;
                    
            if(p=BST_Search(input))
                    {
                        cout
            <<"Parent="<<(p->parent)->key<<endl;
                        
            if(p->left)
                            cout
            <<"Left:"<<p->left->key<<endl;
                        
            if(p->right)
                            cout
            <<"Right:"<<p->right->key<<endl;
                    }
                    
            else
                    {
                        cout
            <<"Not found!"<<endl;
                        
            break;
                    }
                    
                    
            if(p=BST_Predecessor(input))
                    {
                        cout
            <<"Predecessor:"<<p->key<<endl;
                    }
                    
                    
            if(p=BST_Successor(input))
                    {
                        cout
            <<"Successor:"<<p->key<<endl;
                    }
                }
                
                BST_Delete(root);
                
                cout
            <<endl;
                system(
            "pause");
                
            return 0;
            }

            posted on 2007-08-24 12:35 Kingoal Lee's Alogrithm Study using cplusplus 閱讀(182) 評論(0)  編輯 收藏 引用


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


            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产V亚洲V天堂无码久久久| 久久亚洲AV永久无码精品| 久久亚洲精品中文字幕| 国产欧美一区二区久久| 久久www免费人成看国产片| 伊人久久成人成综合网222| 日产精品99久久久久久| 大蕉久久伊人中文字幕| 精品久久久中文字幕人妻| 青青草国产精品久久| 久久久SS麻豆欧美国产日韩| 久久电影网一区| 99精品国产综合久久久久五月天| 伊人久久精品线影院| 久久久无码精品亚洲日韩蜜臀浪潮 | 青青草原综合久久大伊人| 久久99国产精品久久99果冻传媒| 欧美久久一区二区三区| 久久综合九色综合久99| 久久99国产精品一区二区| 欧美成a人片免费看久久| 99久久婷婷免费国产综合精品| 亚洲伊人久久综合中文成人网 | 久久99久久无码毛片一区二区| 久久久亚洲欧洲日产国码二区| 久久久久97国产精华液好用吗| 狠狠久久亚洲欧美专区| 亚洲国产另类久久久精品黑人| 无码任你躁久久久久久老妇| 国产激情久久久久影院| 久久国产精品一区二区| jizzjizz国产精品久久| 色诱久久久久综合网ywww| 久久国语露脸国产精品电影| 伊人久久无码精品中文字幕| 久久精品国产一区二区三区不卡| 婷婷久久综合九色综合98| 久久精品国产福利国产秒| 国产精品一区二区久久| 久久精品国产99国产电影网| 99久久精品影院老鸭窝|