Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am working on a BinaryTree in Java. Need help with the following boolean. I wo

ID: 3837667 • Letter: I

Question

I am working on a BinaryTree in Java. Need help with the following boolean. I would like to see what a Boolean update would look like for a Binary tree

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


/*This class stores node information*/
class BinaryTreeNode

{  

     BinaryTreeTNode left, right;

     int value;

     public BinaryTreeNode()

     {

         left = null;

         right = null;

         value = 0;

     }

     public BinaryTreeNode(int n)

     {

         left = null;

         right = null;

         value= n;

     }

     public void assignToLeftNode(BinaryTreeNode n)

     {

         left = n;

     }

     public void assignToRightNode(BinaryTreeNode n)

     {

         right = n;

     }

     public BinaryTreeNode getLeftNode()

     {

         return left;

     }

     public BinaryTreeNode getRightNode()

     {

         return right;

     }

     public void setValue(int d)

     {

         value = d;

     }

     /* Function to get data from node */

     public int getValue()

     {

         return value;

     }   

}


class BinaryTree
{
    private BinaryTreeNode root;
        
     public BinaryTree()

     {
         root=null;
     }
     /* update function will do as per the requirement*/
     public boolean update(int oldValue ,int newValue)
     {
      return search(root,oldValue,newValue);
     }
          private boolean search(BinaryTreeNode r, int oldValue ,int newValue)

     {

         if (r.getValue() == oldValue)
            {
                if(r.getLeftNode==null&&r.getRightNode==null)
                {
                    r.setValue(newValue);
                  
                }
                return true;
            }
         if (r.getLeftNode() != null)
        {
             if (search(r.getLeftNode(), oldValue, newValue))
             {
                 r.assignToLeftNode(newValue);

                 return true;
             }
        }

         if (r.getRightNode() != null)
            {
             if (search(r.getRightNode(), oldValue, newValue))
                {
                    r.assignToRightNode(newValue);
                 return true;
                }
            }
         return false;       

     }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote