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