-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST.cpp
304 lines (272 loc) · 6.94 KB
/
BST.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
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
struct Node
{
int key;
Node* pLeft;
Node* pRight;
};
Node* createNode(int data){
Node* p = new Node;
p->key = data;
p->pLeft = p->pRight = NULL;
return p;
}
void Insert(Node*& pRoot, int x){
if (pRoot == NULL){
pRoot = createNode(x);
return;
}
if(pRoot->key > x){
Insert(pRoot->pLeft, x);
}
if(pRoot->key < x){
Insert(pRoot->pRight, x);
}
}
//! Pre-order
void NLR(Node* pRoot){
if(pRoot == NULL){
return;
}
cout << pRoot->key << " ";
NLR(pRoot->pLeft);
NLR(pRoot->pRight);
}
//! In-order
void LNR(Node* pRoot){
if(pRoot == NULL){
return;
}
LNR(pRoot->pLeft);
cout << pRoot->key << " ";
LNR(pRoot->pRight);
}
//! Post-order
void LRN(Node* pRoot){
if(pRoot == NULL){
return;
}
LRN(pRoot->pLeft);
LRN(pRoot->pRight);
cout << pRoot->key << " ";
}
void levelOrder(Node* pRoot){
if(pRoot == NULL){
return;
}
queue <Node*> q; // ! Queue kieu Node*
q.push(pRoot);
while(!q.empty()){
Node* temp = q.front();
q.pop();
if(temp->pLeft != NULL){
q.push(temp->pLeft);
}
if(temp->pRight != NULL){
q.push(temp->pRight);
}
cout << temp->key << " ";
}
}
int height(Node* pRoot){
if(pRoot == NULL){
return 0;
}
return max(height(pRoot->pLeft), height(pRoot->pRight)) + 1;
}
int countNode(Node* pRoot){
if(pRoot == NULL){
return 0;
}
return countNode(pRoot->pLeft) + countNode(pRoot->pRight) + 1;
}
int sumNode(Node* pRoot){
if(pRoot == NULL){
return 0;
}
return sumNode(pRoot->pLeft) + sumNode(pRoot) + pRoot->key;
}
Node* Search(Node* pRoot, int x){
if(pRoot == NULL || pRoot->key == x){
return pRoot;
}
if(pRoot->key < x){
Search(pRoot->pRight, x);
}
if(pRoot->key > x){
Search(pRoot->pLeft, x);
}
}
void Remove(Node*& pRoot, int x){
if(pRoot == NULL){
return;
}
if(pRoot->key > x){
Remove(pRoot->pLeft, x);
}
if(pRoot->key < x){
Remove(pRoot->pRight, x);
}
if(pRoot->key == x){
//! Truong hop cay co mot con
if(pRoot->pLeft == NULL && pRoot->pRight == NULL){
delete pRoot;
//! Node parent tro vao NULL de khong bi gia tri rac
pRoot = NULL;
return;
}
//! Truong hop cay co mot con ben trai
if(pRoot->pLeft != NULL && pRoot->pRight == NULL){
Node* p = pRoot;
pRoot = pRoot->pLeft;
delete p;
return;
}
//! Truong hop cay co mot con ben phai
if(pRoot->pLeft == NULL && pRoot->pRight != NULL){
Node* p = pRoot;
pRoot = pRoot->pRight;
delete p;
return;
}
//! Truong hop cay co hai con
if(pRoot->pLeft != NULL && pRoot->pRight != NULL){
Node* p = pRoot->pRight;
while(p->pLeft != NULL){
p = p->pLeft; //! Node ben trai cuoi cung cua cay ben phai
//! Thay bang Node ben phai cuoi cung cua cai ben trai cung duoc
}
pRoot->key = p->key;
Remove(pRoot->pRight, pRoot->key);
}
}
}
Node* createTree(int a[], int n){
Node* pRoot = NULL;
for(int i = 0; i < n; i++){
Insert(pRoot, a[i]);
}
return pRoot;
}
void removeTree(Node*& pRoot){
while(pRoot != NULL){
Remove(pRoot, pRoot->key);
}
}
//! Chieu cao cua mot node la chieu cao cua cay duoc tao ra boi node do
int heightNode(Node* pRoot, int value){
//! Node khong ton tai thi de quy trai = de quy phai = 0
if(pRoot == NULL){
return 0;
}
if(pRoot->key == value){
return height(pRoot);
}
return heightNode(pRoot->pLeft, value) + heightNode(pRoot->pRight, value);
}
int Level(Node* pRoot, Node* p){
if (pRoot == NULL){
return 0;
}
queue <Node*> q1; //! Chua node tren cay
queue <int> q2; //! Chua level tuong ung cua node trong q1
q1.push(pRoot);
q2.push(1);
while(!q1.empty()){
Node* temp = q1.front(); //! Lay gia tri phan tu dau tien cua q1
q1.pop(); //! Xoa gia tri dau tien khoi q1
int x = q2.front();
q2.pop();
if(temp == p){
return x;
}
if(temp->pLeft != NULL){
q1.push(temp->pLeft);
q2.push(x + 1);
}
if(temp->pRight != NULL){
q1.push(temp->pRight);
q2.push(x + 1);
}
}
return 0;
}
int countLeaf(Node* pRoot){
if(pRoot == NULL){
return 0;
}
if(pRoot->pLeft == NULL && pRoot->pRight == NULL){
return 1;
}
return countLeaf(pRoot->pLeft) + countLeaf(pRoot->pRight);
}
//! Gia tri cua Node nho hon gia tri x
int countLess(Node* pRoot, int x){
if(pRoot == NULL){
return 0;
}
if(pRoot->key == x){
return countNode(pRoot);
}
if(pRoot->key > x){
return countLess(pRoot->pLeft, x);
}
if(pRoot->key < x){
return countNode(pRoot->pLeft) + 1 + countLess(pRoot->pRight, x);
}
}
//! Gia tri cua Node lon hon gia tri x
int countGreater(Node* pRoot, int x){
if(pRoot == NULL){
return 0;
}
if(pRoot->key == x){
return countNode(pRoot);
}
if(pRoot->key > x){
return countNode(pRoot->pRight) + 1 + countGreater(pRoot->pLeft, x);
}
if(pRoot->key < x){
return countGreater(pRoot->pRight, x);
}
}
int minNode(Node* pRoot){
if(pRoot == NULL){
return 10000000;
}
return min(min(pRoot->key, minNode(pRoot->pLeft)), minNode(pRoot->pRight));
}
int maxNode(Node* pRoot){
if(pRoot == NULL){
return -10000000;
}
return max(max(pRoot->key, maxNode(pRoot->pLeft)), maxNode(pRoot->pRight));
}
bool isBST(Node* pRoot){
if(pRoot == NULL){
return true;
}
//! Node goc lon hon Node lon nhat ben trai
//! Node goc nho hon Node nho nhat ben phai
//! Tat ca cac Node tren cay phai thoa 2 dieu kien nay
if(pRoot->key < minNode(pRoot->pRight) && pRoot->key > maxNode(pRoot->pLeft)){
return isBST(pRoot->pLeft) && isBST(pRoot->pRight);
}
return false;
}
bool isFullBST(Node* pRoot){
if(pRoot == NULL){
return true;
}
if(pRoot->pLeft == NULL && pRoot->pRight == NULL){
return true;
}
if(pRoot->pLeft != NULL && pRoot->pRight != NULL && isBST(pRoot)){
return isFullBST(pRoot->pLeft) && isFullBST(pRoot->pRight);
}
return false;
}