Menu

[Solved]Java Need Following Code Modified Duplicates Cannot Inserted Runs Red Black Binary Search Q37187226

In java, I need the following code to be modified so thatduplicates cannot be inserted and it runs as a red-black binarysearch tree

package application;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Vector;

import javafx.geometry.Point2D;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

public class Tree {
   private Node root; // the root node of the tree
  
  
   /*******************************  Implementation Here:*****************************************/
  
   /*
   * The function below is to insert a new node to thetree. You need to modify it by imposing “Red-Black”constraints
   * If a node is successfully inserted, it returns”true”
   * If the node to be inserted has the value alreadyexist in the tree, it should not be inserted and return “false”. Soyou need to modify the code to forbid
   * nodes with repeating values to be inserted.
   */
   public boolean insertNode(Node node) {
      
       //Here is just an example ofsetting colors for a node. So far, it is in green color. But youneed to modify the code to dynamically adjust the color to
       //either RED or BLACK according tothe red-black logic
       node.setColor(Node.RED);
      
       // if the root exists
       if (root == null) {
           root = node; //let the root point to the current node
       } else {
           Nodecurrent_node = root;
           while(current_node != null) {
              int value = current_node.getValue();
              if (node.getValue() < value) // go to theleft sub-tree
              {
                  if (current_node.getLeft() !=null) // if the left node is
                                                   // not empty
                  {
                     current_node = current_node.getLeft();
                  } else // put node as theleft child of current_node
                  {
                     current_node.setLeft(node);
                     current_node = null;
                  }
              } else // go to the right
              {
                  if (current_node.getRight()!= null) // if the right node is
                                                       // notempty
                  {
                     current_node = current_node.getRight();
                  } else // put node as theright child of current_node
                  {
                     current_node.setRight(node);
                     current_node = null;
                  }

              }
           }
       }

       return true;
   }

Expert Answer


Answer to In java, I need the following code to be modified so that duplicates cannot be inserted and it runs as a red-black binar… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *