The following code should add a node to my BST structure, but fails to do so. New nodes simply dont get attached to the tree, including the root node. Could anyone point out where the problem is?
The constructor for the tree:
template <typename K, typename T>
MyBinaryTree<K, T>::MyBinaryTree() {
root = nullptr;
}
Function that gets called from the outside:
template <typename K, typename T>
void MyBinaryTree<K, T>::addValue(K key, T value) {
Node *node = new Node(key, value);
addToSubtree(root, node);
}
And the internal private function which has to attach the node:
template <typename K, typename T>
void MyBinaryTree<K, T>::addToSubtree(MyBinaryTree<K, T>::Node *node,
MyBinaryTree<K, T>::Node *toAdd) {
if(node == nullptr) {
node = toAdd;
} else {
if(toAdd->key > node->key) addToSubtree(node->right, toAdd);
else if(toAdd->key < node->key) addToSubtree(node->left, toAdd);
}
}