-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcompsci201_Recitation7
More file actions
350 lines (285 loc) · 8.8 KB
/
compsci201_Recitation7
File metadata and controls
350 lines (285 loc) · 8.8 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
public class BST {
TreeNode root = null;
public class TreeNode {
public int myValue;
public TreeNode left; // holds smaller tree nodes
public TreeNode right; // holds larger tree nodes
public TreeNode(int val) { myValue = val; }
}
public void add(int newValue)
{
if(root == null)
root = new TreeNode(newValue);
else
add(newValue, root);
}
public void add(int newValue, TreeNode current) {
if (newValue < current.myValue) {
if (current.left == null) {
current.left = new TreeNode(newValue);
} else {
add(newValue, current.left);
}
} else {
// newValue >= myValue
if (current.right == null) {
current.right = new TreeNode(newValue);
} else {
add(newValue, current.right);
}
}
}
public String toString()
{
return toString(root, "");
}
public String toString(TreeNode current, String level) {
String leftString = "null";
String rightString = "null";
if (current.left != null)
leftString = toString(current.left, level+" ");
if (current.right != null)
rightString = toString(current.right, level+" ");
return current.myValue + "\n" + level +"L: " + leftString + "\n" + level + "R: " + rightString;
}
public int computeHeight()
{
return computeHeight(root);
}
private int computeHeight(TreeNode current) {
if(current == null)
return 0;
int lResult = computeHeight(current.left);
int rResult = computeHeight(current.right);
if(lResult > rResult) {
return lResult + 1;
} else {
return rResult + 1;
}
}
public int countNodes()
{
return countNodes(root);
}
public int countNodes(TreeNode current)
{
if(current == null) {
return 0;
}
int lCount = countNodes(current.left);
int rCount = countNodes(current.right);
// your code here
return 1 + lCount + rCount;
}
public boolean containsNode(int value) {
return containsNode(value, root);
}
private boolean containsNode(int value, TreeNode current) {
// your code goes here
// hint: base case...if the node is null, it does not contain the value
if (current == null)
return false;
if(current.myValue == value)
return true;
boolean lBool = containsNode(value, current.left);
boolean rBool = containsNode(value, current.right);
return lBool || rBool;
}
public int findMax() {
return findMax(root);
}
private int findMax(TreeNode current) {
//assuming all nodes have values greater than -1000
int max = -1000;
if(current == null)
return max;
int lMax = findMax(current.left);
int rMax = findMax(current.right);
return Math.max(current.myValue, Math.max(lMax, rMax));
}
public int numLeaves() {
return numLeaves(root);
}
private int numLeaves(TreeNode current) {
if(current.left == null && current.right == null) {
// we are a leaf
return 1;
}
int leafCount = 0;
if(current.left != null)
leafCount += numLeaves(current.left);
if(current.right != null)
leafCount += numLeaves(current.right);
return leafCount;
}
public int levelCount(int target) {
return levelCount(root, target);
}
/**
* Returns number of nodes at specified level in t, where level >= 0.
* @param level specifies the level, >= 0
* @param t is the tree whose level-count is determined
* @return number of nodes at given level in t
*/
public int levelCount(TreeNode t, int level){
if(t == null) {
return 0;
}
if(level == 0) {
return 1;
}
int nodeCount = 0;
nodeCount += levelCount(t.left, level - 1);
nodeCount += levelCount(t.right, level - 1);
return nodeCount;
}
public boolean hasPathSum(int target) {
return hasPathSum(root, target);
}
/**
* Return true if and only if t has a root-to-leaf path that sums to target.
* @param t is a binary tree
* @param target is the value whose sum is searched for on some root-to-leaf path
* @return true if and only if t has a root-to-leaf path summing to target
*/
private boolean hasPathSum(TreeNode current, int target) {
if(current == null)
return false;
if(current.left == null && current.right == null) {
if(target == current.myValue) {
return true;
}
}
boolean rightPath = hasPathSum(current.right, target - current.myValue);
boolean leftPath = hasPathSum(current.left, target - current.myValue);
return rightPath || leftPath;
}
public int findK(int k) {
return findK(root, k);
}
private int findK(TreeNode current, int k) {
if(current == null) {
return 0;
}
if(current.left == null && current.right == null) {
k--;
if(k == 0) {
return current.myValue;
}
} else {
int val = findK(current.left, k);
if(val != -1) {
return val;
}
k = k - countNodes(current.left);
k--;
if(k == 0)
return current.myValue;
return findK(current.right, k);
}
return -1;
}
public static int height(TreeNode t){
if (t == null) return 0;
return 1 + Math.max(height(t.left), height(t.right));
}
public static int diameter(TreeNode t) {
if (t == null) return 0;
int leftD = diameter(t.left);
int rightD = diameter(t.right);
int rootD = height(t.left) + height(t.right) + 1;
return Math.max(rootD, Math.max(leftD, rightD));
}
/**
* Return both height and diameter of t, return height as
* value with index 0 and diameter as value with index 1
* in the returned array.
* @param t is a binary tree
* @return array containing both height (index 0) and diameter (index 1)
*/
public static int[] diameterHelper (TreeNode t) {
int[] ret = new int[2]; // return this array
if (t == null) {
ret[0] = 0; // height is 0
ret[1] = 0; // and diameter is 0
return ret;
}
int[] left = diameterHelper(t.left);
int[] right = diameterHelper(t.right);
ret[0] = 1 + Math.max(left[0],right[0]); // this is height
// fill in value for ret[1] below
//FIX THIS
ret[1] = Math.max((right[0] + left[0] + 1), Math.max(left[1], right[1]));
return ret;
}
public static int diameterTwo(TreeNode t) {
int[] ret = diameterHelper(t);
return ret[1];
}
/**
* Returns true if s and t are isomorphic, i.e., have same shape.
* @param s is a binary tree (not necessarily a search tree)
* @param t is a binary tree
* @return true if and only if s and t are isomorphic
*/
public static boolean isIsomorphic(TreeNode s, TreeNode t) {
if(s == null && t == null) {
return true;
}
if(s == null || t == null) {
return false;
}
boolean leftIso = isIsomorphic(s.left, t.left);
boolean rightIso = isIsomorphic(s.right, t.right);
return leftIso && rightIso;
}
/**
* Returns true if s and t are quasi-isomorphic, i.e., have same quasi-shape.
* @param s is a binary tree (not necessarily a search tree)
* @param t is a binary tree
* @return true if and only if s and t are quasi-isomporphic
*/
public static boolean isQuasiIsomorphic(TreeNode s, TreeNode t) {
if(s == null && t == null) {
return true;
}
if(s == null || t == null) {
return false;
}
boolean leftQIso = isQuasiIsomorphic(s.left, t.left);
boolean rightQIso = isQuasiIsomorphic(s.right, t.right);
if(leftQIso && rightQIso)
return true;
leftQIso = isQuasiIsomorphic(s.left, t.right);
rightQIso = isQuasiIsomorphic(s.right, t.left);
return leftQIso && rightQIso;
}
public static void main(String[] args) {
BST bst = new BST();
int[] data = {6,8,2,4,1,7,5,3,9};
for(int i: data)
bst.add(i);
System.out.println(bst.toString());
System.out.printf("Number of leaves: %d\n", bst.numLeaves()); // 5
System.out.printf("Number of nodes on level 2: %d\n", bst.levelCount(2)); // 4
System.out.println("Has path sum 17: " + bst.hasPathSum(17));//true
System.out.println("Has path sum 10: " + bst.hasPathSum(10));//false
System.out.printf("The value in node 5 is: %d\n", bst.findK(5)); // 5
int[] dataOne = {10,2,1,7,6,15,14,13}; // isomorphic to 3
int[] dataTwo = {10,1,2,3,15,11,12,17}; //quasi-isomorphic to 1 & 3
int[] dataThree = {11,3,2,8,7,16,15,14}; // isomorphic to 1
BST one = new BST();
BST two = new BST();
BST three = new BST();
for(int i: dataOne)
one.add(i);
for(int i: dataTwo)
two.add(i);
for(int i: dataThree)
three.add(i);
System.out.println(diameter(one.root)); // 7
System.out.println(diameterTwo(one.root)); // also 7
System.out.println(isIsomorphic(one.root, three.root)); //true
System.out.println(isQuasiIsomorphic(one.root, two.root)); //true
}
}