hdu5444-Elven Postman-最最朴素的二叉搜索树

就是裸的二叉搜索树

只需要在插入的时候 给节点附加一个W或E的信息


查询时输出该信息就可以


这么水的题居然没看到。。。

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
#define  inf 0x7fffffff
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 50005;

struct node
{
	int val;
	node *l,*r;
	char p;
	node ()
	{
		l=r=NULL;
	}
};


class Btree
{
public:   
	void insert(int& sb,node* &hd,int p)
	{
		
		if (hd==NULL)
		{
			hd=new node;
			hd->val=sb;
			 
			if (p)
			{
				if (p==1) 
					hd->p='E';
				else
					hd->p='W';
			}
		
		

		}
		else
		{
			if (sb<=hd->val)
				insert(sb,hd->l,1);
			else
				insert(sb,hd->r,2);
		}
		
	}
	
	int query(int& sb,node *hd)
	{
		if (hd->val==sb)
		{
			printf("%c",hd->p);
			return 0;
		}
		else
		{
			if(hd->p)
				printf("%c",hd->p); 
			if (sb<=hd->val)
				query(sb,hd->l);
			else
				query(sb,hd->r);
		} 
		
	}
	
	
} ;

node *head=NULL;

Btree tp;
int tm[1005];
int main(void)
{
	int t,n,i ;
	scanf("%d",&t);
	while(t--)
	{

		head=NULL; 
 
		scanf("%d",&n);
			int tmp;  
		for (i=1;i<=n;i++)
		{
		
			scanf("%d",&tm[i]); 
			tp.insert(tm[i], head,0);
			

		}
		head->p=0;
	
		int m;
		scanf("%d",&m);
		for (i=1;i<=m;i++)
		{
			scanf("%d",&tmp);
			if (tmp!=tm[1]) 
			 
			tp.query(tmp,head);
			printf("\n");
		}
	}
	
	
    return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值