• <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)論排行榜

            热久久最新网站获取| 中文字幕久久精品 | 久久99精品国产麻豆不卡| 蜜臀av性久久久久蜜臀aⅴ| 久久久久久伊人高潮影院| 99国产精品久久| 久久综合亚洲色HEZYO国产| 伊人久久精品无码二区麻豆| 97久久精品人妻人人搡人人玩| 国内精品伊人久久久久影院对白| 久久综合亚洲鲁鲁五月天| 久久97精品久久久久久久不卡| 日韩欧美亚洲综合久久影院Ds| 99久久人妻无码精品系列蜜桃| 久久久久久国产精品美女| 精品无码久久久久国产| 欧美国产成人久久精品| 成人综合伊人五月婷久久| 伊人色综合久久天天网| 精品国产综合区久久久久久| 亚洲精品乱码久久久久久蜜桃图片 | 久久精品国产亚洲av水果派| 99久久精品国产一区二区| 精品熟女少妇AV免费久久| 日韩久久无码免费毛片软件| 亚洲国产二区三区久久| AV无码久久久久不卡蜜桃| 狠狠色综合网站久久久久久久高清 | 2021少妇久久久久久久久久| 久久久久久精品免费免费自慰| 久久久国产一区二区三区| 草草久久久无码国产专区| 久久精品一区二区| 久久99热狠狠色精品一区| 久久中文字幕一区二区| 免费观看久久精彩视频| 青青青伊人色综合久久| 精品国产婷婷久久久| 久久精品国产只有精品66| 色婷婷综合久久久久中文字幕| 亚洲日韩欧美一区久久久久我 |