自定义结构体通过模板实现

#include <iostream>
#include <typeinfo>
#include <string>
using namespace std;
template < typename T >
class bst
{
	struct Node{
		T data;
		//int password;
		//int name;
		Node* L;
		Node* R;
		Node(const T&d):data(d),L(),R(){}
		Node(const T&d,Node* l,Node* r):data(d),L(l),R(r){}
	};
	Node* rp;
	int n;
public:
	bst():rp(),n(0){}
	void insert(const T&d){
		insert(rp,new Node(d));
	}
	void insert(Node*& p,Node* d)		//增
	{
		if(p==NULL) {p=d;n++;}
		else if(p->data<d->data) insert(p->R,d);//将string转化成c风格字符串,再用strcmp比较
		else insert(p->L,d);
	}
	void find(const T&d){
		if(find(rp,d)!=NULL) cout<<"find OK!"<<endl;
		else cout<<"find fail!"<<endl;
	}
	bool remove(const T&d)				//删
	{
		Node*& p=find(rp,d);
		if(p==NULL) return 0;
		Node* pn=p;
		if(p->L!=NULL)//**************insert没有对第二个参数进行检验,如果第二个参数为空,会出现最右边的子节点无法删除*********************
		insert(p->R,p->L);
		p=p->R;
		delete pn;
		//travel(rp);
		--n;
		return 1;
	}
	void updata(const T&t,const T&d)	//改
	{
		if(remove(t))		//当remove中travel存在时 if语句中的insert都不起作用
		{
			//insert(rp,new Node(d));
			insert(d);
		}
	}
	Node*& find(Node*& p,const T&d)		//查找
	{
		if(p==NULL||d==p->data) return p;
		else if(d<p->data) find(p->L,d);
		else find(p->R,d);

	}
	void travel(Node* rp) const{		//遍历
		if(rp){
			travel(rp->L);
			cout<<rp->data<<' ';
			travel(rp->R);
		}
	}
	void travel() const{
		travel(rp);
		cout << endl;
	}
	~bst(){clear();}
	void clear(){
		clear(rp);
	}
	void clear(Node* p){				//清空
		if(p!=NULL)
		{
			clear(p->L);
			clear(p->R);
			delete p;
		}
	}
	int size()
	{
		return n;
	}
	int high()
	{
		return high(rp);
	}
	int high(Node*& p)const		//树的高度
	{
		if(p==NULL) return 0;
		int LH = high(p->L);
		int RH = high(p->R);
		return 1+(LH>RH?LH:RH);
	}
};

template < >
class bst<string>
{
	typedef string T;
	struct Node{
		T data;
		//int password;
		//int name;
		Node* L;
		Node* R;
		Node(const T&d):data(d),L(),R(){}
		Node(const T&d,Node* l,Node* r):data(d),L(l),R(r){}
	};
	Node* rp;
	int n;
public:
	bst():rp(),n(0){}
	void insert(const T&d){
		insert(rp,new Node(d));
	}
	void insert(Node*& p,Node* d)		//增
	{
		if(p==NULL) {p=d;n++;}
		else if((strcmp((p->data).c_str(),(d->data).c_str())>-1?0:1)) insert(p->R,d);//将string转化成c风格字符串,再用strcmp比较
		else insert(p->L,d);
	}
	void find(const T&d){
		if(find(rp,d)!=NULL) cout<<"find OK!"<<endl;
		else cout<<"find fail!"<<endl;
	}
	bool remove(const T&d)				//删
	{
		Node*& p=find(rp,d);
		if(p==NULL) return 0;
		Node* pn=p;
		insert(p->R,p->L);
		p=p->R;
		delete pn;
		//travel(rp);
		--n;
		return 1;
	}
	void updata(const T&t,const T&d)	//改
	{
		if(remove(t))		//当remove中travel存在时 if语句中的insert都不起作用
		{
			//insert(rp,new Node(d));
			insert(d);
		}
	}
	Node*& find(Node*& p,const T&d)		//查找
	{
		if(p==NULL||d==p->data) return p;
		else if(d<p->data) find(p->L,d);
		else find(p->R,d);

	}
	void travel(Node* rp) const{		//遍历
		if(rp){
			travel(rp->L);
			cout<<rp->data<<' ';
			travel(rp->R);
		}
	}
	void travel() const{
		travel(rp);
		cout << endl;
	}
	~bst(){clear();}
	void clear(){
		clear(rp);
	}
	void clear(Node* p){				//清空
		if(p!=NULL)
		{
			clear(p->L);
			clear(p->R);
			delete p;
		}
	}
	int size()
	{
		return n;
	}
	int high()
	{
		return high(rp);
	}
	int high(Node*& p)const		//树的高度
	{
		if(p==NULL) return 0;
		int LH = high(p->L);
		int RH = high(p->R);
		return 1+(LH>RH?LH:RH);
	}
};
//class Person{
//	int age;
//	string name;
//	/*Person* L;
//	Person* R;*/
//public:
//	Person(string str,const int&d):age(d),name(str){}
//	Person(const int&d,Person* l,Person* r,string str):age(d),name(str){}
//	friend bool operator<(const Person& a,const Person& b){
//		return a.age<b.age;
//	}
//	friend bool operator==(const Person& a,const Person& b){
//		return a.age==b.age;
//	}
//};
class Person{
	string name;
	int age;
public:
	Person( int age, const char* name):name(name),age(age){}
	friend ostream&operator<<(ostream&o,const Person&p){
		return o<<p.name<<':'<<p.age;
	}
	friend bool operator<(const Person& a,const Person& b){
		//return a.age<b.age;	//出错
		if(a.age < b.age) return 1;
		else return 0;
	}
	friend bool operator==(const Person& a,const Person& b){
		return a.age==b.age;
	}
};
int main()
{
	bst<string> admin;			//其他的都差不多,指针类型不一样
	admin.insert("admin");
	admin.insert("admin123");
	admin.insert("admin888");
	admin.insert("XSS");
	admin.insert("beef");
	admin.insert("IIS5.X/IIS6.0");
	admin.insert("a.asp;.jpg");
	admin.insert("a.asp/a.jpg");
	admin.insert("apache");
	admin.insert("a.asp.abd.aws.aqz");
	admin.insert("IIS7.X");
	admin.insert("*.jpg/*.php");
	admin.insert("123");
	admin.travel();
	cout<<endl;
	admin.find("admin");
	admin.remove("123");
	admin.travel();
	cout<<"\n**********************\n";
	admin.updata("admin","while");
	admin.travel();
	cout << "树的大小:" << admin.size() << endl;
	cout << "树的高度:" << admin.high() << endl;
	bst<int> in;
	in.insert(120);
	in.insert(220);
	in.insert(320);
	in.insert(20);
	in.travel();
	in.remove(120);
	in.travel();
	Person a(12,"Alice");
	Person b(1210,"彭殇");
	Person c(430,"帝弑天");
	Person d(120,"帝释天");
	bst<Person> test;
	test.insert(a);
	test.insert(b);
	test.insert(c);
	test.insert(d);
	test.travel();
	test.find(c);
	test.remove(b);
	test.travel();
	cout << "树的大小:" << test.size() << endl;
	cout << "树的高度:" << test.high() << endl;
	system("pause");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值