To answer your question in a generic way, you would do the allocations and make any calls to functions that may throw
prior to any mutation or change in your data structure:
For example:
bool BinarySearchTree::treeInsert(string firstname, string lastname, string phonenumber)
{
TreeNode* newNode = new TreeNode();
// any other functions that may throw
//...
// now do housekeeping to update tree
}
So in other words, make sure that you have all your data already set up before you update your tree. This is perfectly safe, as any exception thrown will not harm the existing tree. You can have a simple try/catch
acknowledging that the node couldn't be created if an exception is thrown.
The wrong (or more cumbersome) way is this:
bool BinarySearchTree::treeInsert(string firstname, string lastname, string phonenumber)
{
// code that changes the internals of BinarySearchTree
// ...
// now create the node
TreeNode* newNode = new TreeNode();
}
Unless you have a try/catch
that does a rollback of the changes made to the tree, you will end up corrupting the tree using this approach if new
or some other function throws an exception.