1#include "stdafx.h"
2#include <iostream>
3using namespace std;
4
5template <typename T>
6class BinarySearchTree
7{
8 friend ostream& operator<<(ostream& , BinarySearchTree<T>& );
9public:
10 BinarySearchTree();
11 void insert(const T );
12 void midvisit()
13 {
14
15 midvisit(root);
16 }
17
18protected:
19
20private:
21 class TNode
22 {
23 friend class BinarySearchTree;
24 public:
25 TNode(T a)
26 :data(a),
27 left(NULL),
28 right(NULL)
29 {
30
31 }
32 protected:
33 private:
34 T data;
35 TNode* left;
36 TNode* right;
37 };
38 void insert(const T ,TNode*&);
39 void midvisit(TNode* x)
40 {
41 if (x==NULL)
42 {
43 return;
44 }
45 midvisit(x->left);
46 cout<<x->data;
47 midvisit(x->right);
48
49 }
50TNode* root;
51
52
53};
54template<typename T2>
55ostream& operator<<(ostream& os, BinarySearchTree<T2>& a)
56{
57
58
59}
60template<typename T>
61BinarySearchTree<T>::BinarySearchTree()
62{
63
64root=NULL;
65}
66template<typename T>
67void BinarySearchTree<T>::insert(const T a,TNode*& t)
68{
69 if (t==NULL)
70 {
71 t=new TNode(a);
72 return;
73 }
74 if (a>t->data)
75 {
76 insert(a,t->right);
77 }
78 else
79 insert(a,t->left);
80
81
82}
83template<typename T>
84void BinarySearchTree<T>::insert(const T a)
85{
86 insert(a,root);
87
88
89
90
91}
92int _tmain(int argc, _TCHAR* argv[])
93{
94 BinarySearchTree<int > a;
95 for (int i=0;i<10;++i)
96 {
97 a.insert(i);
98 }
99 cout<<"insert data"<<endl;
100 a.midvisit();
101 return 0;
102}
103
posted on 2009-05-13 17:21
黄大仙 阅读(745)
评论(0) 编辑 收藏 引用 所属分类:
c++