-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTree.java
55 lines (50 loc) · 1.15 KB
/
BinaryTree.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
import java.util.ArrayList;
/* used in DSTest.java to test IBTree
*
*/
public class BinaryTree {
static class Node {
Node left;
Node right;
int value;
public Node(int value) {
this.value = value;
}
}
public void insert(Node node, int value) {
if (value < node.value) {
if (node.left != null) {
insert(node.left, value);
} else {
node.left = new Node(value);
}
} else if (value > node.value) {
if (node.right != null) {
insert(node.right, value);
} else {
node.right = new Node(value);
}
}
}
public void getInOrder(Node node, ArrayList<Integer> a) {
if (node != null) {
getInOrder(node.left,a);
a.add(node.value);
getInOrder(node.right, a);
}
}
public void getPostOrder(Node node, ArrayList<Integer> a) {
if (node != null) {
getPostOrder(node.left, a);
getPostOrder(node.right, a);
a.add(node.value);
}
}
public void getPreOrder(Node node, ArrayList<Integer> a) {
if (node != null) {
a.add(node.value);
getPreOrder(node.left, a);
getPreOrder(node.right, a);
}
}
}