I am writing some methods for a Binary tree in JAVA. I can\'t seem to get this o
ID: 3834298 • Letter: I
Question
I am writing some methods for a Binary tree in JAVA. I can't seem to get this one right. The main is done and functional. I am adding other methods to it. Can anyone show me what this method would look like in JAVA:
boolean update (int oldValue, int newValue)
-Searches for oldValue in the tree. If found, the data for that node is changed to newValue and the tree is modified such that the node with the new value is placed in the appropriate place in the tree and returns true
-If the oldValue is not found in the tree, the function returns false and the tree stays as is
Explanation / Answer
-Searches for oldValue in the tree. If found, the data for that node is changed to newValue
and the tree is modified such that the node with the new value is placed in the
appropriate place in the tree and returns true
-If the oldValue is not found in the tree, the function returns false and the tree stays as is
boolean update(int oldValue, int newValue)
{
return updateHelper(root, oldValue, newValue);
}
boolean updateHelper(BinaryTree root, int oldValue, int newValue)
{
if(root == null)
return false
else if(root.getInfo() == oldValue)
{
root.setInfo(newValue);
return true;
}
else
return updateHelper(root.getLeft(), oldValue, newValue) || updateHelper(root.getRight(), oldValue, newValue)
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.