-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeDouble.java
48 lines (40 loc) · 895 Bytes
/
TreeDouble.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
public class TreeDouble {
public TreeDouble left;
public TreeDouble right;
/**
* Build a tree by adding elements to the left n times recursively
* When going up the recursion, add an element to the right of each element
*
* /\
* /\
* /\
*
*
* @author Joel Beckmann
*/
public static void main(String[] args) {
int n = args.length;
TreeDouble tree = build(n);
inOrder(tree);
}
public TreeDouble() {
this.left = null;
this.right = null;
}
public static TreeDouble build(int height) {
TreeDouble t = new TreeDouble();
if (height > 0) {
t.left = build(height - 1);
TreeDouble d = new TreeDouble();
t.right = d;
}
return t;
}
private static void inOrder(TreeDouble node) {
if (node == null) {
return;
}
inOrder(node.left);
inOrder(node.right);
}
}