The problem that I am having is that my root node doesn't ever seem to actually end up pointing to anything. I had my driver perform a series of inserts, and when I went into debugging mode and watched what happened the node was created to add to the tree, but it never actually gets put on the tree. I'm not sure what I did wrong.
#include "BinaryTree.h"
/*Creates an empty Binary Search Tree
Postcondition: The root node is set to a null pointer */
BinaryTree::BinaryTree() {
root = nullptr;
}
/*Deallocates all elements in the binary search tree
Postcondition: The root node is set to a null pointer */
BinaryTree::~BinaryTree() {
destroyTree();
}
void BinaryTree::inOrderTraversal() {
inOrderHelper(root);
}
/*Inserts the input integer into the tree
Postcondition: The input is inserted into the proper position */
void BinaryTree::add(int i) {
addHelper(i, root);
}
/*Destroys all elements in the Binary Search Tree
Postcondition: The root node is set to a null pointer */
void BinaryTree::destroyTree() {
destroy(root);
}
/*Deallocates the inputted node and all of its children
Postcondition: The inputted note is set to a null pointer */
void BinaryTree::destroy(node* n) {
if(n != nullptr) {
destroy(n->leftChild);
destroy(n->rightChild);
delete n;
n = nullptr;
}
}
void BinaryTree::addHelper(int input, node* n) {
if(n == nullptr) {
n = new node;
n->value = input;
n->leftChild = nullptr;
n->rightChild = nullptr;
}
elseif(n->value == input) {
cout << "Value already in tree.\n";
cin.get();
}
elseif(input > n->value) addHelper(input, n->rightChild);
elseif(input < n->value) addHelper(input, n->leftChild);
}
void BinaryTree::deleteHelper(int) {
}
void BinaryTree::inOrderHelper(node* n) {
if(n == nullptr) return;
inOrderHelper(n->leftChild);
cout << n->value << "\n";
inOrderHelper(n->rightChild);
}