-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitree.cpp
332 lines (329 loc) · 7.62 KB
/
Bitree.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
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
using namespace std;
const int maxn = 1000;
class Node{
public:
int key;
Node *left;
Node *right;
Node *parent;
Node(int value,Node *p,Node *l, Node *r):
key(value),parent(p),left(l),right(r){}
// 判断是否为空
bool empty() {
if(left == nullptr && right == nullptr) return true;
return false;
};
};
class Tree{
public:
Node *mroot;
Tree(){
mroot = nullptr;
};
~Tree(){
destroy(mroot);
};
// 插入函数
void insert(int key){
Node *z = nullptr;
if((z = new Node(key, nullptr, nullptr, nullptr)) == nullptr)
return;
insert(mroot, z);
};
// 前序遍历
void preOrder(){
preOrder(mroot);
cout<<endl;
};
// 非递归前序遍历
void preOrderNoRecursion(){
if(mroot == nullptr) {
cout<<endl;
return;
}
Node * s[maxn];
int top = 1;
s[0] = mroot;
Node * tmp;
while(top > 0){
top--;
tmp = s[top];
cout<<tmp->key<<" ";
if(tmp->right != nullptr){
s[top] = tmp->right;
top++;
}
if(tmp->left != nullptr){
s[top] = tmp->left;
top++;
}
}
cout<<endl;
};
// 中序遍历
void inOrder(){
inOrder(mroot);
cout<<endl;
};
// 非递归中序遍历
void inOrderNoRecursion(){
if(mroot == nullptr) {
cout<<endl;
return;
}
int top = 0;
Node * s[maxn];
Node * tmp = mroot;
while (top != 0 || tmp != nullptr) {
while (tmp != nullptr) {
s[top] = tmp;
top++;
tmp = tmp->left;
}
if(top!=0) {
tmp = s[top-1];
cout << tmp->key << " ";
top--;
tmp = tmp->right;
}
}
cout<<endl;
};
// 后续遍历
void postOrder(){
postOrder(mroot);
cout<<endl;
};
// 非递归后续遍历
void postOrderNoRecursion(){
if(mroot == nullptr) {
cout<<endl;
return;
}
Node *s[maxn], *tmp;
int mark[maxn], top = -1;
s[++top] = mroot;
mark[top] = 0;
while(top >= 0){
tmp = s[top];
if(mark[top] == 0 && !tmp->empty()){
mark[top] = 1;
if(tmp->right != nullptr){
s[++top] = tmp->right;
mark[top] = 0;
}
if(tmp->left != nullptr){
s[++top] = tmp->left;
mark[top] = 0;
}
}
if(s[top]->empty() || mark[top] == 1)
cout<<s[top--]->key<<" ";
}
cout<<endl;
};
// 层次遍历
void hierachicalorder(){
if(mroot == nullptr) return;
Node* s[maxn];
s[0] = mroot;
int head = 0, tail = 1;
while(head < tail){
int p = tail;
for(int i = head; i < p; i++){
cout<<s[i]->key<<" ";
head++;
if(!s[i]->empty()){
if(s[i]->left != nullptr){
s[tail] = s[i]->left;
tail++;
}
if(s[i]->right != nullptr){
s[tail] = s[i]->right;
tail++;
}
}
}
}
cout<<endl;
};
// 结点个数
int count(){
return count(mroot);
};
// 统计高度
int height(){
return height(mroot);
};
// 判断是否是满树
bool full(){
int h=height();
int t=1;
for(int i=0;i<h;i++) t*=2;
if(count()==t-1) return true;
else return false;
};
// 判断是否完全二叉树
bool complete(){
if(mroot == nullptr) true;
Node* s[maxn];
s[0] = mroot;
int head = 0, tail = 1, h = height(), th = 1;
bool tag = false;
while(head < tail){
int p = tail;
for(int i = head; i < p; i++){
head++;
if(!s[i]->empty()){
if(s[i]->left == nullptr || s[i]->right == nullptr){
if(tag) return false;
tag = true;
}
if(s[i]->left != nullptr){
s[tail] = s[i]->left;
tail++;
}
if(s[i]->right != nullptr){
if(tag) return false;
s[tail] = s[i]->right;
tail++;
tag = true;
}
}
}
}
return true;
};
// 判断一棵二叉树是否左子树上的结点的值都小于根,右子树上的结点的值都大于根
bool judgement(){
return judgement(mroot);
};
// 销毁树
void destroy(){
destroy(mroot);
};
private:
// 插入函数
void insert(Node* &root, Node* node){
Node *x = root;
Node *y = nullptr;
while (x != nullptr){
y = x;
if(node->key < x->key)
x = x->left;
else
x = x->right;
}
node->parent = y;
if(y != nullptr){
if(node->key < y->key)
y->left = node;
else
y->right = node;
}
else
root = node;
};
// 前序遍历
void preOrder(Node* tree) const{
if (tree != nullptr){
cout<< tree->key <<" ";
preOrder(tree->left);
preOrder(tree->right);
}
};
// 中序遍历
void inOrder(Node* tree) const{
if(tree != nullptr){
inOrder(tree->left);
cout<< tree->key <<" ";
inOrder(tree->right);
}
};
// 后序遍历
void postOrder(Node* node) const{
if (node != nullptr){
postOrder(node->left);
postOrder(node->right);
cout<<node->key<<" ";
}
};
// 结点个数
int count(Node * node){
if(node==nullptr) return 0;
return count(node->left)+count(node->right)+1;
};
// 统计高度
int height(Node* node){
if (node == nullptr) return 0;
int l=height(node->left),r=height(node->right);
if(l > r)
return l+1;
else
return r+1;
};
// 判断一棵二叉树是否左子树上的结点的值都小于根,右子树上的结点的值都大于根
bool judgement(Node* node){
if(node->empty()) return true;
else if(node->left!=nullptr && node->right!=nullptr){
if(node->left->key <= node->key && node->right->key >= node->key)
return(judgement(node->left) && judgement(node->right));
else return false;
}
else if(node->left == nullptr){
if(node->right->key >= node->key) return judgement(node->right);
else return false;
}
else{
if(node->left->key <= node->key) return judgement(node->left);
else return false;
}
};
// 销毁树
void destroy(Node* &tree){
if(tree == nullptr)
return;
if(tree->left != nullptr)
return destroy(tree->left);
if(tree->right != nullptr)
return destroy(tree->right);
delete tree;
tree = nullptr;
};
};
int main(){
Tree tree;
vector<int> l;
for (int i = 0; i < 10; i++)
l.push_back(i);
srand((unsigned)time(0));
random_shuffle(l.begin(),l.end());
for (int i = 0; i < 10; i++)
tree.insert(l[i]);
cout<<"postOrder: ";
tree.postOrder();
cout<<"postOrderNoRecursion: ";
tree.postOrderNoRecursion();
cout<<"inOrder: ";
tree.inOrder();
cout<<"inOrderNoRecursion: ";
tree.inOrderNoRecursion();
cout<<"preOrder: ";
tree.preOrder();
cout<<"preOrderNoRecursion: ";
tree.preOrderNoRecursion();
cout<<"Hierachicalorder: ";
tree.hierachicalorder();
cout<<"Height: "<<tree.height()<<endl;
cout<<"number of nodes: "<<tree.count()<<endl;
if(tree.full())cout<<"full tree!\n";
else cout<<"not full tree!\n";
if(tree.complete())cout<<"complete tree!\n";
else cout<<"not compelete tree!\n";
if(tree.judgement()) cout<<"二叉树左子树上的结点的值都小于根,右子树上的结点的值都大于根\n";
else cout<<"不满足二叉树左子树上的结点的值都小于根,右子树上的结点的值都大于根\n";
}