-1
我正在使用C++實現BST,但是在實現插入功能後,我發現我只能在樹中插入一個節點。我試過許多辦法來解決這個問題,但他們沒有工作......C++ BST插入
這是我實現插入功能的:
void BSTree::insertHelper(Customer* customer, Node* currentNode, Node* parent)
{
if (currentNode == NULL)
{
Node* newNode = new Node(customer);
currentNode = newNode;
newNode->setParent(parent);
return;
}
if (*customer < *currentNode->getCustomer())
insertHelper(customer, currentNode->getLeft(), currentNode);
else insertHelper(customer, currentNode->getRight(), currentNode);
}
bool BSTree::insert(string lastName, char initial, int account)
{
Customer* customer = new Customer(lastName, initial, account);
if (isEmpty())
{
Node* newNode = new Node(customer);
root = newNode;
return true;
}
Node* currentNode = root;
insertHelper(customer, currentNode, NULL);
return true;
}
感謝您對您的所有幫助。