HDU-3791二叉搜索树

#include<iostream>
#include<string>
#include<fstream>
int n;
std::string str;
std::string str1,str2,strcmp1,strcmp2;
class btree
{
	struct node
	{
		node():v(0),left(NULL),right(NULL){}
		int v;
		node* left;
		node* right;
	};
private:
	node* root_;
public:
	
	btree()
	{
		root_=NULL;
	}
	void insert(int v)
	{
		insert(v,root_);
	}
	void print()
	{
		print_1(root_);
		print_2(root_);
	}
	void clear()
	{
		clear(root_);
		root_=NULL;
	}
private:
	void insert(int& v,node *& n)
	{
		if(n==NULL){
			n=new node();
			n->v=v;
			return;
		}
		if(v<=n->v){
			insert(v,n->left);
		}else{
			insert(v,n->right);
		}
	}
	void print_1(node* n)
	{
		if(n==NULL){
			return;
		}
		strcmp1.append(1,'0'+n->v);
		print_1(n->left);
		print_1(n->right);
	}
    void  print_2(node* n)
	{	
		if(n==NULL){
			return;
		}
		print_2(n->left);
		strcmp2.append(1,'0'+n->v);
		print_2(n->right);
	}
	void clear(node* n)
	{
		if(n==NULL){
			return ;
		}
		clear(n->left);
		clear(n->right);
		delete(n);
		n=NULL;
	}
};

btree tree;
void build(std::string &str)
{
	for(int i=0;i!=str.size();i++){
		tree.insert(str[i]-'0');
	}
	tree.print();
	tree.clear();
}
int main()
{
	//std::fstream std::cin("1.txt");
	while (std::cin>>n&&n){
		std::cin>>str;
		build(str);
		str1.assign(strcmp1);
		str2.assign(strcmp2);
		strcmp1.clear();
	    strcmp2.clear();
		for(int i=0;i!=n;i++){
			std::cin>>str;
			build(str);
			if(str1==strcmp1&&str2==strcmp2){
				std::cout<<"YES"<<std::endl;
			}else{
				std::cout<<"NO"<<std::endl;
			}
			strcmp1.clear();
			strcmp2.clear();
		}

		str.clear();
		str1.clear();
		str2.clear();
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值