Data structures - Find - Find a binary sort

0, why binary sort tree

Find in front of us are static look, because the data set is stored orderly , there are several ways to find, use binary, interpolation, Fibonacci, etc., but because of an orderly, efficient in the insert and delete operations and not tall.
Then we need a dynamic search method, either look for efficient implementation, and can make a good insert and delete efficiency , then we can consider binary sort tree

1, binary sort tree (Binary Sort Tree)

Also known as a binary search tree, which is either an empty tree or a binary tree with the following properties:

1) if its left subtree is not empty, then the values ​​of all the left child node of the tree is less than the value of its root structure;

2) If it is not empty right subtree, the right subtree of the value of all the nodes are greater than the value of its root structure;

3) its left and right sub-trees are binary sort tree (recursively).

Although the data we get is disordered, but we follow the way binary sort tree organization, preorder binary tree is ordered. Of course, more objective to construct the binary sort tree is easy to find speed in the future, insert and delete keywords.

1) Find:

/*
BiTree T    我们要搜索的二叉树
ElemType key我们要搜索的关键字
BiTree F    记录下我们的当前搜索子树的双亲结点
BiTree* P    当我们插入之前,会先搜索是否存在数据,若存在,不插入,若不存在,我们通过这个可以获取我们要插入的位置,直接插入即可
*/
Status SearchBST(BiTree T, ElemType key, BiTree F, BiTree* P)
{
    if (!T)
    {
        *P = F;        //若是未找到则返回父节点位置
        return FALSE;
    }
    else
    {
        if (T->data == key)
        {
            *P = T;    //若是找到则P返回该结点位置
            return TRUE;
        }
        else if (T->data < key)
            return SearchBST(T->rchild, key, T, P);
        else
            return SearchBST(T->lchild, key, T, P);
    }
}

2)插入

Status InsertBST(BiTree* T, int key)
{
    BiTree P,s;
    if (!T)
        return ERROR;
    if (!SearchBST(*T, key, NULL, &P))
    {
        //没有查找到有重复数据,获取到了应该插入的位置
        s = (BiTree)malloc(sizeof(BiTNode));
        s->data = key;
        s->lchild = s->rchild = NULL;
        if (!P)    //空树
            *T = s;
        else if (key < P->data)    //插入左子树
            P->lchild = s;
        else
            P->rchild = s;
        return OK;
    }
    else
        return ERROR;
}

3)删除

如果是叶子结点,则直接删除;

如果结点只存在左子树或右子树,则直接补上;

如果同时存在左子树和右子树,则按照中序遍历的顺序将删除结点的前驱或者后继补上即可;

Status Delete(BiTree* T)
{
    BiTree q,f;
    if (!*T)
        return ERROR;
    if (!(*T)->lchild)    //若是左子树不存在,我们只需要接到右子树
    {
        q = *T;
        *T = (*T)->rchild;
        free(q);
    }
    else if (!(*T)->rchild)    //若右子树不存在,接入左子树
    {
        q = *T;
        *T = (*T)->lchild;
        free(q);
    }
    else  //两边都存在,我们可以选择将右子树最小,或者左子树最大接入,这里选择右子树最小
    {
        f = *T;    //f指向q的双亲结点
        q = (*T)->rchild;
        while (q)
        {
            f = q;
            q = q->lchild;    //找到右子树最小,注意其可能存在右子树,我们要进行保存,接入其父节点
        }
        //将最小的数据更新到根节点处即可,然后记录最小点处,删除即可
        (*T)->data = q->data;
        if (f != (*T))
            f->lchild = q->rchild;
        else
            f->rchild = q->rchild;    //当右子树是一个右斜树
        free(q);
    }
    return TRUE;
}

Status DeleteBST(BiTree* T, int key)
{
    if (!*T)
        return ERROR;
    else
    {
        if ((*T)->data == key)    //找到了,开始删除
        {
            //删除该结点,由于要分情况讨论,所以另外写一个函数
        }
        else if ((*T)->data < key)
            DeleteBST(&(*T)->rchild, key);
        else
            DeleteBST(&(*T)->lchild, key);
    }
}

 

Guess you like

Origin www.cnblogs.com/lemonzhang/p/12396491.html
Recommended