-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_binary_tree.cpp
196 lines (153 loc) · 4.9 KB
/
test_binary_tree.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
191
192
193
194
195
#include <iostream>
#include "BinaryTree.h"
using namespace std;
typedef BinaryTreeNode<int> IntNode;
typedef BinaryTree<int> IntTree;
void printHeader() {
cout << endl << "#####################################" << endl;
}
const char* printFound(bool fFound) {
if (fFound) {
return "FOUND";
} else {
return "NOT FOUND";
}
}
int main(int /*argc*/, char** /*argv*/) {
cout << endl << "Beginning BinaryTree Test." << endl;
//
// Test insertions and traversal
//
printHeader();
IntTree tree;
cout << endl << "Testing Insertions And Traversal..." << endl;
cout << endl << "Inserting 50";
tree.insert(50);
cout << endl << "Inserting 30";
tree.insert(30);
cout << endl << "Inserting 80";
tree.insert(80);
cout << endl << "Inserting 80";
tree.insert(80);
cout << endl << "Inserting 40";
tree.insert(40);
cout << endl << "Inserting 10";
tree.insert(10);
cout << endl << "Inserting 100";
tree.insert(100);
cout << endl << "Inserting 0";
tree.insert(0);
cout << endl;
cout << endl << "PRE ORDER:";
cout << endl << "Expected: 50, 30, 10, 0, 40, 80, 100";
cout << endl << "Actual: ";
tree.print(IntTree::PRE_ORDER);
cout << endl << "IN ORDER:";
cout << endl << "Expected: 0, 10, 30, 40, 50, 80, 100";
cout << endl << "Actual: ";
tree.print(IntTree::IN_ORDER);
cout << endl << "POST ORDER:";
cout << endl << "Expected: 0, 10, 40, 30, 100, 80, 50";
cout << endl << "Actual: ";
tree.print(IntTree::POST_ORDER);
cout << endl << "BREADTH FIRST:";
cout << endl << "Expected: 50, 30, 80, 10, 40, 100, 0";
cout << endl << "Actual: ";
tree.print(IntTree::BREADTH_FIRST);
//
// Testing finds
//
printHeader();
cout << endl << "Testing Searches...";
int shouldFind[] = { 50, 30, 80, 40, 10, 100, 0 };
int shouldFindSize = sizeof(shouldFind) / sizeof(shouldFind[0]);
int shouldNotFind[] = { 51, 3, 87 };
int shouldNotFindSize = sizeof(shouldNotFind) / sizeof(shouldNotFind[0]);
for (int index = 0; index < shouldFindSize; index++) {
const IntNode* const pNode = tree.find(shouldFind[index]);
bool fFound = pNode != NULL;
if (pNode) {
assert(pNode->data == shouldFind[index]);
}
cout << endl;
cout << endl << shouldFind[index];
cout << endl << "Expected: FOUND";
cout << endl << "Actual: " << printFound(fFound);
}
for (int index = 0; index < shouldNotFindSize; index++) {
const IntNode* const pNode = tree.find(shouldNotFind[index]);
bool fFound = pNode != NULL;
cout << endl;
cout << endl << shouldNotFind[index];
cout << endl << "Expected: NOT FOUND";
cout << endl << "Actual: " << printFound(fFound);
}
cout << endl;
//
// Test deletions
//
printHeader();
cout << endl << "Testing Deletions (Traversals IN ORDER)..." << endl;
cout << endl << "Expected: 0, 10, 30, 40, 80, 100";
cout << endl << "Actual: ";
tree.remove(50);
tree.print(IntTree::IN_ORDER);
cout << endl << "Expected: 0, 10, 30, 40, 80";
cout << endl << "Actual: ";
tree.remove(100);
tree.print(IntTree::IN_ORDER);
cout << endl << "Expected: 0, 30, 40, 80";
cout << endl << "Actual: ";
tree.remove(10);
tree.print(IntTree::IN_ORDER);
cout << endl << "Expected: 0, 30, 40, 80";
cout << endl << "Actual: ";
tree.remove(3);
tree.print(IntTree::IN_ORDER);
cout << endl << "Expected: 0, 40, 80";
cout << endl << "Actual: ";
tree.remove(30);
tree.print(IntTree::IN_ORDER);
cout << endl << "Expected: 0, 40";
cout << endl << "Actual: ";
tree.remove(80);
tree.print(IntTree::IN_ORDER);
cout << endl << "Expected: 40";
cout << endl << "Actual: ";
tree.remove(0);
tree.print(IntTree::IN_ORDER);
cout << endl << "Expected: NULL Root";
cout << endl << "Actual: ";
tree.remove(40);
tree.print(IntTree::IN_ORDER);
cout << endl << "Expected: NULL Root";
cout << endl << "Actual: ";
tree.remove(3);
tree.print(IntTree::IN_ORDER);
//
// Test deletion of node with two children
//
printHeader();
IntTree deleteTree;
cout << endl << "Testing Deletion Of Node With Two Children..." << endl;
cout << endl << "Inserting 30";
deleteTree.insert(30);
cout << endl << "Inserting 5";
deleteTree.insert(5);
cout << endl << "Inserting 10";
deleteTree.insert(10);
cout << endl << "Inserting 0";
deleteTree.insert(0);
cout << endl;
cout << endl << "Initial Tree..." << endl;
deleteTree.print();
cout << endl << "Expected: 0, 10, 30";
cout << endl << "Actual: ";
deleteTree.remove(5);
deleteTree.print();
//
// Done.
//
printHeader();
cout << endl << "Done With BinaryTree Test." << endl;
}