#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);
}

 

原文地址:http://www.cnblogs.com/atggg/p/16862468.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性