美文网首页数据结构和算法分析互联网科技程序员
小朋友学数据结构(15):二叉排序树

小朋友学数据结构(15):二叉排序树

作者: 海天一树X | 来源:发表于2018-09-16 18:56 被阅读27次

    二叉排序树会用到指针的指针,在学习二叉排序树之前,请先了解
    小朋友学C语言(41):二级指针与多级指针

    代码:

    #include<iostream>
    using namespace std;
    
    typedef struct BiTNode
    {
        int data;
        struct BiTNode *lchild, *rchild;
    }BiTnode, *Bitree;
    typedef bool status;
    
    status biSearch(Bitree t, int key, Bitree f, Bitree *p)
    {
        if (!t)
        {
            *p = f;
            return false;
        }
        else if (key == t->data)
        {
            *p = t;
            return true;
        }
        else if(key < t->data)
        {
            return biSearch(t->lchild, key, t, p);
        }
        else
        {
            return biSearch(t->rchild, key, t, p);
        }
    }
    
    status biInsert(Bitree *root, int key)
    {
        Bitree p, s;
        if (!biSearch(*root, key, NULL, &p))
        {
            s = (Bitree)malloc(sizeof(BiTnode));
            s->data = key;
            s->lchild = s->rchild = NULL;
            if (!p)
            {
                *root = s;
            }
            else if (key < p->data)
            {
                p->lchild   = s;
            }
            else
            {
                p->rchild = s;
            }
            return true;
        }
        else
        {
            return false;
        }
    }
    
    status Delete(Bitree *p)
    {
        Bitree q, s;
        if ((*p)->lchild == NULL)
        {
            q = *p;
            *p = (*p)-> lchild;
            free(q);
        }
        else if ((*p)->rchild == NULL)
        {
            q = *p;
            *p = (*p)-> rchild;
            free(q);
        }
        else
        {
            q = *p;
            s = (*p)->lchild;
            while (s->rchild)
            {
                q = s;
                s = s->rchild;
            }
            (*p)->data = s->data;
            if (q != *p)
            {
                q->rchild = s->lchild;
            }
            else
            {
                q->lchild = s->lchild;
            }
            free(s);
        }
        return true;
    }
    
    status biDel(Bitree *t, int key)
    {
        if (!*t)
        {
            return false;
        }
        else
        {
            if (key == (*t)->data)
            {
                return Delete(t);
            }
            else if (key < (*t)->data)
            {
                return biDel(&(*t)->lchild, key);
            }
            else
            {
                return biDel(&(*t)->rchild, key);
            }
        }
    }
    
    void inOrder(Bitree &root)
    {
        if (NULL != root)
        {
            inOrder(root->lchild);
            printf("%d ", root->data);  //访问结点
            inOrder(root->rchild);
        }
    }
    
    int main()
    {
        int a[] = {62, 88, 58, 47, 35, 73, 51, 99, 37, 93, 29, 49, 56, 36, 48, 50};
        int len = sizeof(a) / sizeof(int);
        Bitree t = NULL;
        for (int i = 0; i < len; i++)
        {
            biInsert(&t, a[i]);
        }
    
        inOrder(t);
        cout << endl;
    
        Bitree p = NULL;
        cout << biSearch(t, 99, NULL, &p) << endl;
    
        cout << biDel(&t, 47) << endl;
    
        inOrder(t);
        cout << endl;
    
        return 0;
    }
    

    运行结果:

    29 35 36 37 47 48 49 50 51 56 58 62 73 88 93 99
    1
    1
    29 35 36 37 48 49 50 51 56 58 62 73 88 93 99
    

    了解小朋友学编程请加QQ307591841(微信与QQ同号),或QQ群581357582。

    相关文章

      网友评论

        本文标题:小朋友学数据结构(15):二叉排序树

        本文链接:https://www.haomeiwen.com/subject/lcufnftx.html