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

            二叉搜索樹操作(1)----生成

            二叉搜索樹是專門有利于搜索(時間復(fù)雜度為logn)的二叉樹。
            生成一棵二叉搜索樹關(guān)鍵是不斷地插入數(shù)據(jù)到該樹中,若當(dāng)前的root為NULL,則當(dāng)前插入的節(jié)點為root,否則比較左右樹插入,直到為NULL為之。
            二叉搜索樹的插入代碼為:
            void BST_Insert(int key)
            {
                構(gòu)建當(dāng)前節(jié)點current,初始化current,設(shè)置其value=key,左右父節(jié)點都為NULL;
               //用x,y兩個指針來跟蹤current放置的位置
               y=NULL;
              x=root;

              while(x)//x有下面的節(jié)點時
                {  
                     y=x;
                     x的key和當(dāng)前參數(shù)里面的key做對比,若大于當(dāng)前key,則x=left[x],否則x=right[x];
                }

              比較y和NULL的關(guān)系,若y==NULL,則root=NULL,有root=current;
              設(shè)置parent[current]=y;
              比較current的key和y的key的大小,若大,則left[y]=current,否則right[y]=current;
            //插入完畢
            }
            使用代碼表示為:
            #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;
                
                
            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);
                }
            }

            int main(int argc,char* argv[])
            {
                BST_Build();
                BST_Inorder_Walk(root);
                BST_Delete(root);
                
                cout
            <<endl;
                system(
            "pause");
                
            return 0;
            }


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


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


            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久久国产精品亚洲一区| 久久综合九色综合久99| 精品永久久福利一区二区| 国产成人香蕉久久久久 | 亚洲国产精品久久久天堂| 亚洲伊人久久大香线蕉苏妲己| 久久乐国产综合亚洲精品| 97久久久精品综合88久久| 亚洲日韩中文无码久久| 久久久国产99久久国产一| 亚洲色欲久久久综合网东京热| 久久国产精品久久精品国产| 久久亚洲精精品中文字幕| 亚洲va久久久噜噜噜久久男同| 2022年国产精品久久久久| 国产99久久久国产精品小说| 国产午夜精品久久久久九九电影| 青青草国产精品久久久久| 成人资源影音先锋久久资源网| 久久AⅤ人妻少妇嫩草影院| 香蕉久久av一区二区三区 | 狠狠色婷婷久久一区二区 | 色综合久久久久综合体桃花网| 久久青青草原精品国产不卡| 欧美一区二区精品久久| 国产精品久久久久9999高清| 亚洲伊人久久成综合人影院| 99久久国产亚洲综合精品| 精品久久久无码中文字幕| 四虎国产精品免费久久久| 欧美熟妇另类久久久久久不卡 | 亚洲国产二区三区久久| 久久精品亚洲一区二区三区浴池| 久久久无码精品亚洲日韩蜜臀浪潮| 久久婷婷五月综合97色直播| 久久国产精品免费一区二区三区| 99久久er这里只有精品18| 亚洲欧美成人综合久久久| 亚洲乱码精品久久久久..| 亚洲精品乱码久久久久久按摩| 亚洲av成人无码久久精品|