I have been having an issue with a Binary Search Tree that I am making for a larger project.
There is a node:
template <class Key, class Value>
struct Node
{
public:
Key key;
vector<Value> value;
Node* leftNode;
Node* rightNode;
Node(Key k, Value v)
{
key = k;
value.push_back(v);
}
};
These nodes are being stored into the BST.
The error occurs in the Insertion function of the BST:
template <class Key, class Value>
void BinarySearchTree<Key, Value>::insert(const Key &key, const Value &value)
{
insert(root, key, value);
}
template <class Key, class Value>
void BinarySearchTree<Key, Value>::insert(Node<Key, Value> *&node, const Key &key, const Value &value)
{
if (node == nullptr)
node = new Node<Key, Value>(key, value);
// Error occurs here.
else if (node->key == key)
node->value.push_back(value);
else if (key < node->key)
insert(node->leftNode, key, value);
else
insert(node->rightNode, key, value);
}
The first node is inserted into the tree without issue, however the second Insertion call throws the error.
Any help in putting me on the right track would be greatly appreciated.