1public class BinaryTree<T extends Comparable<T>> {
2 TreeNode<T> root;
3
4 public BinaryTree() {
5 super();
6 this.root = new TreeNode<T>();
7 }
8
9 public BinaryTree(T o) {
10 super();
11 // TODO Auto-generated constructor stub
12 TreeNode<T> node = new TreeNode<T>(o);
13 this.root = node;
14 }
15
16 public void add(T o) {
17 if (root == null) {
18 root = new TreeNode<T>(o);
19 } else {
20 root.insert(o);
21 }
22 }
23
24 public boolean isMember (TreeNode<T> node, T o) {
25 if(node == null) return false;
26
27 if (node.element.compareTo(o) == 0) {
28 return true;
29 } else if (node.element.compareTo(o) < 0) {
30 isMember(node.right, o);
31 } else {
32 isMember(node.left, o);
33 }
34 return false;
35 }
36
37 public void preOrderHelper (TreeNode<T> node) {
38 if (node != null) {
39 node.visit();
40 preOrderHelper(node.left);
41 preOrderHelper(node.right);
42 }
43 }
44
45 public void inOrderHelper(TreeNode<T> node) {
46 if (node != null) {
47 inOrderHelper(node.left);
48 node.visit();
49 inOrderHelper(node.right);
50 }
51 }
52
53 public void postOrderHelper(TreeNode<T> node) {
54 if (node != null) {
55 postOrderHelper(node.left);
56 postOrderHelper(node.right);
57 node.visit();
58 }
59 }
60
61 public void preOrder() {
62 if (root != null) {
63 preOrderHelper(root);
64 }
65 }
66
67 public void inOrder() {
68 if (root != null) {
69 inOrderHelper(root);
70 }
71 }
72
73 public void postOrder() {
74 if (root != null) {
75 postOrderHelper(root);
76 }
77 }
78
79}
80