zoukankan      html  css  js  c++  java
  • 数据结构实验三题目一

    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;
    
    }
    
    
    }
  • 相关阅读:
    关于C#静态函数什么时候被调用的问题
    Visual Studio调试之断点技巧篇
    使用MPLex实现语法高亮显示的功能
    Generate Ellipsoid画椭球用MATLAB
    matlab学习
    12.17 V155 Q169. 机经加感悟。
    GRE阅读
    Matlab7.0程序启动后自动退出问题
    远程打开MATLAB
    Resin是CAUCHO公司的产品,是一个非常流行的application server,对servlet和JSP提供了良好的支持,性能也比较优良,resin自身采用JAVA语言开发。
  • 原文地址:https://www.cnblogs.com/ilovetheworld/p/10838826.html
Copyright © 2011-2022 走看看