-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestBinaryTree.java
114 lines (98 loc) · 4.09 KB
/
TestBinaryTree.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
public class TestBinaryTree {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
BinaryNode root = tree.addRoot("Root");
BinaryNode nodeB = null;
BinaryNode nodeD = null;
BinaryNode nodeE = null;
if (tree.root == null) {
System.out.println("Root is empty");
System.out.println("=== Expect: 'Root'");
System.out.println("=== Got: 'null'");
} else if (!tree.root.value.equals("Root")) {
System.out.println("Root is not equal to 'Root'");
System.out.println("=== Expect: 'Root'");
System.out.println("=== Got: '" + tree.root.value + "'");
}
try {
nodeB = tree.addLeftChild(root, "B");
} catch (Exception e) {
System.out.println("Error while add left child in root");
System.out.println(e.getMessage());
}
if (nodeB != null && !nodeB.parent.value.equals(root.value)) {
System.out.println("Node B parent is not equal to 'Root'");
System.out.println("=== Expect: 'Root'");
System.out.println("=== Got: '" + nodeB.parent.value + "'");
}
try {
nodeB = tree.addLeftChild(root, "B");
System.out.println("Expect erro while add child left to root");
System.out.println("=== Expect: 'Node already have left child'");
} catch (Exception e) {
if (!e.getMessage().equals("Node already have left child")) {
System.out.println("Erro while add child left to root");
System.out.println("=== Expect: 'Node already have left child'");
System.out.println("=== Got: '" + e.getMessage() + "'");
}
}
try {
tree.addRightChild(root, "C");
} catch (Exception e) {
System.out.println("Error while add right child in root");
System.out.println(e.getMessage());
}
try {
nodeD = tree.addRightChild(nodeB, "D");
} catch (Exception e) {
System.out.println("Error while add right child in nodeB");
System.out.println(e.getMessage());
}
try {
nodeE = tree.addRightChild(nodeD, "E");
} catch (Exception e) {
System.out.println("Error while add right child in nodeD");
System.out.println(e.getMessage());
}
if (tree.getNodesCount() != 5) {
System.out.println("Tree doesn't have '5' nodes");
System.out.println("=== Expect: '5'");
System.out.println("=== Got: '" + tree.getNodesCount() + "'");
}
if (tree.getLevelsCount() != 3) {
System.out.println("Tree doesn't have '3' lavels");
System.out.println("=== Expect: '3'");
System.out.println("=== Got: '" + tree.getLevelsCount() + "'");
}
if (nodeB != null && nodeB.getParentsCount() != 1) {
System.out.println("Node B doesn't have '1' parent");
System.out.println("=== Expect: '1'");
System.out.println("=== Got: '" + nodeB.getParentsCount() + "'");
}
if (nodeE != null && nodeE.getParentsCount() != 3) {
System.out.println("Node E doesn't have '3' parents");
System.out.println("=== Expect: '3'");
System.out.println("=== Got: '" + nodeE.getParentsCount() + "'");
}
if (tree.getExternalNodesCount() != 2) {
System.out.println("Tree doesn't have '2' external nodes");
System.out.println("=== Expect: '2'");
System.out.println("=== Got: '" + tree.getExternalNodesCount() + "'");
}
if (!tree.printPreOrder().equals("[Root] - [B] - [D] - [E] - [C]")) {
System.out.println("Tree pre order don't match");
System.out.println("=== Expect: '[Root] - [B] - [D] - [E] - [C]'");
System.out.println("=== Got: '" + tree.printPreOrder() + "'");
}
if (!tree.printPosOrder().equals("[E] - [D] - [B] - [C] - [Root]")) {
System.out.println("Tree pos order don't match");
System.out.println("=== Expect: '[E] - [D] - [B] - [C] - [Root]'");
System.out.println("=== Got: '" + tree.printPosOrder() + "'");
}
if (!tree.printInterOrder().equals("[B] - [D] - [E] - [Root] - [C]")) {
System.out.println("Tree pos order don't match");
System.out.println("=== Expect: '[B] - [D] - [E] - [Root] - [C]'");
System.out.println("=== Got: '" + tree.printInterOrder() + "'");
}
}
}