-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbinary_search_tree.h
More file actions
294 lines (254 loc) · 7.98 KB
/
binary_search_tree.h
File metadata and controls
294 lines (254 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//
// Created by codercat on 19-4-18.
//
#ifndef CPP_ALGORITHMS_BINARY_SEARCH_TREE_H
#define CPP_ALGORITHMS_BINARY_SEARCH_TREE_H
#include "stack.h"
#include <assert.h>
#include <optional>
#include "circular_queue.h"
using namespace std;
template<typename T>
class BinarySearchTree {
private:
typedef struct Node {
T element;
Node *leftNode = NULL, *rightNode = NULL;
Node(T element) {
this->leftNode = NULL;
this->rightNode = NULL;
this->element = element;
}
} Node;
Node *rootNode = NULL;
unsigned int size = 0;
Node * insert(Node *rootNode, T element) {
if (NULL == rootNode) {
this->size ++;
return new Node(element);
} else {
if (element < rootNode->element) {
rootNode->leftNode = this->insert(rootNode->leftNode, element);
} else if (element > rootNode->element) {
rootNode->rightNode = this->insert(rootNode->rightNode, element);
}
}
return rootNode;
}
bool contains(Node *node, T element) {
if (NULL == node) {
return false;
}
if (element == node->element) {
return true;
} else if (element < node->element) {
return this->contains(node->leftNode, element);
} else {
return this->contains(node->rightNode, element);
}
}
void inOrderTraverse(Node *node) {
if (NULL == node) {
return;
}
this->inOrderTraverse(node->leftNode);
cout<<node->element<<" ";
this->inOrderTraverse(node->rightNode);
}
void postOrderTraverse(Node *node) {
if (NULL == node) {
return;
}
this->postOrderTraverse(node->leftNode);
this->postOrderTraverse(node->rightNode);
cout<<node->element<<" ";
}
Node *removeMin(Node *node) {
if (NULL == node) {
return NULL;
}
if (NULL == node->leftNode) {
Node *rightNode = node->rightNode;
this->size --;
node = NULL;
delete node;
return rightNode;
}
node->leftNode = this->removeMin(node->leftNode);
return node;
}
public:
BinarySearchTree() {
this->rootNode = NULL;
this->size = 0;
}
unsigned int getSize() {
return this->size;
}
void preOrderTraverse() {
Stack<Node *> nodeStack(this->getSize());
nodeStack.push(this->rootNode);
cout<<"preOrderTraverse ";
while( !nodeStack.isEmpty() ) {
Node *currentNode = nodeStack.top();
cout<<currentNode->element<<" ";
nodeStack.pop();
if (currentNode->rightNode != NULL) {
nodeStack.push(currentNode->rightNode);
}
if (currentNode->leftNode != NULL) {
nodeStack.push(currentNode->leftNode);
}
}
cout<<endl;
}
T min() {
assert(!this->isEmpty());
Node *currentNode = this->rootNode;
while(currentNode->leftNode != NULL) {
currentNode = currentNode->leftNode;
}
return currentNode->element;
}
Node *minNode(Node *node) {
assert(!this->isEmpty());
Node *currentNode = node;
while(currentNode->leftNode != NULL) {
currentNode = currentNode->leftNode;
}
return currentNode;
}
T removeMin() {
assert(!this->isEmpty());
T min = this->min();
this->rootNode = this->removeMin(this->rootNode);
return min;
}
optional<T> find(T element) {
Node *curNode = this->rootNode;
while( curNode != NULL ) {
if ( element == curNode->element ) {
return optional<T>(curNode->element);
} else if ( element < (curNode->element) ) {
curNode = curNode->leftNode;
} else {
curNode = curNode->rightNode;
}
}
return optional<T>();
}
Node *remove(Node *node, T element) {
if( node == NULL ) {
return NULL;
}
if ( element < node->element ) {
node->leftNode = this->remove(node->leftNode, element);
return node;
} else if (element > node->element) {
node->rightNode = this->remove(node->rightNode, element);
return node;
} else {
if ( NULL == node->leftNode ) {
Node *rightNode = node->rightNode;
this->size --;
node = NULL;
delete node;
return rightNode;
} else if ( NULL == node->rightNode ) {
Node *leftNode = node->leftNode;
this->size --;
node = NULL;
delete node;
return leftNode;
} else {
T rightMinElement = this->minNode(rootNode->rightNode)->element;
Node *newRootNode = new Node(rightMinElement);
newRootNode->leftNode = rootNode->leftNode;
newRootNode->rightNode = rootNode->rightNode;
this->removeMin(rootNode->rightNode);
rootNode = NULL;
delete(rootNode);
return newRootNode;
}
}
}
void remove(T element) {
assert(!this->isEmpty());
this->rootNode = this->remove(this->rootNode, element);
return;
}
/*
* 使用stack来实现删除最大节点,因为stack先进后出的特性可以记录最大节点的父节点
*/
T removeMax() {
assert(!this->isEmpty());
T max = this->max();
Stack<Node *> nodeStack(this->getSize());
nodeStack.push(this->rootNode);
while( nodeStack.top()->rightNode != NULL ) {
nodeStack.push(nodeStack.top()->rightNode);
}
Node *maxNode = nodeStack.top();
nodeStack.pop();
if ( maxNode == this->rootNode ) {
this->rootNode = maxNode->leftNode;
} else {
Node *parentNode = nodeStack.top();
parentNode->rightNode = maxNode->leftNode;
}
this->size --;
maxNode = NULL;
delete maxNode;
return max;
}
T max() {
assert(!this->isEmpty());
Node *currentNode = this->rootNode;
while(currentNode->rightNode != NULL) {
currentNode = currentNode->rightNode;
}
return currentNode->element;
}
void inOrderTraverse() {
assert(!this->isEmpty());
cout<<"inOrderTraverse ";
this->inOrderTraverse(this->rootNode);
cout<<endl;
}
void postOrderTraverse() {
assert(!this->isEmpty());
cout<<"postOrderTraverse ";
this->postOrderTraverse(this->rootNode);
cout<<endl;
}
/*
* 使用queue实现层序遍历(广度优先遍历),queue具有先进先出的特性,把每一层的节点放入queue,然后逐一处理.
*/
void levelOrderTraverse() {
assert(!this->isEmpty());
cout<<"levelOrderTraverse";
CircularQueue<Node *> nodeCircularQueue(this->getSize());
nodeCircularQueue.enqueue(this->rootNode);
while( !nodeCircularQueue.isEmpty() ) {
Node *currentNode = nodeCircularQueue.dequeue();
cout<<currentNode->element<<" ";
if (currentNode->leftNode != NULL) {
nodeCircularQueue.enqueue(currentNode->leftNode);
}
if (currentNode->rightNode != NULL) {
nodeCircularQueue.enqueue(currentNode->rightNode);
}
}
cout<<endl;
}
void insert(T element) {
this->rootNode = this->insert(this->rootNode, element);
}
bool isEmpty() {
return 0 == this->getSize();
}
bool contains(T element) {
return this->contains(this->rootNode, element);
}
};
#endif //CPP_ALGORITHMS_BINARY_SEARCH_TREE_H