Before I start, I just want to preface that this is my first post on stackoverflow.
I'm working on a binary search tree for class. I have an insert function finished, as well as a traversal that prints nodes. However, I am getting an error preventing me from compiling to test my tree. Here is my .h file, it is a template, so everything is included here.
I am getting a LNK error and from what I've read, I still can't seem to figure why this is happening.
Error 3 error LNK2019: unresolved external symbol "private: void __thiscall main_dc::BST::helperInOrder(struct main_dc::Node * &)" (?helperInOrder@?$BST@H@main_dc@@AAEXAAPAU?$Node@H@2@@Z) referenced in function "public: void __thiscall main_dc::BST::traversalInOrder(void)" (?traversalInOrder@?$BST@H@main_dc@@QAEXXZ) \codingpc\Users\Devyn\Documents\Visual Studio 2013\Projects\2420_Data_And_Algorithm\BinarySearchTree_Project_07\BinarySearchTree_Project_07\Driver.obj BinarySearchTree_Project_07
template <class T>
class BST
{
public:
BST();
~BST(); // delete root that deletes left and right child
int getSize();
void print();
void erase(T item); // item is an integer number
void insert(T item);
// Traversal functions (pre-order, in-order, post-order)
// void traverselPreOrder();
void traversalInOrder();
void traversalPostOrder();
private:
// Helper function for insert
void insertHelper(Node<T>*& n, T item);
//Traversal helper functions
void helperPostOrder(Node<T>*& n);
void helperInOrder(Node<T>*& n);
// member data
Node<T>* root;
int size;
};
template <class T>
BST<T>::BST()
{
root = nullptr;
size = 0;
}
template <class T>
BST<T>::~BST()
{
// delete root;
}
template <class T>
int BST<T>::getSize()
{
// return size;
// return number of elements in the tree
}
template <class T>
void BST<T>::print()
{
// pre order traversal
// print one element per line
// indent according to the depth (see page 505)
}
template <class T>
void BST<T>::traversalInOrder()
{
helperInOrder(root);
}
template <class T>
void helperInOrder(Node<T>*& nPtr)
{
// something in tree - if root != null
if (root != nullptr)
{
// if root != null... if left child pointer != null, recursively call helper, passing ptrs left
if (nPtr->lchild != nullptr)
{
helperInOrder(nPtr->lchild);
}
// print out current node
cout << nPtr->data;
if (nPtr->rchild != nullptr)// if right child pointer != null, recursively call helper, passing ptrs right
{
helperInOrder(nPtr->rchild)
}
}
else // else tree is empty print out tree is empty
{
cout << "The tree is empty" << endl;
}
}
template <class T>
void BST<T>::erase(T item)
{
// erase the occurrence of the item in the tree (there should only be 1
// or 0 because there are no dupes)
// resulting tree should still be a binary search tree (balanced AND full?)
}
template <class T>
void BST<T>::insert(T item)
{
insertHelper(root, item);
}
template <class T>
void BST<T>::insertHelper(Node<T>*& nPtr, T item) // insert an item into BTS, do not allow duplicate numbers
{
if (root == nullptr) // if nothing in tree - insert node
{
root = new Node<T>(item);
}
// if something in tree - check to see if less or greater than node you are on
else if (item < nPtr->data) // if item is less than data in node and nothing is in lchild, create a node
{
if (nPtr->lchild != nullptr) // if left child has a value, traverse left once
{
insertHelper(nPtr->lchild, item);
}
else
{
nPtr->lchild = new Node<T>(item);
}
}
else if (item > nPtr->data) // if item is greater than data node and nothing is in rchild, create a node
{
if (nPtr->rchild != nullptr) // if right child already exsists, traverse right once
{
insertHelper(nPtr->rchild, item);
}
else
{
nPtr->rchild = new Node<T>(item);
}
}
else // parent has no place for item to go
{
cout << "The input " << item << " has already been added to the tree." << endl;
}
}
}