数据结构实验三题目一

Problem Description 
 
输入一个整数关键字序列L,生成一棵用链式存储结构存储的二叉排序树,对该二叉排序树能进行查找和插入结点的操作,并对该二叉排序树中结点的关键字按递增和递减顺序输出。
具体要求:
输入数据的第一行为一个正整数T, 表示测试数据的组数。然后是T组测试数据。每组测试数据的第一行输入正整数n(5≤n≤20),第二行输入n个整数,要求依次完成以下工作:
(1) 以这n个整数生成(建立)一棵用链式存储结构存储的二叉排序树;
(2) 按递增顺序输出该二叉排序树中的整数(关键字);
(3) 输入一个整数key,对该二叉排序树进行查找,若在该二叉排序树中存在这个整数key,则输出find,否则输出not find。
(4) 输入一个整数key,若该二叉排序树中不存在这个整数key,则将key插入到该二叉排序树中,使插入后仍为原性质的二叉排序树;否则不必插入;
(5) 在(4)的基础上,按递减顺序输出该二叉排序树中的整数(关键字)。
Input

输入数据的第一行为一个正整数T, 表示测试数据的组数。然后是T组测试数据。每组测试数据的第一行输入正整数n(5≤n≤20),第二行输入n个整数,第三和第四行均输入整数key。
Output

每组输出的第一行为按递增顺序输出该二叉排序树中的整数(关键字),每两个整数之间一个空格;第二行为find或not find;第三行为按递减顺序输出该二叉排序树中的整数(关键字)。
Sample Input
2
8
10 79 6 81 43 75 26 69
43
69
10
94 22 25 24 20 42 39 71 53 57
88
1
Sample Output
6 10 26 43 69 75 79 81
find
81 79 75 69 43 26 10 6
20 22 24 25 39 42 53 57 71 94
not find
94 71 57 53 42 39 25 24 22 20 1
 

 

#include<iostream>
using namespace std;
#include<stdio.h>
#include<stdlib.h>
/*
递归前中后遍历
*/
int N;
int j=0;
int j1=0;
typedef struct node
{
  int data;
  struct node *left;
  struct node *right;
}BTnode;
BTnode* CreateTree(BTnode* root,int x)
{
    if(!root)  //如果root结点为空,创建叶子结点
    {
        root = new BTnode;
        root->data = x;
        root->left=root->right=NULL;
    }else
    {
        if(root->data>x) 
            root->left = CreateTree(root->left,x);  //递归调用左
        else if(root->data<x)
            root->right = CreateTree(root->right,x);//递归调用右
    }
    return root;
}

void Inorder(BTnode* root)
{

  if(root)
  {
      Inorder(root->left);
      if(j<N-1)
      {
            cout<<root->data<<" ";
            j++;
      }
   else{
        cout<<root->data<<endl;
   }
      Inorder(root->right);
  }
}

void Inorder1(BTnode* root)
{
  if(root)
  {
       Inorder1(root->right);
      if(j1<N-1)
      {
            cout<<root->data<<" ";
            j1++;
      }
   else{
        cout<<root->data<<endl;
   }
      Inorder1(root->left);
  }
}

int find(BTnode* root,int a) 
{
    
     while(root!=NULL)
  {
    if(root->data==a){
        return 1;
    }
     else if(root->data>a)
     {
          root=root->left;
     }
     else root=root->right;
  }
  return 0;
}
 

int main(void)
{ 
int o;
cin>>o;
for(int p=0;p<o;p++)
{
     BTnode * head = NULL;
 int x;
 int n;
 int i;
 int a;
 int b;
 scanf("%d",&n);
 N=n;
 for(i=0;i<n;i++)
 {
   scanf("%d",&x);
   head = CreateTree(head,x);
 }
 
Inorder(head);

cin>>a;
if(find(head,a)==1) cout<<"find"<<endl;
else cout<<"not find"<<endl;
cin>>b;
if(find(head,b)==0)
{
    head = CreateTree(head,b);
    N=N+1;
    Inorder1(head);
}
else{
    Inorder1(head);
}

j=0;
j1=0;

}


}

 

转载于:https://www.cnblogs.com/ilovetheworld/p/10838826.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值