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

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            91精品无码久久久久久五月天| 久久久久人妻一区精品果冻| 亚洲а∨天堂久久精品9966| 欧美日韩精品久久久免费观看| 久久99精品久久久久子伦| 久久黄视频| 久久无码AV中文出轨人妻| 国产精品久久国产精麻豆99网站| 亚洲精品无码久久毛片| 国产福利电影一区二区三区久久老子无码午夜伦不 | 久久一本综合| 久久精品国产欧美日韩| 色8久久人人97超碰香蕉987| yy6080久久| 国内精品久久久久影院网站| 国产精品成人精品久久久| 久久只有这里有精品4| 欧美亚洲国产精品久久高清| 久久国产高潮流白浆免费观看| 色诱久久久久综合网ywww| 久久国产综合精品五月天| 国产∨亚洲V天堂无码久久久| 久久成人国产精品免费软件| 久久亚洲精品国产精品婷婷| 国产日产久久高清欧美一区| 国产69精品久久久久99| 久久久精品人妻一区二区三区四 | 狠狠色丁香婷婷综合久久来来去| 亚洲国产成人久久精品99| 亚洲国产二区三区久久| 国产综合久久久久| 久久九九精品99国产精品| 精品伊人久久大线蕉色首页| 天天影视色香欲综合久久| 欧美久久综合九色综合| 久久午夜综合久久| 伊人久久精品影院| 亚洲国产精品无码久久一区二区| 国产精品久久久久9999高清| 久久精品国产99久久无毒不卡| 国产精品女同久久久久电影院|