-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySerchTree.cpp
More file actions
317 lines (290 loc) · 7.92 KB
/
binarySerchTree.cpp
File metadata and controls
317 lines (290 loc) · 7.92 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <iostream>
#include <queue>
#include <stack>
#include "include/binaruSerchTree.h"
using namespace std;
void BinarySerchTree::add(int val){
Node *newNode = new Node(val);
if(root == nullptr){
m_size++;
root = newNode;
return;
}
Node *tmp = root;
Node *parent = tmp->parent;
while(tmp != nullptr){
if(tmp->Val > val) {
parent = tmp;
tmp = tmp->left;
}else if(tmp->Val < val){
parent = tmp;
tmp = tmp->right;
}else{ //相等
parent = tmp;
}
}
if(parent->Val > val){
parent->left = newNode;
newNode->parent = parent;
}else if(parent->Val < val){
parent->right = newNode;
newNode->parent = parent;
}else{
return;
}
m_size++;
return;
}
void BinarySerchTree::remove(int val){
Node* tmp = node(root, val);
if(tmp == nullptr){
cout << "remove value is not exist" << endl;
return;
}
m_size--;
if(tmp->left != nullptr && tmp->right != nullptr){ //度为2的节点
//找到后继节点,由于后继节点只能度为1或者2,所以复制后继节点的值,然后把要删除的节点改为后继节点
Node* su = successor(root);
tmp->Val = su->Val;
tmp = su;
}
if(tmp->left == nullptr && tmp->right == nullptr){ //度为0的节点
if(tmp->parent == nullptr){ //度为0且没有父节点,则是只有根节点的树
root = nullptr;
}else{
if(tmp == tmp->parent->right){
tmp->parent->right = nullptr;
}else{
tmp->parent->left = nullptr;
}
}
}else if(tmp->left == nullptr){ //度为1的节点
tmp->parent->right = tmp->right;
tmp->right->parent = tmp->parent;
}else if(tmp->right == nullptr){
tmp->parent->left = tmp->left;
tmp->left->parent = tmp->parent;
}
}
bool BinarySerchTree::contains(int val){
printf("bool BinarySerchTree::contains(int val) %p\n",root);
printf("bool BinarySerchTree::contains(int val) %p\n",*root);
return node(root, val) != nullptr;
}
void BinarySerchTree::clear(){
m_size = 0;
root = nullptr;
}
void BinarySerchTree::preorder(){
preorder(root);
cout << endl;
}
void BinarySerchTree::preorder(Node *root){
if(root == nullptr){
return;
}
cout << root->Val << " ";
preorder(root->left);
preorder(root->right);
}
void BinarySerchTree::preorder(void (*func)(int)){
if (func != nullptr){
preorder(root, func);
}else{
preorder(root);
}
cout << endl;
}
void BinarySerchTree::preorder(Node *root, void (*func)(int)){
if(root == nullptr){
return;
}
/*
stack<Node*> stack;
stack.push(root);
while(!stack.empty()){
if(stack.top()==nullptr){
stack.pop();
continue;
}
Node *tmp = stack.top();
func(tmp->Val);
stack.pop();
stack.push(tmp->right);
stack.push(tmp->left);
}
*/
func(root->Val);
preorder(root->left, func);
preorder(root->right, func);
}
void BinarySerchTree::inorder(void (*func)(int)){
inorder(root, func);
cout << endl;
}
void BinarySerchTree::inorder(Node *root, void (*func)(int)){
stack<pair<Node*,bool>> stack;
stack.push(make_pair(root,false));
while(!stack.empty()){
if (stack.top().first == nullptr)
{
stack.pop();
continue;
}
Node *tmp = stack.top().first;
bool visit = stack.top().second;
stack.pop();
if(visit){
func(tmp->Val);
}
else{
stack.push(make_pair(tmp->right,false));
stack.push(make_pair(tmp,true));
stack.push(make_pair(tmp->left,false));
}
}
}
void BinarySerchTree::leveltraversal(void (*func)(int)){
leveltraversal(root, func);
}
void BinarySerchTree::leveltraversal(Node *root,void (*func)(int)){
queue<Node*> Tree;
Tree.push(root);
while(!Tree.empty()){
if (Tree.front()==nullptr)
{
Tree.pop();
continue;
}
func(Tree.front()->Val);
Tree.push(Tree.front()->left);
Tree.push(Tree.front()->right);
Tree.pop();
}
}
int BinarySerchTree::height(){
return height1(root);
}
int BinarySerchTree::height1(Node *root){
queue<Node *> queue;
queue.push(root);
int levelsize = 1;
int height = 0;
while(!queue.empty()){
Node *tmp = queue.front();
queue.pop();
if(tmp->left != nullptr){
queue.push(tmp->left);
}
if(tmp->right != nullptr){
queue.push(tmp->right);
}
levelsize--;
if(levelsize == 0){
levelsize = queue.size();
height++;
}
}
return height;
}
int BinarySerchTree::height(Node *root){
if(root == nullptr){
return 0;
}
int max = height(root->left) > height(root->right) ? height(root->left) : height(root->right);
return 1 + max;
}
bool BinarySerchTree::isComplete(){
if(root == nullptr){
return false;
}
bool isLeaf = false;
queue<Node*> queue;
queue.push(root);
while(!queue.empty()){
Node *tmp = queue.front();
queue.pop();
if(isLeaf && (tmp->left != nullptr || tmp->right != nullptr)){
return false;
}
if(tmp->left != nullptr && tmp->right != nullptr){
queue.push(tmp->left);
queue.push(tmp->right);
}else if(tmp->left == nullptr && tmp->right != nullptr){
return false;
}else{
isLeaf = true;
if(tmp->left != nullptr){
queue.push(tmp->left);
}
}
}
return true;
}
/*
前驱后继节点概念:中序遍历一个二叉树,某一个节点的前一个或后一个节点
思路:
1. 如果有左子树,则找左子节点下面最右的节点
2. 如果没有左子树,则一直向上找父节点,直到某个节点是父节点的右子节点为止
3. 没有左子树也没有父节点,则前驱节点为空
*/
Node *BinarySerchTree::predecessor(Node *node){
if(node == nullptr){
return node;
}
Node *tmp = node->left;
if(tmp != nullptr){
while(tmp->right != nullptr){
tmp = tmp->right;
}
return tmp;
}
if(node->left == nullptr && node->parent != nullptr){
while(node->parent != nullptr && node != node->parent->right){
node = node->parent;
}
}
return node->parent;
}
Node *BinarySerchTree::successor(Node *node){
if(node == nullptr){
return node;
}
Node *tmp = node->right;
if(tmp != nullptr){
while(tmp->left != nullptr){
tmp = tmp->left;
}
return tmp;
}
if(node->right == nullptr && node->parent != nullptr){
while(node->parent != nullptr && node != node->parent->left){
node = node->parent;
}
}
return node->parent;
}
Node* BinarySerchTree::node(Node *root, int key){
if(root == nullptr){
return nullptr;
}
while(root != nullptr){
if(key > root->Val){
root = root->right;
}else if(key < root->Val){
root = root->left;
}else{
return root;
}
}
return nullptr;
}
void BinarySerchTree::test(){
Node *tmp = root;
while(tmp != nullptr){
tmp = successor(tmp);
if(tmp != nullptr){
cout << "successor is :" << tmp->Val << endl;
}
}
}