forked from kbeathanabhotla/DSAAJ2-Answers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chap04.question.41.BinaryTreeLevelTraverse.java
69 lines (60 loc) · 2.2 KB
/
Chap04.question.41.BinaryTreeLevelTraverse.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//Chap04.question.41.BinaryTreeLevelTraverse.java
import java.util.ArrayDeque;
import java.util.Arrays;
public class BinaryTreeLevelTraverse {
public static void main(String... args) {
BinarySearchTree<Integer> tree = new BinarySearchTree<>();
for (int i : Arrays.asList(4, 1, 3, 2, 5))
tree.insert(i);
tree.levelTraverse(new BinarySearchTree.Function<Integer>() {
@Override
public void f(BinarySearchTree.BinaryTreeNode<Integer> node) {
System.out.print(node.data + " ");
}
});
}
public static class BinarySearchTree<T extends Comparable<? super T>> {
private BinaryTreeNode<T> root;
public void levelTraverse(Function<T> function) {
if (root == null) return;
ArrayDeque<BinaryTreeNode<T>> queue = new ArrayDeque<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
BinaryTreeNode<T> node = queue.poll();
function.f(node);
if (node.left != null)
queue.offer(node.left);
if (node.right != null)
queue.offer(node.right);
}
}
}
public void insert(T t) {
root = insert(t, root);
}
private BinaryTreeNode<T> insert(T t, BinaryTreeNode<T> node) {
if (node == null)
return new BinaryTreeNode<>(t, null, null);
int compareResult = t.compareTo(node.data);
if (compareResult < 0)
node.left = insert(t, node.left);
if (compareResult > 0)
node.right = insert(t, node.right);
return node;
}
interface Function<T> {
void f(BinaryTreeNode<T> node);
}
private static class BinaryTreeNode<T> {
T data;
BinaryTreeNode<T> left, right;
BinaryTreeNode(T t, BinaryTreeNode<T> l, BinaryTreeNode<T> r) {
data = t;
left = l;
right = r;
}
}
}
}