Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Do you know why this isnt compiling? Im suppose to insert a new node into the tr

ID: 3759595 • Letter: D

Question

Do you know why this isnt compiling? Im suppose to insert a new node into the tree and only if its not a duplicate and if it is then return false otherwise true

Helper function: bool InsertRecursive(BinaryTreeNode* tree_node, int value) { return false; }

main part

bool Tree::Insert(int value)

if(BinaryTreeNode == NULL){

return(newNode(value));

}

else{

if (value <= tree_node->value)

BinaryTreeNode->left_child = insert(BinaryTreeNode->left_child, value);

}

else BinaryTreeNode->right_child = insert(BinaryTreeNode->right_child, value){

return tree_node;

}

return InsertRecursive(head,value);

}

if(BinaryTreeNode == NULL){

return(newNode(value));

}

else{

if (value <= BinaryTreeNode->value)

BinaryTreeNode->left_child = insert(BinaryTreeNode->left_child, value);

}

else BinaryTreeNode->right_child = insert(BinaryTreeNode->right_child, value){

return BinaryTreeNode;

}

return InsertRecursive(head,value);

}

Explanation / Answer

Modified Code:

bool Tree::Insert(int value)
{
return InsertRecursive(head, value);
}

bool InsertRecursive(BinaryTreeNode* tree_node, int value)
{
if(tree_node == NULL)
{
  BinaryTreeNode * newNode = new BinaryTreeNode;
  newNode->value = value;
  newNode->left_child = NULL;
  newNode->right_child = NULL;
  tree_node = newNode;
  return true;
}
else if(tree_node->value == value )
{
  return false;
}
else if(tree_node->value > value)
{
  return InsertRecursive(tree_node->right_child, value);
}
else
{
  return InsertRecursive(tree_node->left_child, value);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote