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);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.