DS二叉排序树之创建和插入

303 阅读3分钟

题目描述

给出一个数据序列,建立二叉排序树,并实现插入功能

对二叉排序树进行中序遍历,可以得到有序的数据序列

输入

第一行输入t,表示有t个数据序列

第二行输入n,表示首个序列包含n个数据

第三行输入n个数据,都是自然数且互不相同,数据之间用空格隔开

第四行输入m,表示要插入m个数据

从第五行起,输入m行,每行一个要插入的数据,都是自然数且和前面的数据不等

以此类推输入下一个示例

输出

第一行输出有序的数据序列,对二叉排序树进行中序遍历可以得到

从第二行起,输出插入第m个数据后的有序序列,输出m行

以此类推输出下一个示例的结果

输入样例1

1
6
22 33 55 66 11 44
3
77
50
10

输入样例1(查看格式)

1\n
6\n
22 33 55 66 11 44\n
3\n
77\n
50\n
10\n

输出样例1

11 22 33 44 55 66 
11 22 33 44 55 66 77 
11 22 33 44 50 55 66 77 
10 11 22 33 44 50 55 66 77 

输出样例1(查看格式)

11 22 33 44 55 66 \n
11 22 33 44 55 66 77 \n
11 22 33 44 50 55 66 77 \n
10 11 22 33 44 50 55 66 77 \n

源代码

/*
[id:127] DS二叉排序树之创建和插入
from SZU OnlineJudge
 */

#include <iostream>
using namespace std;
struct Node     //二叉排序树的节点
{
    int data;       //数据域
    Node* lchild;   //左子树
    Node* rchild;   //右子树

    Node(int key)   //key为查找的关键词
    {
        data = key;
        lchild = NULL;
        rchild = NULL;
    }
};

class BinarySortTree
{
    Node* root; //根节点

    //笔者创建二叉排序树是使用一直插入新节点的方法
    bool SearchBST(Node* root, int key, Node* father, Node*& p)
    {
        /*
            搜索是否存在数据域为key的节点
            1.存在返回true,并使p指向这个节点
            2.不存在返回false,并使p指向当前节点的父母节点
            3.注意p为引用
            
            root为当前正在搜索的二叉树的根节点,key同上
            father当前根节点的父母节点
        */

        if (!root)
        {
            //找到空节点,说明不存在
            p = father;
            return false;
        }
        else if (key == root->data)
        {
            //找到数据域为key的节点
            p = root;
            return true;
        }
        else if (key < root->data)
        {
            //key小于当前根节点的数据域
            //往左走
            return SearchBST(root->lchild, key, root, p);
        }
        else if (key > root->data)
        {
            //key大于当前根节点的数据域
            //往右走
            return SearchBST(root->rchild, key, root, p);
        }
    }

    bool InsertBST(Node*& root, int key)
    {
        //注意root为引用
        Node* p = root;
        if (!SearchBST(root, key, NULL, p))
        {
            //若没找到数据域为key的节点
            //就插入一个节点
            Node* s = new Node(key);

            if (!p) //此时该二叉查找树为空树
            {
                root = s;
            }
            else if (key < p->data)
            {
                //key插入为左子树
                p->lchild = s;
            }
            else if (key > p->data)
            {
                //key插入为右子树
                p->rchild = s;
            }
            return true;
        }
        else    //已经有了数据域为key的节点,插入失败
            return false;
    }

    void inorder_travel(Node* root)
    {
        //中序遍历二叉排序树
        if (root)
        {
            inorder_travel(root->lchild);
            cout << root->data << ' ';
            inorder_travel(root->rchild);
        }
    }

public:
    BinarySortTree()
    {
        root = NULL;
    }

    //创建共有接口再调用私有接口
    //原因是root为private属性
    bool InsertBST(int key)
    {
        return InsertBST(root, key);
    }

    void inorder_travel()
    {
        inorder_travel(root);
        cout << endl;
    }
};

int main()
{
    //freopen("D:\\Read.txt", "r", stdin);

    int t;
    cin >> t;

    while (t--)
    {
        BinarySortTree bsTree;

        int n;
        cin >> n;

        int key;
        for (int i = 0; i < n; i++)
        {   //建树
            cin >> key;
            bsTree.InsertBST(key);
        }

        //建完树中序遍历一次
        bsTree.inorder_travel();
        
        int k;
        cin >> k;

        while (k--)
        {
            //插入k个节点
            //每次插入完都遍历一次
            cin >> key;
            bsTree.InsertBST(key);
            bsTree.inorder_travel();
        }
        
    }

    return 0;
}