zoukankan      html  css  js  c++  java
  • HDU5444 Elven Postman(二叉树的查找插入)

    题意:首先输入t组数据
       接下来是输入n和n个数,这n个数构成树;

               再输如m和m个数,每个数表示需要寻找的数;

    输出找到需要寻找的数要走的路向左向右,向左输出W,向右输出E;

     

    #include <iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<queue>
    
    using namespace std;
    
    typedef struct node{
        int date;
        struct node *lchild;
        struct node *rchild;
    }*Tree,tree;
    
    node *newnode(int v)
    {
        node* Node=new node;
        Node->date=v;
        Node->lchild=Node->rchild=NULL;
        return Node;
    }
    
    void insert(Tree &root,int x)
    {
        if(root==NULL) {
            root=newnode(x);
        }
        else if(root->date>x)
        {
            insert(root->lchild,x);
        }
        else if(root->date<x)
        {
            insert(root->rchild,x);
        }
    }
    
    void search(Tree &root,int x)
    {
        if(root->date < x)
        {
            printf("W");
            search(root->rchild,x);
        }
        else if(root->date > x)
        {
            printf("E");
            search(root->lchild,x);
        }
    }
    
    int main()
    {
        int T,n,m;
        cin>>T;
        while(T--)
        {
            cin>>n;
            Tree root=NULL;
            int s;
            for(int i=0;i<n;i++)
            {
                cin>>s;
                insert(root,s);
            }
            cin>>m;
            for(int i=0;i<m;i++){
                cin>>s;
                search(root,s);
                cout<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    市值
    01-HTML基础与进阶-day3-录像237
    01-HTML基础与进阶-day3-录像236
    01-HTML基础与进阶-day3-录像235
    01-HTML基础与进阶-day2-HTML第二讲
    01-HTML基础与进阶-day2-HTML第一讲
    01H5-fe-html5-006插入视频
    MySQL 备份和恢复数据
    MySQL 索引
    MySQL 视图
  • 原文地址:https://www.cnblogs.com/Fy1999/p/9437719.html
Copyright © 2011-2022 走看看