-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBSTree.cpp
365 lines (333 loc) · 7.44 KB
/
BSTree.cpp
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <iostream>
#include <functional>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <string>
using namespace std;
struct Node
{
Node(int data) : data_(data), left_(nullptr), right_(nullptr)
{
}
int data_;
Node* left_;
Node* right_;
};
class BSTree
{
public:
BSTree(Node* root = nullptr) : root_(root)
{
}
~BSTree()
{
if(root_)
{
queue<Node*> q;
q.push(root_);
while(!q.empty())
{
Node* front = q.front();
q.pop();
if(front->left_)
{
q.push(front->left_);
}
if(front->right_)
{
q.push(front->right_);
}
delete front;
}
}
}
void insert(const int& val)
{
if(!root_)
{
root_ = new Node(val);
return;
}
Node* parent = nullptr;
Node* cur = root_;
while(cur)
{
if(cur->data_ == val) //不插入值相同的元素
{
return;
}
else if(cur->data_ < val)
{
parent = cur;
cur = cur->right_;
}
else
{
parent = cur;
cur = cur->left_;
}
}
if(val < parent->data_)
{
parent->left_ = new Node(val);
}
else
{
parent->right_ = new Node(val);
}
}
void remove(const int& val)
{
if(!root_)
{
return;
}
Node* parent = nullptr;
Node* cur = root_;
while(cur)
{
if(cur->data_ == val)
{
break;
}
else if(cur->data_ < val)
{
parent = cur;
cur = cur->right_;
}
else
{
parent = cur;
cur = cur->left_;
}
}
//未找到相应的节点
if(cur == nullptr)
{
return;
}
if(cur->left_ && cur->right_)
{
parent = cur;
Node* pre = cur->left_;
while(pre->right_)
{
parent = pre;
pre = pre->right_;
}
cur->data_ = pre->data_;
cur = pre;
}
//cur指向需被删除节点,parent指向其父节点
Node* child = cur->left_;
if(!child)
{
child = cur->right_;
}
if(!parent) //特殊情况,表示删除的是根节点
{
root_ = child;
}
else
{
if(parent->left_ == cur)
{
parent->left_ = child;
}
else
{
parent->right_ = child;
}
}
delete cur;
}
bool query(const int& val)
{
Node* cur = root_;
while(cur)
{
if(cur->data_ == val)
{
return true;
}
else if(cur->data_ < val)
{
cur = cur->right_;
}
else
{
cur = cur->left_;
}
}
return false;
}
void preOrder()
{
cout << "非递归前序遍历:";
if(!root_)
{
return;
}
stack<Node*> s;
s.push(root_);
while(!s.empty())
{
Node* top = s.top();
s.pop();
cout << top->data_ << " ";
if(top->right_)
{
s.push(top->right_);
}
if(top->left_)
{
s.push(top->left_);
}
}
cout << endl;
}
void inOrder()
{
cout << "非递归中序遍历:";
if(!root_)
{
return;
}
stack<Node*> stk;
Node* cur = root_;
while(!stk.empty() || cur)
{
if(cur)
{
stk.push(cur);
cur = cur->left_;
}
else
{
Node* top = stk.top();
stk.pop();
cout << top->data_ << " ";
cur = top->right_;
}
}
cout << endl;
}
void postOrder()
{
cout << "非递归后序遍历:" << endl;
if(!root_)
{
return;
}
stack<Node*> stk1;
stack<Node*> stk2;
stk1.push(root_);
while(!stk1.empty())
{
Node* top = stk1.top();
stk1.pop();
stk2.push(top);
if(top->left_)
{
stk1.push(top->left_);
}
if(top->right_)
{
stk1.push(top->right_);
}
}
while(!stk2.empty())
{
cout << stk2.top()->data_ << " ";
stk2.pop();
}
cout << endl;
}
void levelOrder()
{
cout << "非递归层序遍历:";
if(!root_)
{
return;
}
queue<Node*> q;
q.push(root_);
while(!q.empty())
{
Node* front = q.front();
q.pop();
cout << front->data_ << " ";
if(front->left_)
{
q.push(front->left_);
}
if(front->right_)
{
q.push(front->right_);
}
}
cout << endl;
}
//求二叉树的高度
int height(Node* node)
{
if(!node) return 0;
int l = height(node->left_);
int r = height(node->right_);
return l > r ? l + 1 : r + 1;
}
//求二叉树节点的个数
int nodeTotalNum(Node* node)
{
if(!node) return 0;
int l = nodeTotalNum(node->left_);
int r = nodeTotalNum(node->right_);
return l + r + 1;
}
//寻找BST树中节点值满足区间[i, j]的节点
void findValues(Node* node, vector<int>& vec, int i, int j)
{
if(!node)
{
if(node->data_ > i)
{
findValues(node->left_, vec, i, j);
}
if(node->data_ >= i && node->data_ <= j)
{
vec.push_back(node->data_); //存储满足取值区间的节点
}
if(node->data_ < j)
{
findValues(node->right_, vec, i, j);
}
}
}
//判断二叉树是否为BST树
bool isValidBST(Node* root) {
return dfs(root, INT_MIN, INT_MAX);
}
bool dfs(Node* root, long long minv, long long maxv) {
if(!root) return true;
if(root->data_ < minv || root->data_ > maxv) return false;
return dfs(root->left_, minv, root->data_ - 1ll) && dfs(root->right_, root->data_ + 1ll, maxv);
}
Node* root_;
};
int main()
{
BSTree bst(new Node(40));
Node* node1 = new Node(20);
Node* node2 = new Node(60);
Node* node3 = new Node(30);
Node* node4 = new Node(40);
bst.root_->left_ = node1;
bst.root_->right_ = node2;
node1->left_ = node3;
node1->right_ = node4;
cout << bst.height(bst.root_) << endl;
bst.preOrder();
return 0;
}