-
Notifications
You must be signed in to change notification settings - Fork 1
/
TreeTest.java
96 lines (83 loc) · 2.5 KB
/
TreeTest.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import org.junit.Assert;
import org.junit.Test;
/**
* Операции с бинарным деревом
*/
public class TreeTest extends Assert {
static TreeOperation sum;
static TreeOperation depth;
static {
depth = node -> {
int r = 1;
if (node.left != null)
r = Math.max(r, 1 + depth.apply(node.left));
if (node.right != null)
r = Math.max(r, 1 + depth.apply(node.right));
return r;
};
sum = node -> {
int r = node.value;
if (node.left != null)
r += sum.apply(node.left);
if (node.right != null)
r += sum.apply(node.right);
return r;
};
}
public static void main(String[] args) {
new TreeTest().testTree();
}
@Test
public void testTree() {
// 6
// 5 1
// 4 3 8
// 2
TreeNode tree = TN(
6,
TN(5,
TN(4,
TN(2)),
TN(3)),
TN(1,
TN(8))
);
assertEquals(4, depth.apply(tree));
assertEquals(3, depth.apply(tree.left));
assertEquals(2, depth.apply(tree.right));
assertEquals(29, sum.apply(tree));
assertEquals(14, sum.apply(tree.left));
assertEquals(9, sum.apply(tree.right));
}
TreeNode TN(int value, TreeNode left, TreeNode right) {
return new TreeNode(value, left, right);
}
TreeNode TN(int value, TreeNode left) {
return new TreeNode(value, left);
}
TreeNode TN(int value) {
return new TreeNode(value);
}
// Операция с деревом
interface TreeOperation {
int apply(TreeNode node);
}
static class TreeNode {
int value; // Значение в каждом узле дерева
TreeNode left;
TreeNode right; // Левое поддерево и правое поддерево
// Конструктор
TreeNode(int value, TreeNode left, TreeNode right) {
this.value = value;
this.left = left;
this.right = right;
}
TreeNode(int value, TreeNode left) {
// Вызов другого конструктора
this(value, left, null);
}
TreeNode(int value) {
this(value, null);
}
}
}