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

            二叉搜索樹操作(3)----非遞歸遍歷

            應用隊列(queue)和棧(stack)對二叉搜索樹進行非遞歸遍歷
            應用隊列queue的是BFS,棧stack的是DFS
            代碼為:
            #include <iostream>
            #include 
            <cstdlib>
            #include 
            <queue>
            #include 
            <stack>
            #include 
            <algorithm>

            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<<" ";
                }
            }

            void BST_Layer_Walk()
            {
                queue
            <BST*> queue;
                BST
            * p;
                queue.push(root);
                
                
            while(!queue.empty())
                {
                    p
            =queue.front();
                    
                    queue.pop();
                    
            if(p->left)
                        queue.push(p
            ->left);
                    
            if(p->right)
                        queue.push(p
            ->right);
                    
                    cout
            <<p->key<<" ";
                }
                
                cout
            <<endl;
            }

            void Inorder_Walk(BST* node=root)
            {
                stack
            <BST*> stk;
                
                
            while(!stk.empty()||node)
                {
                    
            if(node)
                    {
                        stk.push(node);
                        node
            =node->left;
                    }
                    
            else
                    {
                        node
            =stk.top();
                        cout
            <<node->key<<" ";
                        stk.pop();
                        node
            =node->right;
                    }
                }
            }

            void Preorder_Walk(BST* node=root)
            {
                stack
            <BST*> stk;
                
                
            while(!stk.empty()||node)
                {
                    
            if(node)
                    {
                        cout
            <<node->key<<" ";
                        stk.push(node);
                        node
            =node->left;
                    }
                    
            else
                    {
                        node
            =stk.top();
                        stk.pop();
                        node
            =node->right;
                    }
                }
            }

            void Postorder_Walk(BST* node=root)
            {
                stack
            <BST*> stk;
                BST
            * p;
                
            int flag = 0;//0 stands for left, 1 stands for right

                
            do
                {
                    
            while(node)
                    {
                        stk.push(node);
                        node
            =node->left;
                    }
                    
                    p
            =NULL;
                    flag
            =0;
                    
                    
            while(!stk.empty()&&(flag==0))
                    {
                        node
            =stk.top();
                        
            if(node->right==p)
                        {
                            stk.pop();
                            p
            =node;
                            cout
            <<node->key<<" ";
                        }
                        
            else
                        {
                            node
            = node->right;
                            flag
            =1;
                        }
                    }
                }
            while(!stk.empty());
            }

            int main(int argc,char* argv[])
            {
                
            int input;
                
                BST_Build();
                
                cout
            <<"Inorder Walk:"<<endl;
                
            //BST_Inorder_Walk(root);
                Inorder_Walk();
                cout
            <<endl;
                
                cout
            <<"Preorder Walk:"<<endl;
                BST_Preorder_Walk(root);
                cout
            <<endl;
                
                cout
            <<"Stack Preorder Walk:"<<endl;
                Preorder_Walk(root);
                cout
            <<endl;
                
                cout
            <<"Postorder Walk:"<<endl;
                BST_Postorder_Walk(root);
                cout
            <<endl;
                
                cout
            <<"Stack Postorder Walk:"<<endl;
                Postorder_Walk(root);
                cout
            <<endl;
                
                cout
            <<"Layer Walk:"<<endl;
                BST_Layer_Walk();
                
                BST_Delete(root);
                
                cout
            <<endl;
                system(
            "pause");
                
            return 0;
            }


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

            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            日韩一区二区久久久久久| 国产精品99久久久久久宅男| 99久久精品免费看国产| 国产精品久久久久国产A级| 久久中文字幕视频、最近更新| 久久青青草原国产精品免费| 亚洲精品无码专区久久久| 97精品伊人久久久大香线蕉| 一本久久a久久精品综合香蕉| 欧美激情精品久久久久久久九九九| 成人a毛片久久免费播放| 久久亚洲欧美日本精品| 伊人色综合久久天天| 性欧美丰满熟妇XXXX性久久久 | 合区精品久久久中文字幕一区| 欧美粉嫩小泬久久久久久久 | 亚洲欧美精品一区久久中文字幕 | 久久天天躁狠狠躁夜夜2020一| 久久精品国产AV一区二区三区| 少妇精品久久久一区二区三区| 国产91色综合久久免费分享| 久久九九青青国产精品| 久久国产精品免费一区| 久久久久久久波多野结衣高潮| 欧美喷潮久久久XXXXx| 国产ww久久久久久久久久| 国产69精品久久久久APP下载| 久久久亚洲欧洲日产国码aⅴ | 伊人久久亚洲综合影院| 久久久久AV综合网成人| 人妻无码精品久久亚瑟影视| 亚洲精品乱码久久久久久| 国产成人精品久久综合| 伊人久久大香线蕉av不卡| 精品国产一区二区三区久久| 一级做a爰片久久毛片免费陪| AV狠狠色丁香婷婷综合久久| 青春久久| 久久噜噜久久久精品66| 青青青青久久精品国产h| 伊人久久大香线焦AV综合影院 |