-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
64 lines (46 loc) · 1.22 KB
/
BinaryTree.java
File metadata and controls
64 lines (46 loc) · 1.22 KB
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
57
58
59
60
61
62
63
64
package javaapplication;
public class BinaryTree<E> {
int size;
Node<E> root;
public BinaryTree () {
size =0;
root=null;
}
public BinaryTree(E val) {
root = new Node(val);
size=1;
}
public boolean isEmpty() {
return size==0;
}
public Node<E> addLeft(Node<E> node, E val) {
Node<E> n = new Node(val);
node.addLeft(n);
size++;
return node;
}
public Node<E> addRight(Node<E> node, E val) {
Node<E> n = new Node(val);
node.addRight(n);
size++;
return node ;
}
public void preOrder(Node<E> n) {
if (n==null) return;
System.out.println(n.getInfo());
preOrder(n.getLeft());
preOrder(n.getRight());
}
public void inOrder(Node<E> n) {
if (n==null) return;
inOrder(n.getLeft());
System.out.println(n.getInfo());
inOrder(n.getRight());
}
public void postOrder(Node<E> n) {
if (n==null) return;
postOrder(n.getLeft());
postOrder(n.getRight());
System.out.println(n.getInfo());
}
}