0
点赞
收藏
分享

微信扫一扫

DS二叉排序树之删除

晚熟的猫 2022-01-13 阅读 20
数据结构
#include <iostream>
using namespace std;

class Binary_tree_node
{
public:
    int data;                                    //数据域
    Binary_tree_node *lChild, *rChild, *parents; //左孩子、右孩子、父母
    Binary_tree_node() : lChild(NULL), rChild(NULL) {}
    Binary_tree_node(int e) : data(e), lChild(NULL), rChild(NULL), parents(NULL) {}
};

class Binary_tree
{
    Binary_tree_node *root;
    //建树、插入、排序操作
    void InsertNode(int data, Binary_tree_node *&r)
    {
        if (data > r->data && r->rChild)
        {
            InsertNode(data, r->rChild);
        }
        else if (data < r->data && r->lChild)
        {
            InsertNode(data, r->lChild);
        }
        else if (data > r->data && !r->rChild)
        {
            Binary_tree_node *s = new Binary_tree_node(data);
            r->rChild = s;
        }
        else if (data < r->data && !r->lChild)
        {
            Binary_tree_node *s = new Binary_tree_node(data);
            r->lChild = s;
        }
    }
    //获取s的中序遍历直接前驱
    Binary_tree_node *getBefore(Binary_tree_node *s)
    {
        Binary_tree_node *p = s->lChild;
        while (p->rChild)
        {
            p = p->rChild;
        }
        return p;
    }

    //中序遍历
    void InOrder(Binary_tree_node *t)
    {
        if (t)
        {
            InOrder(t->lChild);
            cout << t->data << " ";
            InOrder(t->rChild);
        }
    }
    //删除操作
    void Delete(int key, Binary_tree_node *&t)
    {
        if (key > t->data && t->rChild)
        {
            Delete(key, t->rChild);
        }
        else if (key < t->data && t->lChild)
        {
            Delete(key, t->lChild);
        }
        else if (key == t->data)
        {
            if (!t->lChild && !t->rChild) //叶子结点
            {
                delete t;
                t = NULL;
            }
            else if (t->lChild && !t->rChild) //只有左子树
            {
                Binary_tree_node *p = t;
                t = t->lChild;
                delete p;
            }
            else if (t->rChild && !t->lChild) //只有右子树
            {
                Binary_tree_node *p = t;
                t = t->rChild;
                delete p;
            }
            else if (t->lChild && t->rChild) //左右子树都有
            {
                Binary_tree_node *p = getBefore(t);
                t->data = p->data;
                if (p->lChild)
                {
                    p->parents->rChild = p->lChild;
                }
                if (t->lChild == p) //当直接前驱就是他的左孩子时候,把该节点左孩子设为NULL
                {
                    t->lChild = NULL;
                }
                delete p;
            }
        }
    }

public:
    Binary_tree(int data) { root = new Binary_tree_node(data); }
    void Delete(int key)
    {
        Delete(key, root);
    }
    void Insert(int key)
    {
        InsertNode(key, root);
    }
    void InOrder()
    {
        InOrder(root);
        cout << endl;
    }
};

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, m;
        cin >> n;
        int data;
        cin >> data;
        Binary_tree bt(data);
        for (int i = 1; i < n; i++)
        {
            cin >> data;
            bt.Insert(data);
        }
        bt.InOrder();
        cin >> m;
        while (m--)
        {
            int key;
            cin >> key;
            bt.Delete(key);
            bt.InOrder();
        }
    }
    return 0;
}
举报

相关推荐

0 条评论