-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsCBT.java
More file actions
199 lines (143 loc) · 3.25 KB
/
IsCBT.java
File metadata and controls
199 lines (143 loc) · 3.25 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
import java.util.*;
public class IsCBT{
public static class Node{
public int value;
public Node left;
public Node right;
public Node(int v){
value = v;
left=null;
right=null;
}
}
public static Node getRandomTree(int maxLevel){
return randomTree(1,maxLevel);
}
public static Node randomTree(int currLevel,int maxLevel){
if(currLevel>maxLevel || Math.random() > 0.9)
return null;
Node head = new Node((int)Math.random()*1000);
head.left = randomTree(currLevel+1,maxLevel);
head.right = randomTree(currLevel+1,maxLevel);
return head;
}
public static boolean isCBT_Old(Node head){
if(head == null)
return true;
Queue<Node> queue = new LinkedList<Node>();
queue.add(head);
Node l=null;
Node r=null;
Node curr=null;
boolean leaf=false;
while(!queue.isEmpty()){
curr = queue.poll();
l = curr.left;
r = curr.right;
if(
(leaf && (l !=null || r != null))
||
(l == null && r != null)
){
return false;
}
if(l != null){
queue.add(l);
}
if(r != null){
queue.add(r);
}
if(l == null || r == null){
leaf=true;
}
}
return true;
}
public static class Info{
public boolean isFull;
public boolean isCBT;
public int hight;
public Info(boolean full,boolean cbt,int h){
isFull = full;
isCBT = cbt;
hight = h;
}
}
public static boolean isCBT_New(Node head){
if(head == null){
return true;
}
return process(head).isCBT;
}
public static Info process(Node head){
if(head == null){
return new Info(true,true,0);
}
Info l = process(head.left);
Info r = process(head.right);
boolean isFull=false;
boolean isCBT=false;
int hight = Math.max(l.hight,r.hight)+1;
if(l.isFull && r.isFull && l.hight == r.hight){
isFull = true;
}
if(isFull){
isCBT = true;
}
else{
if(l.isCBT && r.isCBT){
if(l.isFull && r.isFull && l.hight == r.hight +1 )
isCBT = true;
if(l.isCBT && r.isFull && l.hight == r.hight +1 )
isCBT = true;
if(l.isFull && r.isCBT && l.hight == r.hight)
isCBT = true;
}
}
return new Info(isFull,isCBT,hight);
}
public static void printTree(Node head) {
System.out.println("Binary Tree:");
printInOrder(head, 0, "H", 17);
System.out.println();
}
public static void printInOrder(Node head, int height, String to, int len) {
if (head == null) {
return;
}
printInOrder(head.right, height + 1, "v", len);
String val = to + head.value + to;
int lenM = val.length();
int lenL = (len - lenM) / 2;
int lenR = len - lenM - lenL;
val = getSpace(lenL) + val + getSpace(lenR);
System.out.println(getSpace(height * len) + val);
printInOrder(head.left, height + 1, "^", len);
}
public static String getSpace(int num) {
String space = " ";
StringBuffer buf = new StringBuffer("");
for (int i = 0; i < num; i++) {
buf.append(space);
}
return buf.toString();
}
public static boolean isTrue(){
int max=10;
for (int i=0; i<max; i++) {
Node root = getRandomTree(4);
boolean _old = isCBT_Old(root);
boolean _new = isCBT_New(root);
if(_old != _new){
return false;
}
if(_old&&_new){
printTree(root);
}
}
return true;
}
public static void main(String[] args){
System.out.println("Result = "+isTrue());
}
}