1
#include "stdafx.h"
2
#include <iostream>
3
using namespace std;
4
5
template <typename T>
6
class BinarySearchTree
7

{
8
friend ostream& operator<<(ostream& , BinarySearchTree<T>& );
9
public:
10
BinarySearchTree();
11
void insert(const T );
12
void midvisit()
13
{
14
15
midvisit(root);
16
}
17
18
protected:
19
20
private:
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
}
50
TNode* root;
51
52
53
};
54
template<typename T2>
55
ostream& operator<<(ostream& os, BinarySearchTree<T2>& a)
56

{
57
58
59
}
60
template<typename T>
61
BinarySearchTree<T>::BinarySearchTree()
62

{
63
64
root=NULL;
65
}
66
template<typename T>
67
void 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
}
83
template<typename T>
84
void BinarySearchTree<T>::insert(const T a)
85

{
86
insert(a,root);
87
88
89
90
91
}
92
int _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
黄大仙 阅读(766)
评论(0) 编辑 收藏 引用 所属分类:
c++