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

            Just enjoy programming

            二叉查找樹實(shí)現(xiàn)

            #include<stdio.h>
            #include<stdlib.h>
            #include<string.h>

            /*結(jié)構(gòu)體節(jié)點(diǎn)*/
            typedef struct  _node{
                int key;
                struct _node *left;
                struct _node *right;
                struct _node *parent;
            }node;


            /*插入節(jié)點(diǎn)*/
            void insert(node *root,node *toInsert)
            {
                node *p,*q;
                p=root;
                q=NULL;
                if(toInsert==NULL || root==NULL)
                {
                    return;
                }

                while(p!=NULL)
                {
                    q=p;/*記錄父節(jié)點(diǎn)*/
                    if(toInsert->key<p->key)
                    {
                        p=p->left;
                    }else{
                        p=p->right;
                    }    
                }
                if(toInsert->key<q->key)
                {
                    q->left=toInsert;
                }else{
                    q->right=toInsert;
                }
                toInsert->parent=q;
                toInsert->left=NULL;
                toInsert->right=NULL;
            }

            /*遞歸中序遍歷根節(jié)點(diǎn)*/
            void middleSearch(node *root)
            {
                if(root!=NULL)
                {
                    middleSearch(root->left);
                    printf("%d\t",root->key);
                    middleSearch(root->right);
                }
            }
            /*先序遍歷*/
            void preSearch(node *root)
            {
                if(root!=NULL)
                {
                    printf("%d\t",root->key);
                    preSearch(root->left);
                    preSearch(root->right);
                }
            }

            /*查找返回節(jié)點(diǎn)關(guān)鍵字為key的節(jié)點(diǎn)*/
            node* search(node *root,int key)
            {
                node *p=root;
                while(p!=NULL)
                {
                    if(key<p->key)
                    {
                        p=p->left;
                    }else if(key>p->key)
                    {
                        p=p->right;
                    }else
                        break;
                }
                return p;
            }

            /*查找二叉樹最小值*/
            node *minimun(node *root)
            {
                node *p=root;
                if(p==NULL)
                    return p;
                while(p->left!=NULL)
                    p=p->left;
                printf("min %d\n",p->key);
                return p;
            }

            /*查找二叉樹最大值*/
            node *maximun(node *root)
            {
                node *p=root;
                if(p==NULL)
                    return;
                while(p->right!=NULL)
                    p=p->right;
                printf("max %d\n",p->key);
                return p;
            }
            /*找節(jié)點(diǎn)后續(xù)*/
            node* successor(node *x)
            {
                node *p;
                if(NULL==x)
                    return x;
                if(x->right!=NULL)
                    return minimun(x->right);
                p=x->parent;
                while(p!=NULL && p->right==x)
                {
                    x=p;
                    p=p->parent;
                }
                return p;
            }

            /*刪除節(jié)點(diǎn)*/
            void delete(node *root,node *toDelete)
            {
                node *p,*q;
                int key;
                if(root==NULL || toDelete==NULL)
                    return ;
                p=toDelete->parent;

                /*第一種情況,要?jiǎng)h除的節(jié)點(diǎn)的左右子樹都為空*/
                if(toDelete->left ==NULL && toDelete->right ==NULL)
                {
                    if(p==NULL)/*要?jiǎng)h除的是根節(jié)點(diǎn)*/
                    {
                        free(toDelete);
                        return;
                    }
                    if(p->left==toDelete)
                    {
                        p->left=NULL;
                    }else
                        p->right=NULL;
                    free(toDelete);

                }

                /*第二種情況,要?jiǎng)h除的左子樹為空,右子樹不為空*/
                else if(toDelete->left==NULL)
                {    
                    if(p==NULL)
                    {
                        q=root->right;
                        root->key=q->key;
                        root->left=q->left;
                        root->right=q->right;

                        free(q);
                        return;
                    }
                    else if(p->left==toDelete)
                    {
                        p->left=toDelete->right;
                    }else
                        p->right=toDelete->right;
                    toDelete->right->parent=p;
                    free(toDelete);
                }

                /* 第三種情況,要?jiǎng)h除的右子樹為空,左子樹不為空*/
                else if(toDelete->right==NULL)
                {
                    if(p==NULL)
                    {
                        q=root->left;
                        root->key=q->key;
                        root->left=q->left;
                        root->right=q->right;
                        root->parent=NULL;
                        free(q);
                        return;
                    }
                    else if(p->left==toDelete)
                    {
                        p->left=toDelete->left;
                    }else
                        p->right=toDelete->right;
                    toDelete->parent=p;
                    free(toDelete);
                }

                /* 第四種情況,要?jiǎng)h除的左右子樹都不為空*/
                else{
                        q=successor(toDelete);
                        key=q->key;
                        delete(root,q);
                        toDelete->key=key;
                }
            }

            int main()
            {
                node *root;

                int a[12]={15,5,3,12,10,13,6,7,16,20,18,23};
                node *toInsert;
                node *x,*y;
                int i;
                /*創(chuàng)建頭節(jié)點(diǎn)*/
                if((root=(node*)malloc(sizeof(node)))==NULL)
                {
                    perror("malloc error");
                    exit(1);
                }
                root->key=a[0];
                /*插入節(jié)點(diǎn)*/
                for(i=1;i<12;i++)
                {
                    if((toInsert=(node*)malloc(sizeof(node)))==NULL)
                    {
                        perror("malloc error");
                        exit(1);
                    }
                    toInsert->key=a[i];
                    insert(root,toInsert);
                }

                /*中序遍歷*/
                middleSearch(root);
                printf("\n");
                /*先序遍歷*/
                preSearch(root);
                printf("\n");

                /*最大值*/
                maximun(root);
                /*最小值*/
                minimun(root);

                /*查找關(guān)鍵字節(jié)點(diǎn)及其前驅(qū)*/
                x=search(root,6);
                y=successor(x);
                if(y!=NULL)
                    printf("節(jié)點(diǎn) 6 后驅(qū) %d\n",y->key);

                x=search(root,3);
                y=successor(x);
                if(y!=NULL)
                    printf("節(jié)點(diǎn) 3 后驅(qū) %d\n",y->key);


                x=search(root,13);
                y=successor(x);
                if(y!=NULL)
                    printf("節(jié)點(diǎn) 13 后驅(qū) %d\n",y->key);


                x=search(root,23);
                y=successor(x);
                if(y!=NULL)
                    printf("節(jié)點(diǎn) 23 后驅(qū) %d\n",y->key);

                /*刪除節(jié)點(diǎn)*/
                printf("before delete the node\n");
                x=search(root,13);

                delete(root,x);
                printf("\nafter delete the node\n");
                
                printf("中序遍歷\n");
                middleSearch(root);
                printf("\n先序遍歷\n");
                preSearch(root);

                if((toInsert=(node*)malloc(sizeof(node)))==NULL)
                {
                    perror("malloc error");
                    exit(1);
                }
                toInsert->key=13;
                insert(root,toInsert);
                printf("\n中序遍歷\n");
                middleSearch(root);
                printf("\n先序遍歷\n");
                preSearch(root);


                printf("\nbefore delete the node\n");
                x=search(root,16);
                delete(root,x);
                printf("\nafter delete the node\n");
                printf("中序遍歷\n");
                middleSearch(root);
                printf("\n先序遍歷\n");
                preSearch(root);

                if((toInsert=(node*)malloc(sizeof(node)))==NULL)
                {
                    perror("malloc error");
                    exit(1);
                }
                toInsert->key=16;
                insert(root,toInsert);
                printf("\n中序遍歷\n");
                middleSearch(root);
                printf("\n先序遍歷\n");
                preSearch(root);

                printf("\nbefore delete the node\n");
                x=search(root,5);
                delete(root,x);
                printf("\nafter delete the node\n");
                printf("中序遍歷\n");
                middleSearch(root);
                printf("\n先序遍歷\n");
                preSearch(root);
                if((toInsert=(node*)malloc(sizeof(node)))==NULL)
                {
                    perror("malloc error");
                    exit(1);
                }
                toInsert->key=5;
                insert(root,toInsert);

                printf("\n中序遍歷\n");
                middleSearch(root);
                printf("\n先序遍歷\n");
                preSearch(root);

                printf("\nbefore delete the node\n");
                x=search(root,15);

                delete(root,x);
                printf("\nafter delete the node\n");
               
                printf("中序遍歷\n");
                middleSearch(root);
                printf("\n先序遍歷\n");
                preSearch(root);

                printf("\n");


                printf("before delete the node\n");
                x=search(root,16);

                delete(root,x);
                printf("\nafter delete the node\n");
               
                printf("中序遍歷\n");
                middleSearch(root);
                printf("\n先序遍歷\n");
                preSearch(root);
                printf("\n");



                printf("before delete the node\n");
                x=search(root,18);

                delete(root,x);
                printf("\nafter delete the node\n");
               
                printf("中序遍歷\n");
                middleSearch(root);
                printf("\n先序遍歷\n");
                preSearch(root);
                printf("\n");

                printf("before delete the node\n");
                x=search(root,20);

                delete(root,x);
                printf("\nafter delete the node\n");
               
                printf("中序遍歷\n");
                middleSearch(root);
                printf("\n先序遍歷\n");
                preSearch(root);
                printf("\n");


                printf("before delete the node\n");
                x=search(root,23);

                delete(root,x);
                printf("\nafter delete the node\n");
               
                printf("中序遍歷\n");
                middleSearch(root);
                printf("\n先序遍歷\n");
                preSearch(root);
                printf("\n");
            }
             

            posted on 2011-03-28 16:15 周強(qiáng) 閱讀(292) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 算法

            开心久久婷婷综合中文字幕| 亚洲国产小视频精品久久久三级 | 日本国产精品久久| 一本综合久久国产二区| 亚洲精品乱码久久久久久| 国产精品久久久久久影院 | 亚洲国产成人久久综合一区77| 人妻中文久久久久| …久久精品99久久香蕉国产| 久久精品国产WWW456C0M| 精品综合久久久久久97| 国产精品无码久久四虎| 久久天堂AV综合合色蜜桃网 | 久久SE精品一区二区| 精品久久一区二区| 亚洲精品美女久久久久99| 久久99精品国产麻豆不卡| 国产午夜免费高清久久影院| 久久强奷乱码老熟女| 亚洲狠狠综合久久| 久久久久亚洲Av无码专| 亚洲精品99久久久久中文字幕 | 99re久久精品国产首页2020| 亚洲伊人久久成综合人影院 | 精品久久久久久久久免费影院| 青青草国产精品久久久久| 亚洲级αV无码毛片久久精品| 亚洲国产精品无码久久青草| 国产农村妇女毛片精品久久| 国产一久久香蕉国产线看观看| 久久精品卫校国产小美女| 亚洲国产天堂久久久久久| 久久久久久久国产免费看| 99久久精品免费观看国产| 91精品国产乱码久久久久久| 精品久久久无码人妻中文字幕 | 午夜不卡久久精品无码免费| 久久久久亚洲AV无码观看| 久久综合偷偷噜噜噜色| 一本久久a久久精品综合香蕉| 亚洲国产高清精品线久久|