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

Please help. Thank you! My code for the BinarySearchTree is below class BinarySe

ID: 3826930 • Letter: P

Question

Please help. Thank you!

My code for the BinarySearchTree is below

class BinarySearchTree {

class Node {

int key;

Node left, right;

public Node(int item) {

key = item;

left = right = null;

}
  
}

// Root of BST

Node root;

// Constructor

BinarySearchTree() {

root = null;

}

// This method mainly calls insertRec()

void insert(int key) {

root = insertRec(root, key);

}

/* A recursive function to insert a new key in BST */

Node insertRec(Node root, int key) {

/* If the tree is empty, return a new node */

if (root == null) {

root = new Node(key);

return root;

}

/* Otherwise, recur down the tree */

if (key < root.key)

root.left = insertRec(root.left, key);

else if (key > root.key)

root.right = insertRec(root.right, key);

/* return the (unchanged) node pointer */

return root;

}

// This method mainly calls InorderRec()

void inorder() {

inorderRec(root);

}

// A utility function to do inorder traversal of BST

void inorderRec(Node root) {

if (root != null) {

inorderRec(root.left);

System.out.println(root.key);

inorderRec(root.right);

}

}

// Driver Program to test above functions

public static void main(String[] args) {

BinarySearchTree tree = new BinarySearchTree();

tree.insert(50);

tree.insert(30);

tree.insert(20);

tree.insert(40);

tree.insert(70);

tree.insert(60);

tree.insert(80);

// print inorder traversal of the BST

tree.inorder();
}
}

