-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwinTreeAltSE.java
56 lines (50 loc) · 1.05 KB
/
TwinTreeAltSE.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
46
47
48
49
50
51
52
53
54
55
56
public class TwinTreeAltSE {
public TwinTreeAltSE left;
public TwinTreeAltSE right;
/*
* creates a tree like this
*
* /\
* /\ \
* /\ \ \
*
*
* @author Joel Beckmann
*/
public static void main(String[] args) {
int n = args.length;
TwinTreeAltSE tree = new TwinTreeAltSE();
build(tree, n);
inOrder(tree);
}
public TwinTreeAltSE() {
this.left = null;
this.right = null;
}
public static void build(TwinTreeAltSE t, int height) {
if (height > 0) {
height--;
TwinTreeAltSE l = new TwinTreeAltSE();
TwinTreeAltSE r = new TwinTreeAltSE();
t.left = l;
t.right = r;
build(l, height);
buildRight(r, height);
}
}
public static void buildRight(TwinTreeAltSE t, int height) {
if (height > 0) {
height--;
TwinTreeAltSE r = new TwinTreeAltSE();
t.right = r;
buildRight(r, height);
}
}
private static void inOrder(TwinTreeAltSE node) {
if (node == null) {
return;
}
inOrder(node.left);
inOrder(node.right);
}
}