1 #include <iostream>
2 #include <algorithm>
3 #include <stack>
4 using namespace std;
5 ////////
6 template <typename T>
7 class BinarySearchTree{
8 /////////
9 private:
10 struct BinaryNode{
11 T element;
12 BinaryNode* left;
13 BinaryNode* right;
14 BinaryNode(T ele, BinaryNode* lt, BinaryNode* rt): element(ele),
15 left(lt), right(rt){}
16 };
17 BinaryNode* root;
18 BinaryNode* clone(BinaryNode* t)const{
19 if(t == NULL)
20 return NULL;
21 return new BinaryNode(t->element, clone(t->left), clone(t->right));
22 }
23 void makeEmpty(BinaryNode* &t){//mind here
24 if(t != NULL){
25 makeEmpty(t->left);
26 makeEmpty(t->right);
27 delete t;//mind here...
28 }
29 t = NULL;
30 }
31 ////////
32 public:
33 BinarySearchTree(){
34 root = new BinaryNode(0, NULL, NULL);
35 }
36 BinarySearchTree(const BinarySearchTree& rhs): root(clone(rhs.root)){}
37 const BinarySearchTree& operator=(const BinarySearchTree& rhs){
38 if(this != &rhs){
39 makeEmpty(this->root);
40 root = clone(rhs.root);
41 }
42 return *this;
43 }
44 ~BinarySearchTree(){
45 if(root != NULL)
46 makeEmpty(root);
47 }
48 /// concrete algorithms
49 BinaryNode* findMax(const BinaryNode* t){
50 if(t == NULL)
51 return NULL;
52 if(t->right == NULL)
53 return t;
54 return findMax(t->right);
55 }
56 ///
57 BinaryNode* findMin(const BinaryNode* t){
58 if(t == NULL)
59 return NULL;
60 if(t->left == NULL)
61 return t;
62 return findMin(t->left);
63 }
64
65
66 bool isBinarySearchTree(const BinaryNode* root){
67 if(root != NULL){
68 if(root->right == NULL && root->left == NULL)
69 return true;
70 if(root->left != NULL && root->right == NULL
71 && root->left->element < root->element)
72 return isBinarySearchTree(root->left);
73 if(root->left == NULL && root->right != NULL
74 && root->element < root->right->element)
75 return isBinarySearchTree(root->right);
76 if(root->left && root->right
77 && root->element < root->right->element
78 && root->left->element < root->element)
79 return isBinarySearchTree(root->left) && isBinarySearchTree(root->right);
80 return false;
81 }
82 return true;
83 }
84 void numofNode(const BinaryNode* root, int& num){
85 if(root != NULL){
86 if(root->left == NULL && root->right == NULL)
87 ++num;
88 if(root->left != NULL && root->right == NULL){
89 ++num;
90 numofNode(root->left, num);
91 }
92 if(root->left == NULL && root->right != NULL){
93 ++num;
94 numofNode(root->right, num);
95 }
96 if(root->left != NULL && root->right !=NULL){
97 ++num;
98 numofNode(root->left, num);
99 numofNode(root->right, num);
100 }
101 }
102 }
103
104 void numofLeafNode(const BinaryNode* root, int& num){
105 if(root != NULL){
106 if(root->left == NULL && root->right == NULL){
107 ++num;
108 }
109 if(root->left != NULL && root->right == NULL){
110 numofLeafNode(root->left, num);
111 }
112 if(root->left == NULL && root->right != NULL){
113 numofLeafNode(root->right, num);
114 }
115 if(root->left != NULL && root->right !=NULL){
116 numofLeafNode(root->left, num);
117 numofLeafNode(root->right, num);
118 }
119 }
120 }
121
122 void numoffullNode(const BinaryNode* root, int& num){
123 if(root != NULL){
124 if(root->left == NULL && root->right == NULL){
125 ++num;
126 }
127 if(root->left != NULL && root->right == NULL){
128 numoffullNode(root->left, num);
129 }
130 if(root->left == NULL && root->right != NULL){
131 numoffullNode(root->right, num);
132 }
133 if(root->left != NULL && root->right !=NULL){
134 numofLeafNode(root->left, num);
135 numofLeafNode(root->right, num);
136 }
137 }
138 }
139
140
141 ////
142 bool contains(const T& x, const BinaryNode* t){
143 if(t == NULL)
144 return false;
145 if(x < t->element)
146 return contains(x, t->left);
147 else if(x > t->right)
148 return contains(x, t->right);
149 else
150 return true;
151 }
152 ///
153 void insert_s(const T& x, BinaryNode* & t){
154 if(t == NULL)
155 t = new BinaryNode(x, NULL, NULL);
156 if(x < t->element)
157 insert_s(x, t->left);
158 else if(x > t->element)
159 insert_s(x, t->right);
160 else
161 ;
162 }
163 void insert(const T& x){
164 insert_s(x, root);
165 }
166 ///
167 void remove_s(const T& x, BinaryNode* & t){
168 if(t == NULL)
169 return;
170 if(x < t->element)
171 remove_s(x, t->left);
172 else if(x, t->element)
173 remove_s(x, t->right);
174 else if(t->right != NULL && t->left != NULL){
175 t->element = findMin(t->right)->element;
176 remove_s(t->element, t->right);
177 }
178 else{
179 BinaryNode* old = t;
180 t = (t->left != NULL)? t->left: t->right;
181 delete old;
182 }
183 }
184 void remove(const T& x){
185 remove_s(x, root);
186 }
187 ///
188 void printTree_s(const BinaryNode* t){
189 if(t != NULL){
190 printTree_s(t->left);
191 printTree_s(t->right);
192 cout << t->element << endl;
193 }
194 }
195 void printTree(){
196 printTree_s(root);
197 }
198
199 //前序遍历
200 void PreOrderTraverseRec(const BinaryNode* root){
201 if(root != NULL){
202 cout << root->element << endl;
203 PreOrderTraverseRec(root->left);
204 PreOrderTraverseRec(root->right);
205 }
206 }
207 void PreOrderTraverseNonRec_1(const BinaryNode* root){
208 if(root == NULL){
209 cout << "enmpty tree...\n";
210 }
211 else{
212 stack<BinaryNode*> st;
213 BinaryNode* curr = root;
214 st.push(curr);
215 while(!st.empty()){
216 curr = st.top();
217 cout << curr->element << endl;
218 st.pop();
219 if(curr->right != NULL)
220 st.push(curr->right);
221 if(curr->left != NULL)
222 st.push(curr->left);
223 }
224 }
225 }
226
227 void PreOrderTraverseNonRec_2(const BinaryNode* root){
228 if(root == NULL)
229 cout << "empty tree...\n";
230 else{
231 stack<BinaryNode*> st;
232 BinaryNode* curr = root;
233 while(!st.empty() || curr != NULL){
234 while(curr != NULL){
235 st.push(curr);
236 cout << curr->element << endl;
237 curr = curr->left;
238 }
239 if(!st.empty()){
240 curr = st.top();
241 st.pop();
242 curr = curr->right;
243 }
244 }
245 }
246 }
247 void PreOrderTraverseMorris(const BinaryNode* root){
248 BinaryNode* curr = root;
249 BinaryNode* pre = NULL;
250 while(curr != NULL){
251 if(curr->left == NULL){
252 cout << curr->element << endl;
253 curr = curr->right;
254 }
255 else{
256 pre = curr->left;
257 while(pre->right != NULL && pre->right != curr)
258 pre = pre->right;
259 if(pre->right == NULL){
260 cout << curr->element << endl;
261 pre->right = curr;
262 curr = curr->left;
263 }
264 else{
265 pre->right = NULL;
266 curr = curr->right;
267 }
268 }
269 }
270 }
271
272
273 //中序遍历
274 void InOrderTraverselRec(const BinaryNode* root){
275 if(root != NULL){
276 InOrderTraverselRec(root->left);
277 cout << root->element << endl;
278 InOrderTraverselRec(root->right);
279 }
280 }
281
282 void InOrderTraverselNonRec(const BinaryNode* root){
283 if(root == NULL)
284 cout << "empty tree...\n";
285 else{
286 stack<BinaryNode*> st;
287 BinaryNode* curr = root;
288 while(!st.empty() || curr != NULL){
289 while(curr != NULL){
290 st.push(curr);
291 curr = curr->left;
292 }
293 if(!st.empty()){
294 curr = st.top();
295 st.pop();
296 cout << curr->element << endl;
297 curr = curr->right;
298 }
299 }
300 }
301 }
302 void InOrderTraverselMorris(const BinaryNode* root){
303 BinaryNode* curr = root;
304 BinaryNode* pre = NULL;
305 while(curr != NULL){
306 if(curr->left == NULL){
307 cout << curr->element << endl;
308 curr = curr->right;
309 }
310 else{
311 pre = curr->left;
312 while(pre->right != NULL && pre->right != curr)
313 pre = pre->right;
314 if(pre->right == NULL){
315 pre->right = curr;
316 curr = curr->left;
317 }
318 else{
319 pre->right = NULL;
320 cout << curr->element << endl;
321 curr = curr->right;
322 }
323 }
324 }
325 }
326 //后续遍历
327 void PostOrderTraverseRec(const BinaryNode* root){
328 if(root != NULL){
329 PostOrderTraverseRec(root->left);
330 PostOrderTraverseRec(root->right);
331 cout << root->element;
332 }
333 }
334
335 void PostOrderTraverseNonRec_1(const BinaryNode* root){
336 if(root == NULL)
337 cout << "empty tree...\n";
338 else{
339 stack<BinaryNode*> st;
340 BinaryNode* curr = root;
341 BinaryNode* pre = NULL;
342 while(!st.empty() || curr != NULL){
343 while(curr != NULL){
344 st.push(curr);
345 curr = curr->left;
346 }
347 if(!st.empty()){
348 curr = st.top();
349 if(curr->right == NULL || pre == curr->right){
350 cout << curr->element << endl;
351 st.pop();
352 pre = curr;
353 curr == NULL;
354 }
355 else
356 curr = curr->right;
357 }
358 }
359 }
360 }
361
362 void PostOrderTraverseNonRec_2(const BinaryNode* root){
363 if(root == NULL)
364 cout << "empty tree...\n";
365 else{
366 stack<BinaryNode*> st;
367 BinaryNode* curr = root;
368 BinaryNode* pre = NULL;
369 st.push(curr);
370 while(!st.empty() || curr != NULL){
371 curr = st.top();
372 if((curr->left == NULL && curr->right == NULL) ||
373 ((curr->right == NULL && pre == curr->left) ||
374 pre == curr->right)){
375 st.pop();
376 cout << curr->element;
377 pre = curr;
378 }
379 else{
380 if(curr->right != NULL)
381 st.push(curr->right);
382 if(curr->left != NULL)
383 st.push(curr->left);
384 }
385 }
386 }
387 }
388
389 void PostOrderTraverseNonRec_3(const BinaryNode* root){
390 stack<BinaryNode*> st;
391 BinaryNode* curr = root;
392 st.push(curr);
393 st.push(curr);
394 while(!st.empty()){
395 curr = st.top();
396 st.pop();
397 if(!st.empty() && curr == st.top()){
398 if(curr->right){
399 st.push(curr->right); st.push(curr->right);
400 }
401 if(curr->left){
402 st.push(curr->left); st.push(curr->left);
403 }
404 }
405 else
406 cout << curr->element << endl;
407 }
408 }
409 };
410
411 int main(){
412 BinarySearchTree<int> lt;
413 lt.insert(10);
414 lt.insert(100);
415 lt.insert(-10);
416 lt.printTree();
417 return 0;
418 }