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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!