C语言数据结构 -BST 树的常规操作

 

 

 

#include<iostream>
#include<queue>


//bst 树
struct node
{
    node* lchild;
    node* rchild;
    int data;
};


void insert(node ** root,int val)
{
    if (root == nullptr)
    {
        return;
    }
    if ((*root) == nullptr)
    {
        (*root) = new node;
        (*root)->lchild = nullptr;
        (*root)->rchild = nullptr;
        (*root)->data = val;
    }
    if ((*root)->data == val)
    {
        return;
    }
    if (val > (*root)->data)
    {
        insert(&(*root)->rchild, val);
    }
    if (val < (*root)->data)
    {
        insert(&(*root)->lchild,val);
    }
}

//先  根左右
void before(node **root)
{
    if (root == nullptr || (*root) == nullptr)
    {
        return;
    }
    std::cout << (*root)->data << " ";
    if ((*root)->lchild != nullptr)
    {
        before(&(*root)->lchild);
    }
    if ((*root)->rchild != nullptr)
    {
        before(&(*root)->rchild);
    }
}

//中 左根右
void in(node** root)
{
    if (root == nullptr || (*root) == nullptr)
    {
        return;
    }
    if ((*root)->lchild != nullptr)
    {
        in(&(*root)->lchild);
    }
    std::cout << (*root)->data << " ";
    if ((*root)->rchild != nullptr)
    {
        in(&(*root)->rchild);
    }
}
//后 左右根
void after(node **root)
{
    if (root == nullptr || (*root) == nullptr)
    {
        return;
    }
    if ((*root)->lchild != nullptr)
    {
        after(&(*root)->lchild);
    }
    if ((*root)->rchild != nullptr)
    {
        after(&(*root)->rchild);
    }
    std::cout << (*root)->data << " ";
}

//层 layer
void layer(node** root)
{
    if (root == nullptr || (*root) == nullptr)
    {
        return;
    }
    std::queue<node*> que;
    que.push(*root);
    while (!que.empty())
    {
        node* head = que.front();
        que.pop();
        std::cout << head->data << " ";
        if (head->lchild != nullptr)
        {
            que.push(head->lchild);
        }
        if (head->rchild != nullptr)
        {
            que.push(head->rchild);
        }
    }
}
//搜索
node* find(node** root, int data)
{
    if (root == nullptr || (*root) == nullptr)
    {
        return nullptr;
    }
    if ((*root)->data == data)
    {
        return (*root);
    }
    if (data > (*root)->data && (*root)->rchild != nullptr)
    {
        return find(&(*root)->rchild,data);
    }
    if (data < (*root)->data && (*root)->lchild == nullptr)
    {
        return find(&(*root)->lchild,data);
    }
    return nullptr;
}
node* findmax(node **root)
{
    if (root == nullptr || (*root) == nullptr)
    {
        return nullptr;
    }
    if ((*root)->rchild == nullptr)
    {
        return (*root);
    }else
    {
        return findmax(&(*root)->rchild);
    }
    return nullptr;
}
//删除
void deletes(node** root ,int data)
{
    if (root == nullptr || (*root) == nullptr)
    {
        return;
    }
    if ((*root)->data == data)
    {
        //如果他是叶子节点
        if ((*root)->lchild == nullptr && (*root)->rchild == nullptr)
        {
            delete (*root);
            root = nullptr;
            return;
        }
        //如果他只有左孩纸
        else if ((*root)->rchild == nullptr)
        {
            node* temp = (*root)->lchild;
            delete (*root);
            *root = temp;
            return;
        }
        //如果他只有右孩子
        else if ((*root)->lchild == nullptr)
        {
            node* temp = (*root)->rchild;
            delete (*root);
            *root = temp;
            return;
        }
        //那么就是左右都有孩子了
        else
        {
            //先从他的左孩子里面找出一个最大的孩子 他的最大孩子肯定是没有右孩子的
            node* temp = findmax(&(*root)->lchild);
            (*root)->data = temp->data;
            deletes(&(*root)->lchild, temp->data);
            return;
        }
    }
    else if (data > (*root)->data)
    {
        //data在右孩子
        deletes(&(*root)->rchild,data);
    }
    else if (data < (*root)->data)
    {
        //data在左孩子
        deletes(&(*root)->lchild,data);
    }
    return;
}
int main()
{
    node* root = nullptr;
    while (true)
    {
        int v ;
        std::cin >> v;
        if (v == 0)
        {
            break;
        }
        insert(&root,v);
    }
    //before(&root);
    //std::cout << std::endl;
    //in(&root);
    //std::cout << std::endl;
    //after(&root);
    in(&root);
    deletes(&root, 7);
    in(&root);
    node* it = find(&root,6);
}

 

posted @ 2022-11-06 13:30  Atggg  阅读(66)  评论(0)    收藏  举报