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

  1. #include <iostream>

  2. #include <typeinfo>

  3. #include <string>

  4. using namespace std;

  5. template < typename T >

  6. class bst

  7. {

  8. struct Node{

  9. T data;

  10. //int password;

  11. //int name;

  12. Node* L;

  13. Node* R;

  14. Node(const T&d):data(d),L(),R(){}

  15. Node(const T&d,Node* l,Node* r):data(d),L(l),R(r){}

  16. };

  17. Node* rp;

  18. int n;

  19. public:

  20. bst():rp(),n(0){}

  21. void insert(const T&d){

  22. insert(rp,new Node(d));

  23. }

  24. void insert(Node*& p,Node* d) //增

  25. {

  26. if(p==NULL) {p=d;n++;}

  27. else if(p->data<d->data) insert(p->R,d);//将string转化成c风格字符串,再用strcmp比较

  28. else insert(p->L,d);

  29. }

  30. void find(const T&d){

  31. if(find(rp,d)!=NULL) cout<<"find OK!"<<endl;

  32. else cout<<"find fail!"<<endl;

  33. }

  34. bool remove(const T&d) //删

  35. {

  36. Node*& p=find(rp,d);

  37. if(p==NULL) return 0;

  38. Node* pn=p;

  39. if(p->L!=NULL)//**************insert没有对第二个参数进行检验,如果第二个参数为空,会出现最右边的子节点无法删除*********************

  40. insert(p->R,p->L);

  41. p=p->R;

  42. delete pn;

  43. //travel(rp);

  44. --n;

  45. return 1;

  46. }

  47. void updata(const T&t,const T&d) //改

  48. {

  49. if(remove(t)) //当remove中travel存在时 if语句中的insert都不起作用

  50. {

  51. //insert(rp,new Node(d));

  52. insert(d);

  53. }

  54. }

  55. Node*& find(Node*& p,const T&d) //查找

  56. {

  57. if(p==NULL||d==p->data) return p;

  58. else if(d<p->data) find(p->L,d);

  59. else find(p->R,d);

  60.  
  61. }

  62. void travel(Node* rp) const{ //遍历

  63. if(rp){

  64. travel(rp->L);

  65. cout<<rp->data<<' ';

  66. travel(rp->R);

  67. }

  68. }

  69. void travel() const{

  70. travel(rp);

  71. cout << endl;

  72. }

  73. ~bst(){clear();}

  74. void clear(){

  75. clear(rp);

  76. }

  77. void clear(Node* p){ //清空

  78. if(p!=NULL)

  79. {

  80. clear(p->L);

  81. clear(p->R);

  82. delete p;

  83. }

  84. }

  85. int size()

  86. {

  87. return n;

  88. }

  89. int high()

  90. {

  91. return high(rp);

  92. }

  93. int high(Node*& p)const //树的高度

  94. {

  95. if(p==NULL) return 0;

  96. int LH = high(p->L);

  97. int RH = high(p->R);

  98. return 1+(LH>RH?LH:RH);

  99. }

  100. };

  101.  
  102. template < >

  103. class bst<string>

  104. {

  105. typedef string T;

  106. struct Node{

  107. T data;

  108. //int password;

  109. //int name;

  110. Node* L;

  111. Node* R;

  112. Node(const T&d):data(d),L(),R(){}

  113. Node(const T&d,Node* l,Node* r):data(d),L(l),R(r){}

  114. };

  115. Node* rp;

  116. int n;

  117. public:

  118. bst():rp(),n(0){}

  119. void insert(const T&d){

  120. insert(rp,new Node(d));

  121. }

  122. void insert(Node*& p,Node* d) //增

  123. {

  124. if(p==NULL) {p=d;n++;}

  125. else if((strcmp((p->data).c_str(),(d->data).c_str())>-1?0:1)) insert(p->R,d);//将string转化成c风格字符串,再用strcmp比较

  126. else insert(p->L,d);

  127. }

  128. void find(const T&d){

  129. if(find(rp,d)!=NULL) cout<<"find OK!"<<endl;

  130. else cout<<"find fail!"<<endl;

  131. }

  132. bool remove(const T&d) //删

  133. {

  134. Node*& p=find(rp,d);

  135. if(p==NULL) return 0;

  136. Node* pn=p;

  137. insert(p->R,p->L);

  138. p=p->R;

  139. delete pn;

  140. //travel(rp);

  141. --n;

  142. return 1;

  143. }

  144. void updata(const T&t,const T&d) //改

  145. {

  146. if(remove(t)) //当remove中travel存在时 if语句中的insert都不起作用

  147. {

  148. //insert(rp,new Node(d));

  149. insert(d);

  150. }

  151. }

  152. Node*& find(Node*& p,const T&d) //查找

  153. {

  154. if(p==NULL||d==p->data) return p;

  155. else if(d<p->data) find(p->L,d);

  156. else find(p->R,d);

  157.  
  158. }

  159. void travel(Node* rp) const{ //遍历

  160. if(rp){

  161. travel(rp->L);

  162. cout<<rp->data<<' ';

  163. travel(rp->R);

  164. }

  165. }

  166. void travel() const{

  167. travel(rp);

  168. cout << endl;

  169. }

  170. ~bst(){clear();}

  171. void clear(){

  172. clear(rp);

  173. }

  174. void clear(Node* p){ //清空

  175. if(p!=NULL)

  176. {

  177. clear(p->L);

  178. clear(p->R);

  179. delete p;

  180. }

  181. }

  182. int size()

  183. {

  184. return n;

  185. }

  186. int high()

  187. {

  188. return high(rp);

  189. }

  190. int high(Node*& p)const //树的高度

  191. {

  192. if(p==NULL) return 0;

  193. int LH = high(p->L);

  194. int RH = high(p->R);

  195. return 1+(LH>RH?LH:RH);

  196. }

  197. };

  198. //class Person{

  199. // int age;

  200. // string name;

  201. // /*Person* L;

  202. // Person* R;*/

  203. //public:

  204. // Person(string str,const int&d):age(d),name(str){}

  205. // Person(const int&d,Person* l,Person* r,string str):age(d),name(str){}

  206. // friend bool operator<(const Person& a,const Person& b){

  207. // return a.age<b.age;

  208. // }

  209. // friend bool operator==(const Person& a,const Person& b){

  210. // return a.age==b.age;

  211. // }

  212. //};

  213. class Person{

  214. string name;

  215. int age;

  216. public:

  217. Person( int age, const char* name):name(name),age(age){}

  218. friend ostream&operator<<(ostream&o,const Person&p){

  219. return o<<p.name<<':'<<p.age;

  220. }

  221. friend bool operator<(const Person& a,const Person& b){

  222. //return a.age<b.age; //出错

  223. if(a.age < b.age) return 1;

  224. else return 0;

  225. }

  226. friend bool operator==(const Person& a,const Person& b){

  227. return a.age==b.age;

  228. }

  229. };

  230. int main()

  231. {

  232. bst<string> admin; //其他的都差不多,指针类型不一样

  233. admin.insert("admin");

  234. admin.insert("admin123");

  235. admin.insert("admin888");

  236. admin.insert("XSS");

  237. admin.insert("beef");

  238. admin.insert("IIS5.X/IIS6.0");

  239. admin.insert("a.asp;.jpg");

  240. admin.insert("a.asp/a.jpg");

  241. admin.insert("apache");

  242. admin.insert("a.asp.abd.aws.aqz");

  243. admin.insert("IIS7.X");

  244. admin.insert("*.jpg/*.php");

  245. admin.insert("123");

  246. admin.travel();

  247. cout<<endl;

  248. admin.find("admin");

  249. admin.remove("123");

  250. admin.travel();

  251. cout<<"\n**********************\n";

  252. admin.updata("admin","while");

  253. admin.travel();

  254. cout << "树的大小:" << admin.size() << endl;

  255. cout << "树的高度:" << admin.high() << endl;

  256. bst<int> in;

  257. in.insert(120);

  258. in.insert(220);

  259. in.insert(320);

  260. in.insert(20);

  261. in.travel();

  262. in.remove(120);

  263. in.travel();

  264. Person a(12,"Alice");

  265. Person b(1210,"彭殇");

  266. Person c(430,"帝弑天");

  267. Person d(120,"帝释天");

  268. bst<Person> test;

  269. test.insert(a);

  270. test.insert(b);

  271. test.insert(c);

  272. test.insert(d);

  273. test.travel();

  274. test.find(c);

  275. test.remove(b);

  276. test.travel();

  277. cout << "树的大小:" << test.size() << endl;

  278. cout << "树的高度:" << test.high() << endl;

  279. system("pause");

  280. return 0;

  281. }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值