-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeExpandAltSE.java
46 lines (40 loc) · 974 Bytes
/
TreeExpandAltSE.java
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
public class TreeExpandAltSE {
public TreeExpandAltSE left;
public TreeExpandAltSE right;
/**
* Build a list of length n - 3
* At the end build a tree of height 3
*
* @author Joel Beckmann
*/
public static void main(String[] args) {
int n = args.length;
TreeExpandAltSE tree = new TreeExpandAltSE();
build(tree, n);
}
public TreeExpandAltSE() {
this.left = null;
this.right = null;
}
public static void build(TreeExpandAltSE t, int length) {
TreeExpandAltSE l = new TreeExpandAltSE();
t.left = l;
if (length > 3) {
length--;
build(l, length);
} else {
expand(l, length);
}
}
public static void expand(TreeExpandAltSE t, int length) {
if (length > 0) {
TreeExpandAltSE l = new TreeExpandAltSE();
TreeExpandAltSE r = new TreeExpandAltSE();
t.left = l;
t.right = r;
length--;
expand(l, length);
expand(r, length);
}
}
}