I needed for an assignment and I an translating a code from Python to C++ but there this line of code where there is a double assignment and i don't know can I go about it.
UPDATES! I have been working on this and It think I found a way around the double assignment issue by updating the node inside the function only since it is passed as a parameter in the method helper. However, I an getting a lot of errors and one specificly has been a pain to me - the error says "[Error] cannot convert 'BST' to 'TreeNode*' in assignment".. am giving the c++ code bellow.
void BST::Delete(const &item)
{
//TreeNode *node;
//_root = node; TreeNode *_root = _subtreeDelete(_root, item);// The error happens here.
}
BST _subtreeDelete(const TreeNode *node, const int &item)
{
TreeNode *node, const int &item, *_root;
_root = node;
if (_root)// same as node != NULL. If it is empty.
{
return _root;
}
if (item < _root->_item)
{
_root->_left = _subtreeDelete(_root->_left, item);
}
else if (item > _root->_item)
{
_root->_right = _subtreeDelete(_root->_right);
}
else
{
if (_root->_left == NULL)
{
_root = _root->_right;
}
else if (_root->_right)
{
_root = _root->_left;
}
else
{
_root->_item = _subtreeDelMax(_root);
}
return _root
}
}
BST _subtreeDelMax(const TreeNode *node);
{
TreeNode *node;
_root = node;
int maxVal;
if (_root->_right == NULL)
{
_root->_left = _root->_left;
return _root->_item;
}
else
{
_root->_right = _root->_right;
maxVal = _subtreeDelMax(_root->_right);
return maxVal;
}
}
if root is None: # Empty tree, nothing to do
return None
if item < root.item: # modify left
root.left = self._subtreeDelete(root.left, item)
elif item > root.item: # modify right
root.right = self._subtreeDelete(root.right, item)
else: # delete root
if root.left is None: # promote right subtree
root = root.right
elif root.right is None: # promote left subtree
root = root.left
else:
# root node can't be deleted, overwrite it with max of
# left subtree and delete max node from the subtree
root.item, root.left = self._subtreeDelMax(root.left)
return root
if root.right is None: # root is the max
return root.item, root.left # return max and promote left subtree
else:
# max is in right subtree, recursively find and delete it
maxVal, root.right = self._subtreeDelMax(root.right)
return maxVal, root
> the error says "[Error] cannot convert 'BST' to 'TreeNode*' in assignment".
> TreeNode *_root = _subtreeDelete(_root, item);
`_subtreeDelete()' returns a BST (¿why?) and you are trying to assign it to a TreeNode pointer.
¿how do you expect that to work?
besides, you are doing nothing with `root', ¿so why do you want it?
> if(_root) // same as node != NULL. If it is empty.
you've got it backwards.
yes, the condition does check if root is not NULL, but that would mean that it has something, that it's not "empty".
So if(_root == NULL) or if( not _root )