-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
190 lines (141 loc) · 6.01 KB
/
main.cpp
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <iostream>
#include "tree.h"
#include "tree.cpp"
int main() {
cout << endl << endl << endl << endl;
cout << "---------- BEGINNING OF TEST ----------" << endl;
cout << "---------------------------------------" << endl;
cout << "Press ENTER until the end to reveal tests..." << endl;
cout << endl;
cin.get();
BinaryTree tree;
cout << "------- TESTING insert FUNCTION -------" << endl;
cout << "---------------------------------------" << endl;
cout << "Inserting values in tree..." << endl;
int nodeValues[] = {15, 10, 20, 5, 12, 17, 25, 3, 7, 11, 13, 16, 19, 22, 27, 1, 4, 6, 9, 14, 18, 21, 23, 26, 28};
for(int i = 0; i < 25; i++) {
cout << "Inserting value " << i << " ..."<< endl;
tree.insert(nodeValues[i]);
}
cout << endl << endl;
cin.get();
cout << "------ TESTING preOrder FUNCTION ------" << endl;
cout << "---------------------------------------" << endl;
cout << "Displaying the values preOrder..." << endl;
tree.preOrder();
cout << endl << endl;
cin.get();
cout << "------ TESTING inOrder FUNCTION ------" << endl;
cout << "--------------------------------------" << endl;
cout << "Displaying the values inOrder..." << endl;
tree.inOrder();
cout << endl << endl;
cin.get();
cout << "-- TESTING INSERTION OF DUPLICATE VALUES --" << endl;
cout << "-------------------------------------------" << endl;
cout << "Attempting to insert some duplicate values..." << endl;
cout << "Insert function prevents this." << endl;
cout << "Inserting duplicate value 15 ..." << endl;
tree.insert(15);
cout << "Inserting duplicate value 7 ..." << endl;
tree.insert(7);
cout << "Inserting duplicate value 6 ..." << endl;
tree. insert(6);
cout << endl;
cout << "Displaying inOrder again after attempting to insert the duplicate values..." << endl;
tree.inOrder();
cout << endl;
cout << "Inserting duplicate values failed. No duplicates in the tree. " << endl;
cout << endl << endl;
cin.get();
cout << "------ TESTING findKey FUNCTION ------" << endl;
cout << "--------------------------------------" << endl;
cout << "Searching for number 5..." << endl;
if (tree.findKey(5)) {
cout << "Number 5 is found in the tree." << endl;
} else {
cout << "Number 5 is not in the tree." << endl;
}
cout << endl;
cout << "Searching for number 24..." << endl;
if (tree.findKey(24) == true) {
cout << "Number 24 is found in the tree." << endl;
} else {
cout << "Number 24 is not in the tree." << endl;
}
cout << endl << endl;
cin.get();
cout << "------ TESTING findMin and findMax FUNCTION ------" << endl;
cout << "--------------------------------------------------" << endl;
cout << "Searching and displaying the minimum value in the tree..." << endl;
cout << "Minimum key is: " << tree.minKey() << endl;
cout << "Searching and displaying the maximum value in the tree..." << endl;
cout << "Maximum key is: " << tree.maxKey() << endl;
cout << endl << endl;
cin.get();
cout << "------ TESTING height FUNCTION ------" << endl;
cout << "-------------------------------------" << endl;
cout << "Displaying the height of the tree..." << endl;
cout << "The root of the tree is: " << tree.height() << endl;
cout << endl << endl;
cin.get();
cout << "------ TESTING delete FUNCTION ------" << endl;
cout << "-------------------------------------" << endl;
cout << "Deleting individual nodes in the tree..." << endl;
cout << "Deleting number 22..." << endl;
tree.remove(22);
cout << "Deleting number 1..." << endl;
tree.remove(1);
cout << "Deleting number 28..." << endl;
tree.remove(28);
cout << "Now displaying the inOrder to see if the removal is successful..." << endl;
tree.inOrder();
cout << endl;
cout << "Individual nodes deleted successfully." << endl;
cout << endl << endl;
cin.get();
cout << "------ TESTING COPY CONSTRUCTOR ------" << endl;
cout << "--------------------------------------" << endl;
cout << "Creating second that is a copy of our original tree..." << endl;
BinaryTree newTree1(tree);
cout << "Displaying the inOrder for the new tree..." << endl;
newTree1.inOrder();
cout << endl << endl;
cin.get();
cout << "------ TESTING OVERLOADED ASSIGNMENT OPERATOR ------" << endl;
cout << "----------------------------------------------------" << endl;
cout << "Creating third tree that is going to be the copy of the second tree... " << endl;
BinaryTree newTree2;
newTree2 = newTree1;
cout << "Displaying the inOrder for the new tree..." << endl;
newTree2.inOrder();
cout << endl << endl;
cin.get();
cout << "------ TESTING countNodes ------" << endl;
cout << "--------------------------------" << endl;
cout << "Counting the nodes in the tree..." << endl;
cout << "The number of nodes in the tree is " << tree.countNodes() << endl;
cout << endl << endl;
cin.get();
cout << "------ TESTING countFullNodes ------" << endl;
cout << "--------------------------------" << endl;
cout << "Counting the nodes with two children..." << endl;
cout << "The number of nodes with two children is " << tree.countFullNodes() << endl;
cout << endl << endl;
cin.get();
cout << "------ TESTING countLeafNodes ------" << endl;
cout << "--------------------------------" << endl;
cout << "Counting the leaf nodes..." << endl;
cout << "The number of leaf nodes is " << tree.countLeafNodes() << endl;
cout << endl << endl;
cin.get();
cout << "------ TESTING levelOrderTraversal ------" << endl;
cout << "--------------------------------" << endl;
cout << "Level order is: ";
tree.levelOrderTraversal();
cout << endl << endl;
cin.get();
cout << "-------------- END OF TEST --------------" << endl;
cout << "-----------------------------------------" << endl;
return 0;
}