C++ how we return and receive two value at the same time as it is possible in Python?

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;
}
}

The Python version is:

#------------------------------------------------------------

def delete(self, item):

"""remove item from binary search tree
post: item is removed from the tree"""

self.root = self._subtreeDelete(self.root, item)

#------------------------------------------------------------

def _subtreeDelete(self, root, item):

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

#------------------------------------------------------------

def _subtreeDelMax(self, 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

#------------------------------------------------------------

Last edited on
Could you post the code that you are having issues?
You mean like this?
1
2
3
def f(): 
  return 1, 2
a, b = f()


Most directly, you can return a tuple or pair, e.g., given auto f(int a, int b) { return std::make_tuple(a, b); }
Since C++11:
1
2
int a, b;
std::tie(a, b) = f(1, 2);

Since C++17:
auto [a, b] = f(1, 2); // destructuring declaration
http://en.cppreference.com/w/cpp/language/declarations#Structured_binding_declaration

Alternatively, you can modify a reference parameter(s):
1
2
3
auto f(int a, int b, int& out_b) { out_b = b; return a; } 
int b; 
int a = f (1, 2, b);
Last edited on
> 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 )

> _root->_left = _root->_left;
A = A.
Thank you but I just got it!
Topic archived. No new replies allowed.