Testing and bug fixes

master
Noah Pederson 6 years ago
parent 914db1cc1e
commit af9d52c3f5

@ -6,6 +6,7 @@
#include <iostream>
#include <fstream>
#include "TreeNode.h"
#include <string>
using namespace std;
@ -62,7 +63,7 @@ public:
file << strCout.str();
}
void disableDuplicates() const {
void disableDuplicates() {
allowDuplicates = false;
}
@ -224,6 +225,7 @@ private:
return ptr;
}
//This output is slightly different, but I like it a lot more.
//Courtesy of https://stackoverflow.com/a/26699993
void outputTree(TreeNode<NODETYPE>* ptr, int totalSpaces) {
if (ptr != nullptr) {
if (ptr->rightPtr != nullptr) outputTree(ptr->rightPtr, totalSpaces + 4); // traverse left subtree

@ -72,6 +72,61 @@ int main() {
cout << "\nPostorder traversal\n";
doubleTree.postOrderTraversal();
cout << endl;
//EXTRA TESTS CAUSE WHY NOT
Tree<int> noDupsInt;
noDupsInt.disableDuplicates();
Tree<int> yesDupsInt;
//lets insert some random value
noDupsInt.insertNode(42);
yesDupsInt.insertNode(42);
for (int i = 0; i < 100; ++i) {
try {
yesDupsInt.insertNode(i);
noDupsInt.insertNode(i); // <- Will thrown an exception when you try to insert a duplicate
}
catch ( const exception &e) {
cout << "Unable to insert " << i << " into noDupsInt tree: " << e.what() << endl;
}
}
cout << "Trees: " << endl;
cout << "No duplicates int tree: " << endl;\
noDupsInt.printTree();
getchar();
cout << "Duplicates string tree: " << endl;
yesDupsInt.printTree();
getchar();
Tree<string> noDupsString;
noDupsString.disableDuplicates();
Tree<string> yesDupsString;
noDupsString.insertNode("hjdfj1");
noDupsString.insertNode("test2");
noDupsString.insertNode("ztest332");
noDupsString.insertNode("agasfasfb");
yesDupsString.insertNode("test1");
yesDupsString.insertNode("test2");
yesDupsString.insertNode("test3");
yesDupsString.insertNode("test4");
yesDupsString.insertNode("test4");
try {
noDupsString.insertNode("test2");
}
catch ( const exception &e) {
cout << "Unable to insert into noDupsString tree: " << e.what() << endl;
}
cout << "No duplicates string tree: " << endl;
noDupsString.printTree();
getchar();
cout << "Duplicates string tree: " << endl;
yesDupsString.printTree();
cout << "DONE!" << endl;
getchar();
getchar();
}

Loading…
Cancel
Save