Question
We have been assigned a Binary Search Tree project for homework (C++ Data Structs) and for the most part my tree program works perfectly fine
We have been assigned a Binary Search Tree project for homework (C++ Data Structs) and for the most part my tree program works perfectly fine except for the "Remove" function. The error i am getting is "Control may reach end of non-void function." This means there is something wrong with the return value, but I am unsure what to do to fix it. Here is the definition/algorithm we are supposed to follow:
A BST is a binary tree that (if not empty) also follows two storage rules regarding its nodes items: |
-- | For any node n of the tree, every item in ns left subtree (LST), if not empty, is less than or equalthe item in n |
-- | For any node n of the tree, every item in ns right subtree (RST), if not empty, is greater than the item in n |
Here is my code:
bool bst_remove(btNode*& bst_root, int remInt)
{
/// Check if tree is empty
if(bst_root == 0)
{
return false;
}
if(bst_root->data > remInt)
{
// Target int less than root
bst_remove(bst_root->left, remInt);
}
else if(bst_root->data < remInt)
{
// Target int greater than root
bst_remove(bst_root->right, remInt);
}
else
{
// Target int equal to root
if(bst_root->left != 0 && bst_root->right != 0){
/// BOTH LST and RST children present
bst_remove_max(bst_root->left, bst_root->data);
}
else
{
// Only LST or RST or non children present.
btNode* old_bst_root = bst_root;
if(bst_root->left == 0 && bst_root->right != 0){
// Has RST but no LST.
bst_root = bst_root->right;
} else if (bst_root-> left != 0 && bst_root->right == 0){
// Has LST but no RST.
bst_root = bst_root->left;
}
else
{
// Parent only
bst_root = 0;
}
// Free up old head
delete old_bst_root;
}
return true;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started