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

题目描述

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

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

输入

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

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

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

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

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

以此类推输入下一个示例

输出

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

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

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

样例输入

1

6

22 33 55 66 11 44

3

77

50

10

样例输出

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

插入实现:

temp初始化为根结点

每次判断要插入的值和结点temp的值的大小

如果比temp大,且temp的右孩子为空,那么直接插在temp的右边

如果比temp大,且temp的右孩子不为空,那么temp=temp->Rightchild,继续判断

如果比temp小,且temp的左孩子为空,那么直接插在temp的左边

如果比temp小,且temp的左孩子不为空,那么temp=temp->Leftchild,继续判断

创建实现:

将第一个数作为根结点,将后面的数依次插入到排序树中即可

代码:

#include <iostream>
#include <queue>
#include <string>
using namespace std;
class BSTNode{
public:
    int data;
    BSTNode *LeftChild;
    BSTNode *RightChild;
public:
    BSTNode():LeftChild(NULL),RightChild(NULL){};
    ~BSTNode(){};
};
class BST{
public:
    BSTNode *Root;
public:
    BST(){};
    ~BST(){};
    void Create();
    void Inorder(BSTNode *t);
    void Insert();
};
void BST::Create()     //创建排序树,这里为了加快速度,采用的是不调用Insert函数,而将Insert函数的代码内嵌在创建的代码中
{
    int n,temp_num;
    cin>>n;
    Root = new BSTNode();
    cin>>Root->data;
    BSTNode *temp;
    for(int i=0;i<n-1;i++)
    {
        cin>>temp_num;
        temp=Root;
        while(1)
        {
            if(temp->data > temp_num)
            {
                if(temp->LeftChild !=NULL)        //比temp大,且temp的右孩子不为空
                    temp=temp->LeftChild;
                else                              //比temp大,且temp的右孩子为空
                {
                    BSTNode *lc = new BSTNode();
                    temp->LeftChild = lc;
                    lc->data = temp_num;
                    break;
                }
            }
            else
            {
                if(temp->RightChild !=NULL)        //比temp小,且temp的左孩子不为空
                    temp=temp->RightChild;
                else                               //比temp小,且temp的左孩子为空
                {
                    BSTNode *lc = new BSTNode();
                    temp->RightChild = lc;
                    lc->data = temp_num;
                    break;
                }
            }
        }
    }
}
void BST::Inorder(BSTNode *t)
{
    if(t->LeftChild != NULL)
        Inorder(t->LeftChild);
    cout<<t->data<<' ';
    if(t->RightChild != NULL)
        Inorder(t->RightChild);
}
void BST::Insert()            //插入操作
{
    int temp_num;
    cin>>temp_num;
    BSTNode *temp;
    temp=Root;
    while(1)
    {
        if(temp->data > temp_num)
        {
            if(temp->LeftChild !=NULL)        //比temp大,且temp的右孩子不为空
                temp=temp->LeftChild;
            else                              //比temp大,且temp的右孩子为空
            {
                BSTNode *lc = new BSTNode();
                temp->LeftChild = lc;
                lc->data = temp_num;
                break;
            }
        }
        else
        {
            if(temp->RightChild !=NULL)        //比temp小,且temp的左孩子不为空
                temp=temp->RightChild;
            else                               //比temp小,且temp的左孩子为空
            {
                BSTNode *lc = new BSTNode();
                temp->RightChild = lc;
                lc->data = temp_num;
                break;
            }
        }
    }
}
int main()
{
    int t,insert_num;
    cin>>t;
    while(t--)
    {
        BST t;
        t.Create();
        t.Inorder(t.Root);
        cout<<endl;

        cin>>insert_num;
        while(insert_num--)
        {
            t.Insert();
            t.Inorder(t.Root);
            cout<<endl;
        }
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值