forked from JaiCantCode/HW2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
161 lines (129 loc) · 4.74 KB
/
Main.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**********************************************************
*
* Place your name / section number in the 'HW2.java' file
* (NOT this file). This main routine is a driver routine for
* testing the methods in the file 'HW1.java'.
*
* DO NOT MODIFY THIS FILE
*
*********************************************************/
public class Main {
/*
* Function main() should be used for your UNIT TESTING. Feel free to change
* the code here in order to enhance your testing and / or add debugging code.
* However, your program must pass the existing tests provided in order to
* get 100%.
*/
public static void main(String[] args) {
int assignmentScore = 0;
boolean errorFlag = false;
BinaryTree tree1 = new BinaryTree();
BinaryTree tree2 = new BinaryTree();
System.out.println("\nProgram test driver starting ...");
// Initial population of the binary tree
tree1.insert(50);
tree1.insert(55);
tree1.insert(2);
tree1.insert(15);
tree1.insert(20);
tree1.insert(0);
tree1.insert(99);
tree1.insert(3);
tree1.insert(45);
tree1.insert(8);
// Validate nothing changed in HW2.java such that the tree was built incorrectly.
String treeContents = tree1.preOrder();
if ( ! treeContents.toString().equals("50 55 15 3 45 20 8 2 0 99 ") ) {
System.out.println("HW2.java was modified resulting incorrectly constructing the tree!");
return;
}
/**************************************
*
* Testing of the replaceValue method
*
***************************************/
// Replace values in the binary tree constructed above.
tree1.replaceValue(3, -3);
tree1.replaceValue(0, -11);
tree1.replaceValue(2, 99);
// Validate the tree was updated correctly.
treeContents = tree1.preOrder();
if ( ! treeContents.toString().equals("50 55 15 -3 45 20 8 99 -11 99 ") ) {
System.out.println("Failure (1): replaceValue method failed testing!");
errorFlag = true;
}
// Adjust assignment score
if ( ! errorFlag )
assignmentScore += 25;
else
errorFlag = false;
/**************************************
*
* Testing of the findMin method
*
***************************************/
// find the minimum value from tree1 used in previous test, should be -11
if ( tree1.findMin() != -11 ) {
System.out.println("Failure (2): findMin method failed testing!");
errorFlag = true;
}
// Construct a second binary tree, and use that for testing as well
tree2.insert(5);
tree2.insert(-6);
tree2.insert(-24);
tree2.insert(32);
tree2.insert(21);
tree2.insert(12);
// find the minimum value from tree that was just created, should be -24
if ( tree2.findMin() != -24 ) {
System.out.println("Failure (3): findMin method failed testing!");
errorFlag = true;
}
// Adjust assignment score
if ( ! errorFlag )
assignmentScore += 25;
else
errorFlag = false;
/**************************************
*
* Testing of the nodesGT method
*
***************************************/
// find the number of nodes in tree 1 having a data value > 20, should be 5
if ( tree1.nodesGT(20) != 5 ) {
System.out.println("Failure (4): nodesGT method failed testing!");
errorFlag = true;
}
// find the number of nodes in tree 2 having a data value > 20, should be 2
if ( tree2.nodesGT(20) != 2 ) {
System.out.println("Failure (5): nodesGT method failed testing!");
errorFlag = true;
}
// Adjust assignment score
if ( ! errorFlag )
assignmentScore += 25;
else
errorFlag = false;
/**************************************
*
* Testing of the average method
*
***************************************/
// find the number of nodes in tree 1 having a data value > 20, should be 5
if ( tree1.average() != 37.7 ) {
System.out.println("Failure (6): nodesGT method failed testing!");
errorFlag = true;
}
// find the number of nodes in tree 2 having a data value > 20, should be 2
if ( tree2.average() != 6.666666666666667 ) {
System.out.println("Failure (7): nodesGT method failed testing!");
errorFlag = true;
}
// Adjust assignment score
if ( ! errorFlag )
assignmentScore += 25;
else
errorFlag = false;
System.out.println("\nFinal assignment score is " + assignmentScore );
} // end main()
} // end class Main()