C++前序中序后序二叉树+遍历+删除

class HeroNode
{
public:
    HeroNode(int id, string name)
    {
        this->id = id;
        this->name = name;
    }
    //前序遍历
    void preOrder()
    {
        cout << this->id << "  " << this->name << endl;
        if (this->left != nullptr)
        {
            this->left->preOrder();
        }
        if (this->right != nullptr)
        {
            this->right->preOrder();
        }
    }
    //中序遍历
    void infixOrder()
    {
        if (this->left != nullptr)
        {
            this->left->infixOrder();
        }
        cout << this->id << "  " << this->name << endl;
        if (this->right != nullptr)
        {
            this->right->infixOrder();
        }
    }
    //后序遍历
    void postOrder()
    {
        if (this->left != nullptr)
        {
            this->left->postOrder();
        }
        if (this->right != nullptr)
        {
            this->right->postOrder();
        }
        cout << this->id << "  " << this->name << endl;
    }
    //前序查找
    HeroNode* preOrderfind(int id)
    {
        if (this->id == id)
        {
            return this;
        }
        HeroNode * resNode = nullptr;
        if (this->left != nullptr)
        {
            resNode=this->left->preOrderfind(id);
        }
        if (resNode!=nullptr)
        {
            return resNode;
        }
        if (this->right != nullptr)
        {
            resNode=this->right->preOrderfind(id);
        }
        return resNode;
    }
    //中序查找
    HeroNode* infixOrderfind(int id)
    {
        HeroNode * resNode = nullptr;
        if (this->left != nullptr)
        {
            resNode=this->left->infixOrderfind(id);
        }
        if (resNode != nullptr)
        {
            return resNode;
        }
        if (this->id == id)
        {
            return this;
        }
        if (this->right != nullptr)
        {
            resNode=this->right->infixOrderfind(id);
        }
        return resNode;
    }
    //后序查找
    HeroNode* postOrderfind(int id)
    {
        HeroNode * resNode = nullptr;
        if (this->left != nullptr)
        {
            resNode=this->left->postOrderfind(id);
        }
        if (resNode != nullptr)
        {
            return resNode;
        }
        if (this->right != nullptr)
        {
            resNode=this->right->postOrderfind(id);
        }
        if (resNode != nullptr)
        {
            return resNode;
        }
        if (this->id == id)
        {
            return this;
        }
        return resNode;
    }
    //如果是叶子节点,就删除该节点
    //如果是非叶子节点,就删除该子树
    //如果当前节点的左子节点不为空,并且为要删除的节点,this->left = nullptr;返回
    //如果当前节点的右子节点不为空,并且为要删除的节点,this->right = nullptr;返回
    //如果上面两步均没找到,那么向左子树进行递归
    //如果上一步均没找到,那么向右子树进行递归
    void delNode(int id)
    {
        if (this->left != nullptr && this->left->id == id)
        {
            this->left = nullptr;
            return;
        }
        if (this->right != nullptr &&this->right->id == id)
        {
            this->right = nullptr;
            return;
        }
        if (this->left != nullptr)
        {
            this->left->delNode(id);
        }
        if (this->right != nullptr)
        {
            this->right->delNode(id);
        }
    }
    int id;
    string name;
    HeroNode *left=nullptr;
    HeroNode *right=nullptr;
};

class BinaryTree
{
public:
    void setRoot(HeroNode *root)
    {
        this->root = root;
    }
    void PreOrder()
    {
        if (this->root != nullptr)
        {
            this->root->preOrder();
        }
        else
        {
            cout << "当前二叉树为空,无法遍历" << endl;
        }
    }
    void InfixOrder()
    {
        if (this->root != nullptr)
        {
            this->root->infixOrder();
        }
        else
        {
            cout << "当前二叉树为空,无法遍历" << endl;
        }
    }
    void PostOrder()
    {
        if (this->root != nullptr)
        {
            this->root->postOrder();
        }
        else
        {
            cout << "当前二叉树为空,无法遍历" << endl;
        }
    }
    HeroNode * PreOrderfind(int id)
    {
        if (root != nullptr)
        {
            return root->preOrderfind(id);
        }
        else
        {
            return nullptr;
        }
    }
    HeroNode * InfixOrderfind(int id)
    {
        if (root != nullptr)
        {
            return root->infixOrderfind(id);
        }
        else
        {
            return nullptr;
        }
    }
    HeroNode * PostOrderfind(int id)
    {
        if (root != nullptr)
        {
            return root->postOrderfind(id);
        }
        else
        {
            return nullptr;
        }
    }
    void delNode(int id)
    {
        if (root != nullptr)
        {
            if (root->id == id)
            {
                root == nullptr;
            }
            else
            {
                root->delNode(id);
            }
        }
        else
        {
            cout << "空树,不能删除";
        }
    }
private:
    HeroNode * root;
};

int main()
{
    BinaryTree tree;
    HeroNode *root = new HeroNode(1, "宋");
    HeroNode *node2 = new HeroNode(2, "李");
    HeroNode *node3 = new HeroNode(3, "赵");
    HeroNode *node4 = new HeroNode(4, "孙");
    HeroNode *node5 = new HeroNode(5, "关");
    tree.setRoot(root);
    root->left = node2;
    root->right = node3;
    node3->right = node4;
    node3->left = node5;
    /*cout << "前序遍历" << endl;
    tree.PreOrder();
    cout << "中序遍历" << endl;
    tree.InfixOrder();
    cout << "后序遍历" << endl;
    tree.PostOrder();
    cout << "前序查找" << endl;
    HeroNode *newNode=tree.PreOrderfind(3);
    if (newNode == nullptr)
    {
        cout << "未找到你想找的id" << endl;
    }
    else
    {
        cout <<newNode->id<<" "<< newNode->name << endl;
    }
    cout << "中序查找" << endl;
    HeroNode *newNode1 = tree.InfixOrderfind(3);
    if (newNode1 == nullptr)
    {
        cout << "未找到你想找的id" << endl;
    }
    else
    {
        cout << newNode1->id << " " << newNode1->name << endl;
    }
    cout << "后序查找" << endl;
    HeroNode *newNode2 = tree.PostOrderfind(3);
    if (newNode2 == nullptr)
    {
        cout << "未找到你想找的id" << endl;
    }
    else
    {
        cout << newNode2->id << " " << newNode2->name << endl;
    }*/
    cout << "删除前:" << endl;
    tree.PreOrder();
    tree.delNode(7);
    cout << "删除后:" << endl;
    tree.PreOrder();

    system("pause");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值