find out the problem with this function because it works find, but when in sert with large amount of numbers it start to allow duplicates
template<class Key, class Value>
void BinarySearchTree<Key, Value>::insert(const std::pair<const Key, Value> &keyValuePair)
{
// TODO
if (root_ == nullptr){ // if tree is empty, insert at root
root_ = new Node<Key, Value>(keyValuePair.first, keyValuePair.second, nullptr);
return;
}
code
if (internalFind(keyValuePair.first) != nullptr){ // if key exists, update value
internalFind(keyValuePair.first) -> setValue(keyValuePair.second);
return;
}
Node<Key, Value>* newNode = new Node<Key, Value>(keyValuePair.first, keyValuePair.second, nullptr);
Node<Key, Value>* curr = root_;
while (curr != nullptr){
if (curr -> getKey() > keyValuePair.first){ // go left
if (curr -> getLeft() == nullptr){// if no left child, insert
curr -> setLeft(newNode);
newNode -> setParent(curr);
return;
}
curr = curr -> getLeft();
}
else{ // go right
if (curr -> getRight() == nullptr){// if no right child, insert
curr -> setRight(newNode);
newNode -> setParent(curr);
return;
}
curr = curr -> getRight();
}
}
}
Assistant
The problem with your insert function in the BinarySearchTree implementation likely stems from how it handles duplicates and inserts new nodes. Even though you've included a check to update the value for an existing key (if it already exists in the tree), if the input of large amounts of numbers introduces duplicates in relation to the < and > comparison logic, your implementation allows for unintended duplicates.
Here are a few key issues and improvements you might want to consider:
Comparison Logic: The way you are inserting nodes relies on the assumption that keys are unique. The use of >, <, and potentially == can lead to issues if the comparison logic isn't defined to strictly enforce uniqueness. If your Key type supports equality checks via operator< and you have a specific sort order for duplicates, you need to handle the duplicates explicitly.
Node Insertion Logic: You have the curr pointer traversing through the tree to find the correct insert position. However, if two nodes are deemed "equal" due to the comparison operations, you could end up attempting to insert two nodes with the same key into the tree. Since C++ has no default behaviors for what to do with equivalent keys, you need to ensure that existing keys are not permitted for insertion in the tree.
To prevent duplicates, it is essential to update the logic as follows:
Suggested Code Fixes:
cpp
template<class Key, class Value>
void BinarySearchTree<Key, Value>::insert(const std::pair<const Key, Value> &keyValuePair)
{
// Check for an empty tree
if (root_ == nullptr) {
root_ = new Node<Key, Value>(keyValuePair.first, keyValuePair.second, nullptr);
return;
}
// Check if the key already exists and update if necessary
Node<Key, Value>* existingNode = internalFind(keyValuePair.first);
if (existingNode != nullptr) { // if key exists, update value
existingNode->setValue(keyValuePair.second);
return;
}
Node<Key, Value>* newNode = new Node<Key, Value>(keyValuePair.first, keyValuePair.second, nullptr);
Node<Key, Value>* curr = root_;
Node<Key, Value>* parent = nullptr;
while (curr != nullptr) {
parent = curr; // Track the parent node
if (keyValuePair.first < curr->getKey()) { // go left
curr = curr->getLeft();
} else if (keyValuePair.first > curr->getKey()) { // go right
curr = curr->getRight();
} else {
// This part should not be reached due to the earlier check.
// But entirely safe to avoid any insertion of duplicates
delete newNode; // Avoid memory leak
return; // Do nothing if the key is equal
}
}
// Insert the new node
if (keyValuePair.first < parent->getKey()) {
parent->setLeft(newNode);
} else {
parent->setRight(newNode);
}
newNode->setParent(parent);
}
Summary of Fixes:
Key Comparison: Ensure that keys are being compared correctly to enforce uniqueness.
Parent Tracking: A parent pointer tracks where to attach the new node, reducing the potential for faulty linking.
Duplicate Handling: The function explicitly checks for duplicates before performing any insertion, helping avoid incorrect node additions.
This change ensures that your Binary Search Tree maintains its properties accurately, prevents duplicates, and provides expected functionality when handling larger numbers of inputs.