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

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

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

              比較y和NULL的關系,若y==NULL,則root=NULL,有root=current;
              設置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 閱讀(541) 評論(0)  編輯 收藏 引用

            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久亚洲色一区二区三区| 狠狠色丁香久久婷婷综合五月| 国内精品久久久久伊人av| 久久久综合九色合综国产| 国产精品久久久天天影视香蕉| 久久精品无码一区二区三区日韩 | 国产精品成人久久久久三级午夜电影 | 色婷婷久久综合中文久久一本| 久久久久99这里有精品10 | 免费国产99久久久香蕉| 日本久久中文字幕| 国产精品99久久久久久人| 免费一级欧美大片久久网 | 国内精品久久久久久久97牛牛| 麻豆精品久久精品色综合| 久久久久久久久66精品片| 国内精品伊人久久久久| 亚洲AⅤ优女AV综合久久久| 国产精品美女久久久久网| 99久久香蕉国产线看观香| 久久福利片| 欧美久久综合性欧美| 亚洲精品国产字幕久久不卡 | 久久精品这里只有精99品| 久久久无码精品亚洲日韩蜜臀浪潮 | 亚洲精品国产成人99久久| 香蕉久久夜色精品升级完成| 无码人妻久久一区二区三区蜜桃| av无码久久久久不卡免费网站 | 亚洲第一永久AV网站久久精品男人的天堂AV| 国产成年无码久久久免费| 亚洲国产精品成人AV无码久久综合影院| 精品国产乱码久久久久久郑州公司 | 婷婷久久综合九色综合九七| 国产高潮国产高潮久久久91 | 欧美国产精品久久高清| 久久精品中文字幕一区| 久久国产精品视频| 青青青青久久精品国产| 亚洲乱亚洲乱淫久久| 国产精品久久精品|