RSTNodw class to tom class RFNod olor field value field to each node (let field he narysearuh the ReTre ude the fallon lement Left Rota d Right R hads RRNacka class. Len-Ratate algorithm is given below Following that algorithm, devise the Right. Rotate algorithm and implement LEFT-ROTATE (T set y ight btree intox's right link s parent elseif p left put x on y's left implement ede Red Buck Tr the insert method. arysearchTree algorithm below). In class implement the case for which parent of r is the len hild of ge CS33 Lab Assigt RB-INSERT(T. Troor while nil else x x right elseif z.key y.key else y.rigir z z, left z, color RED RB-INSERT-FIXUPOT, z) while z p, color RED p.p. left z.p.p right if y color case y color BLACK case 2.P.p color RED case case P.P else if z,p, right LEFT-ROTATE case 2 2.p color BLACK z.p.p. color RED RIGHT-ROTATEIT.z.p.p) case 3 else (same as then clause with "right" and "left" exchanged Traor.color CS303 Lab Assigement 4, Write RBTren class. Make

Explanation / Answer

import java.util.Scanner;

/* Class BSTNode */

class BSTNode

{

     BSTNode left, right;

     int data;

     /* Constructor */

     public BSTNode()

     {

         left = null;

         right = null;

         data = 0;

     }

     /* Constructor */

     public BSTNode(int n)

     {

         left = null;

         right = null;

         data = n;

     }

     /* Function to set left node */

     public void setLeft(BSTNode n)

     {

         left = n;

     }

     /* Function to set right node */

     public void setRight(BSTNode n)

     {

         right = n;

     }

     /* Function to get left node */

     public BSTNode getLeft()

     {

         return left;

     }

     /* Function to get right node */

     public BSTNode getRight()

     {

         return right;

     }

     /* Function to set data to node */

     public void setData(int d)

     {

         data = d;

     }

     /* Function to get data from node */

     public int getData()

     {

         return data;

     }    

}

/* Class BST */

class BST

{

     private BSTNode root;

     /* Constructor */

     public BST()

     {

         root = null;

     }

     /* Function to check if tree is empty */

     public boolean isEmpty()

     {

         return root == null;

     }

     /* Functions to insert data */

     public void insert(int data)

     {

         root = insert(root, data);

     }

     /* Function to insert data recursively */

     private BSTNode insert(BSTNode node, int data)

     {

         if (node == null)

             node = new BSTNode(data);

         else

         {

             if (data <= node.getData())

                 node.left = insert(node.left, data);

             else

                 node.right = insert(node.right, data);

         }

         return node;

     }

     /* Functions to delete data */

     public void delete(int k)

     {

         if (isEmpty())

             System.out.println("Tree Empty");

         else if (search(k) == false)

             System.out.println("Sorry "+ k +" is not present");

         else

         {

             root = delete(root, k);

             System.out.println(k+ " deleted from the tree");

         }

     }

     private BSTNode delete(BSTNode root, int k)

     {

         BSTNode p, p2, n;

         if (root.getData() == k)

         {

             BSTNode lt, rt;

             lt = root.getLeft();

             rt = root.getRight();

             if (lt == null && rt == null)

                 return null;

             else if (lt == null)

             {

                 p = rt;

                 return p;

             }

             else if (rt == null)

             {

                 p = lt;

                 return p;

             }

             else

             {

                 p2 = rt;

                 p = rt;

                 while (p.getLeft() != null)

                     p = p.getLeft();

                 p.setLeft(lt);

                 return p2;

             }

         }

         if (k < root.getData())

         {

             n = delete(root.getLeft(), k);

             root.setLeft(n);

         }

         else

         {

             n = delete(root.getRight(), k);

             root.setRight(n);            

         }

         return root;

     }

     /* Functions to count number of nodes */

     public int countNodes()

     {

         return countNodes(root);

     }

    /* Function to count number of nodes recursively */

     private int countNodes(BSTNode r)

     {

         if (r == null)

             return 0;

         else

         {

             int l = 1;

             l += countNodes(r.getLeft());

             l += countNodes(r.getRight());

             return l;

         }

     }

     /* Functions to search for an element */

     public boolean search(int val)

     {

         return search(root, val);

     }

     /* Function to search for an element recursively */

     private boolean search(BSTNode r, int val)

     {

         boolean found = false;

         while ((r != null) && !found)

         {

             int rval = r.getData();

             if (val < rval)

                 r = r.getLeft();

             else if (val > rval)

                 r = r.getRight();

             else

             {

                 found = true;

                 break;

             }

             found = search(r, val);

         }

         return found;

     }

     /* Function for inorder traversal */

     public void inorder()

     {

         inorder(root);

     }

     private void inorder(BSTNode r)

     {

         if (r != null)

         {

             inorder(r.getLeft());

             System.out.print(r.getData() +" ");

             inorder(r.getRight());

         }

     }

     /* Function for preorder traversal */

     public void preorder()

     {

         preorder(root);

     }

     private void preorder(BSTNode r)

     {

         if (r != null)

         {

            System.out.print(r.getData() +" ");

             preorder(r.getLeft());            

             preorder(r.getRight());

         }

     }

     /* Function for postorder traversal */

     public void postorder()

     {

         postorder(root);

     }

   private void postorder(BSTNode r)

     {

         if (r != null)

         {

             postorder(r.getLeft());            

             postorder(r.getRight());

             System.out.print(r.getData() +" ");

         }

     }    

}

/* Class BinarySearchTree */

public class BinarySearchTree

{

     public static void main(String[] args)

    {                

        Scanner scan = new Scanner(System.in);

        /* Creating object of BST */

        BST bst = new BST();

        System.out.println("Binary Search Tree Test ");         

        char ch;

        /* Perform tree operations */

        do   

        {

            System.out.println(" Binary Search Tree Operations ");

            System.out.println("1. insert ");

            System.out.println("2. delete");

            System.out.println("3. search");

            System.out.println("4. count nodes");

            System.out.println("5. check empty");

            int choice = scan.nextInt();           

            switch (choice)

            {

            case 1 :

                System.out.println("Enter integer element to insert");

                bst.insert( scan.nextInt() );                    

                break;                         

            case 2 :

                System.out.println("Enter integer element to delete");

                bst.delete( scan.nextInt() );                    

                break;                        

            case 3 :

                System.out.println("Enter integer element to search");

                System.out.println("Search result : "+ bst.search( scan.nextInt() ));

                break;                                         

            case 4 :

                System.out.println("Nodes = "+ bst.countNodes());

                break;    

            case 5 :

                System.out.println("Empty status = "+ bst.isEmpty());

                break;           

            default :

                System.out.println("Wrong Entry ");

                break;  

            }

            /* Display tree */

            System.out.print(" Post order : ");

            bst.postorder();

            System.out.print(" Pre order : ");

            bst.preorder();

            System.out.print(" In order : ");

            bst.inorder();

            System.out.println(" Do you want to continue (Type y or n) ");

            ch = scan.next().charAt(0);                       

        } while (ch == 'Y'|| ch == 'y');              

    }

}