-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeStruct.java
More file actions
553 lines (438 loc) · 11.2 KB
/
TreeStruct.java
File metadata and controls
553 lines (438 loc) · 11.2 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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
import java.util.*;
public class TreeStruct{
public class WrapTreeNode{
//当前节点
public TreeStruct currentNode;
//当前节点的位置,从左到右的空格数量
public int x;
//当前层前一个节点
public WrapTreeNode currLevelPrevNode;
//父节点
public WrapTreeNode parentNode;
//当前节点的类型:左/右孩子
//left : 0
//right : 1
//root : -1
public int childType;
public WrapTreeNode(
TreeStruct _node,
int type,
int _x,
WrapTreeNode parent,
WrapTreeNode prevNode){
currentNode = _node;
childType = type;
x = _x;
currLevelPrevNode = prevNode;
parentNode = parent;
}
}
public class TreeLine{
public TreeLine currLevelPrevLine;
//当前节点的位置,从左到右的空格数量
public int x;
//当前节点的类型:左/右孩子
//left : 0
//right : 1
//root : -1
public int childType;
public TreeLine(int _x,int type){
x =_x;
childType = type;
}
}
private String data;
private TreeStruct left;
private TreeStruct right;
public TreeStruct(String v){
data=v;
left=null;
right=null;
}
//先序,使用栈的方式
public void perOrderByStack(TreeStruct root,ArrayList<String> list){
Stack<TreeStruct> stack = new Stack<TreeStruct>();
stack.push(root);
TreeStruct curr = null;
while(!stack.isEmpty()){
curr = stack.pop();
//System.out.print(curr.data+",");
list.add(curr.data);
if(curr.right!=null){
stack.push(curr.right);
}
if(curr.left!=null){
stack.push(curr.left);
}
}
}
//先序,递归方式
public void preOrder(TreeStruct root,ArrayList<String> list){
if(root == null)
return;
//System.out.print(root.data+",");
list.add(root.data);
preOrder(root.left,list);
preOrder(root.right,list);
}
//后序,递归方式
public void lastOrder(TreeStruct root,ArrayList<String> list){
if(root == null) return;
lastOrder(root.left,list);
lastOrder(root.right,list);
list.add(root.data);
}
//后序,使用栈的方式
public void lastOrderByStack(TreeStruct root,ArrayList<String> list){
Stack<TreeStruct> stack = new Stack<TreeStruct>();
Stack<TreeStruct> help = new Stack<TreeStruct>();
stack.push(root);
TreeStruct curr = null;
while(!stack.isEmpty()){
curr=stack.pop();
help.push(curr);
if(curr.left!=null){
stack.push(curr.left);
}
if(curr.right!=null){
stack.push(curr.right);
}
}
while(!help.isEmpty()){
list.add(help.pop().data);
}
}
//中序,递归方式
public void inOrder(TreeStruct root,ArrayList<String> list){
if(root == null) return ;
inOrder(root.left,list);
list.add(root.data);
inOrder(root.right,list);
}
//中序,使用栈的方式
public void inOrderByStack(TreeStruct root,ArrayList<String> list){
Stack<TreeStruct> stack = new Stack<TreeStruct>();
TreeStruct curr = root;
while(!stack.isEmpty() || curr != null){
if(curr!=null){
stack.push(curr);
curr = curr.left;
}
else{
curr = stack.pop();
list.add(curr.data);
curr = curr.right;
}
}
}
//层次遍历树
public void levelOrder(TreeStruct root,ArrayList<String> list){
Queue<TreeStruct> queue = new LinkedList<TreeStruct>();
queue.offer(root);
while(!queue.isEmpty()){
TreeStruct curr = queue.poll();
//list.add(curr.data);
System.out.print(curr.data+",");
if(curr.left != null){
queue.offer(curr.left);
}
if(curr.right!=null){
queue.offer(curr.right);
}
}
}
//获取树最大宽度
public int getMaxWidth(TreeStruct root){
TreeStruct currEnd=root;
TreeStruct nextEnd=null;
int currCount=0;
int maxCount=0;
Queue<TreeStruct> queue = new LinkedList<TreeStruct>();
queue.offer(root);
while(!queue.isEmpty()){
TreeStruct curr = queue.poll();
currCount++;
if(curr.left !=null){
queue.offer(curr.left);
nextEnd = curr.left;
}
if(curr.right !=null){
queue.offer(curr.right);
nextEnd = curr.right;
}
if(curr == currEnd){
currEnd = nextEnd;
maxCount = Math.max(maxCount,currCount);
currCount = 0;
nextEnd = null;
}
}
return maxCount;
}
//获取树最大高度
public int getMaxLevel(TreeStruct root){
TreeStruct currEnd=root;
TreeStruct nextEnd=null;
int maxLevel=0;
Queue<TreeStruct> queue = new LinkedList<TreeStruct>();
queue.offer(root);
while(!queue.isEmpty()){
TreeStruct curr = queue.poll();
if(curr.left !=null){
queue.offer(curr.left);
nextEnd = curr.left;
}
if(curr.right !=null){
queue.offer(curr.right);
nextEnd = curr.right;
}
if(curr == currEnd){
currEnd = nextEnd;
maxLevel++;
nextEnd = null;
}
}
return maxLevel;
}
//将一棵树序列化
public Queue<String> getSerialization(TreeStruct root){
if(null == root) return null;
Queue<String> queue = new LinkedList<String>();
serialzation(root,queue);
return queue;
}
private void serialzation(TreeStruct root,Queue<String> queue){
if(root == null)
queue.offer("#");
else{
queue.offer(root.data);
serialzation(root.left,queue);
serialzation(root.right,queue);
}
}
//反序列化成一棵树
public TreeStruct unSerialztaion(Queue<String> queue){
if(null == queue || queue.size() == 0) return null;
return unSerial(queue);
}
private TreeStruct unSerial(Queue<String> queue){
String v = queue.poll();
if(v=="#") return null;
TreeStruct root = new TreeStruct(v);
root.left = unSerial(queue);
root.right = unSerial(queue);
return root;
}
public Boolean isCompleteBinTree(TreeStruct root){
if(root == null ||
root.left == null && root.right ==null ||
root.left !=null && root.right ==null) return true;
boolean isleaf=false;
Queue<TreeStruct> queue = new LinkedList<TreeStruct>();
queue.offer(root);
while(!queue.isEmpty()){
TreeStruct curr = queue.poll();
if(curr.right!=null && curr.left == null)
return false;
if(curr.left == null && curr.right==null){
isleaf=true;
}
if(isleaf && (curr.left !=null || curr.right!=null))
return false;
if(curr.left !=null)
queue.offer(curr.left);
if(curr.right!=null)
queue.offer(curr.right);
}
return true;
}
private String getChildStr(TreeStruct root){
String str=root.data;
if(root.left != null) str = "^"+str;
if(root.right != null) str = str+"^";
return str;
}
private void printNode(WrapTreeNode node,int currLevel){
int perWidth = 20/currLevel;
if(node.parentNode == null){
printStr(getStr(getChildStr(node.currentNode),node.x));
return;
}
if(node.currLevelPrevNode == null){
//left
if(node.childType == 0){
int space = node.parentNode.x - perWidth;
printStr(getStr(getChildStr(node.currentNode),space));
}
//right
else{
int space = node.parentNode.x + perWidth;
printStr(getStr(getChildStr(node.currentNode),space));
}
}else{
int space = node.parentNode.x - node.currLevelPrevNode.x+perWidth;
if(space+node.currLevelPrevNode.x >node.parentNode.x && currLevel > 2){
space = node.parentNode.x - node.currLevelPrevNode.x;
}
printStr(getStr(getChildStr(node.currentNode),space));
}
}
//打印树
public void printBinTree(TreeStruct root){
int maxWidth = 180;
int perLeftWidth = 10;
int perRightWidth = 20;
int usedWidth = 0;
TreeStruct currEnd=root;
TreeStruct nextEnd=null;
int currLevel =1;
WrapTreeNode currLevelPrev=null;
Queue<WrapTreeNode> nodeQueue = new LinkedList<WrapTreeNode>();
Queue<TreeLine> lineQueue = new LinkedList<TreeLine>();
nodeQueue.offer(
new WrapTreeNode(
root,-1,(maxWidth-root.data.length())/2,null,null));
while(!nodeQueue.isEmpty()){
WrapTreeNode curr = nodeQueue.poll();
printNode(curr,currLevel);
//printStr(getStr(curr.currentNode.data,curr.x));
int midd = curr.x;
if(curr.currentNode.left !=null){
WrapTreeNode l = new
WrapTreeNode(curr.currentNode.left,
0,
midd-perLeftWidth,
curr,
currLevelPrev);
nodeQueue.offer(l);
//lineQueue.offer(new TreeLine(midd-perWidth,0));
nextEnd = curr.currentNode.left;
currLevelPrev = l;
}
if(curr.currentNode.right !=null){
WrapTreeNode r = new
WrapTreeNode(curr.currentNode.right,
1,
midd+perRightWidth,
curr,
currLevelPrev);
nodeQueue.offer(r);
//lineQueue.offer(new TreeLine(midd+perWidth,1));
nextEnd = curr.currentNode.right;
currLevelPrev = r;
}
if(curr.currentNode == currEnd){
printLine(lineQueue);
currEnd = nextEnd;
nextEnd = null;
currLevelPrev = null;
currLevel++;
}
}
}
private void printLine(Queue<TreeLine> lineQueue){
System.out.println();
System.out.println();
System.out.println();
System.out.println();
// while(!lineQueue.isEmpty()){
// TreeLine line = lineQueue.poll();
// if(line.childType == 1){
// printStr(getStr("/",line.x));
// }else{
// printStr(getStr("\\",line.x));
// }
// }
// System.out.println();
}
private String getStr(String str,int spaceCount){
for (int i=0; i<spaceCount; i++) {
str= " "+str;
}
return str;
}
private void printStr(String v){
System.out.print(v);
}
public static TreeStruct getNode(){
String v = Character.toString((char)('A'+((int)(Math.random()*26))));
return new TreeStruct(v);
}
public static void randomBinTree(TreeStruct root,int maxLevel){
TreeStruct p = root;
int left = maxLevel;
int random = (int)(Math.random()*9999);
int ont = (int)(Math.random()*8)+1;
while(left > 0){
if(left == maxLevel){
p.right = getNode();
p.left=getNode();
p = p.left;
}
else{
if(random/left % ont >0){
p.left = getNode();
p=p.left;
}
else{
p.right = getNode();
p.left=getNode();
p = p.right;
}
}
left--;
}
}
public static Boolean compar(ArrayList<String> list,ArrayList<String> list2){
if(list.size()!= list2.size()) return false;
int size = list.size()-1;
while(size >0){
if(list.get(size) != list2.get(size--)) return false;
}
return true;
}
public static TreeStruct getRandomBinTree(){
TreeStruct root = new TreeStruct("AAAA");
root.left = new TreeStruct("A1");
root.right = new TreeStruct("A2");
randomBinTree(root.left,2);
randomBinTree(root.right,1);
return root;
}
public static Boolean isTrue(){
int max = 1000000;
for (int i=0; i<max;i++ ) {
ArrayList<String> list = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
TreeStruct root = getRandomBinTree();
root.inOrder(root,list);
root.inOrderByStack(root,list2);
if(!compar(list,list2)){
return false;
}
}
return true;
}
public static void printSerialztion(TreeStruct root){
//TreeStruct root = getRandomBinTree();
Queue<String> queue = root.getSerialization(root);
while(!queue.isEmpty())
{
System.out.print(queue.poll()+",");
}
}
public static void getUnSerialzation(Queue<String> queue){
TreeStruct root = (new TreeStruct("")).unSerialztaion(queue);
root.printBinTree(root);
}
public static TreeStruct printTreeTest(){
TreeStruct root = getRandomBinTree();
root.printBinTree(root);
return root;
}
public static void main(String[] args) {
TreeStruct root = printTreeTest();
printSerialztion(root);
System.out.print("result = "+root.isCompleteBinTree(root));
}
}