• <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 閱讀(184) 評(píng)論(0)  編輯 收藏 引用


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


            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            欧美大战日韩91综合一区婷婷久久青草| 国产精品成人精品久久久 | 99久久国产综合精品网成人影院| 精品久久久久中文字幕日本| 久久精品国产91久久综合麻豆自制| 99久久国产综合精品五月天喷水| 2021国产精品久久精品| www久久久天天com| 一本色综合久久| 久久国产精品成人免费| 偷偷做久久久久网站| 久久精品www| 伊人久久综合精品无码AV专区| 久久九九青青国产精品| 精品一二三区久久aaa片| 大蕉久久伊人中文字幕| 国产精品一久久香蕉国产线看观看 | 国产精品久久一区二区三区| 久久青青草原精品国产不卡| 精品熟女少妇av免费久久| 一本色道久久88—综合亚洲精品| 国产精品va久久久久久久| 99久久精品午夜一区二区| 99精品久久精品一区二区| 久久久久亚洲AV综合波多野结衣| 久久99国产综合精品女同| 狠狠色婷婷久久综合频道日韩| 久久人人爽人人爽人人片AV麻豆 | 久久久久久国产精品无码下载 | 久久丫精品国产亚洲av| 99久久国产宗和精品1上映| 久久亚洲国产成人影院| 国产精品久久久久久久app | 久久久久久伊人高潮影院| 色欲综合久久躁天天躁| 国产日韩久久免费影院| 久久精品国产亚洲7777| 亚洲乱码日产精品a级毛片久久 | 久久婷婷人人澡人人| 久久天天躁狠狠躁夜夜2020| 午夜精品久久影院蜜桃|