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

Objective: In java write this and make it your own original code, as I know this

ID: 3863192 • Letter: O

Question

Objective: In java write this and make it your own original code, as I know this is on chegg already.

Write a program in JAVA which creates a binary search tree of different fruit from a file.

·     The comparison is based on the fruit’s weight

·     The file is tab delimited format goes as follows

o Fruit Type weight

·     The binary search tree needs to have the following methods

o insert: inserts a new fruit into the tree

o delete: deletes the fruit instance. Keep in mind that in order for a fruit to be equal it must have the same same type and weight.

o print pre-order traversal: Print the data. Next explore the left subtree. Finally right explore subtree.

o print in-order traversal: Explore the left subtree. Print the data. Finally explore the right subtree.

o print post-order traversal: Explore the left subtree. Then explore the right subtree. Finally print out the data.

·     Finally write a test file that demonstrates THE POWER OF TREES!!! FRUIT!!! By reading a shape file.

Explanation / Answer

//============================ BST.java ================================//

public class BST {
    private BSTNode root;

    public BST(){
        root = null;
    }
  
    public void insert(Fruit data){
        root = insert(root, data);
    }

    private BSTNode insert(BSTNode node, Fruit data){
        if (node == null)
            node = new BSTNode(data);
        else {
            if (data.getWeight() <= node.getData().getWeight())
                node.left = insert(node.left, data);
            else
                node.right = insert(node.right, data);
        }
        return node;
    }

    public void delete(Fruit 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, Fruit k) {
        BSTNode p, p2, n;
        if (root.getData().getWeight() == k.getWeight()){
            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.getWeight() < root.getData().getWeight())
        {
            n = delete(root.getLeft(), k);
            root.setLeft(n);
        }
        else{
            n = delete(root.getRight(), k);
            root.setRight(n);           
        }
        return root;
    }

    public int countNodes(){
        return countNodes(root);
    }

    private int countNodes(BSTNode r) {
        if (r == null)
            return 0;
        else{
            int l = 1;
            l += countNodes(r.getLeft());
            l += countNodes(r.getRight());
            return l;
        }
    }
  
    public boolean search(Fruit val){
        return search(root, val);
    }

    private boolean search(BSTNode r, Fruit val){
        boolean found = false;
        while ((r != null) && !found){
           Fruit rval = r.getData();
            if (val.getWeight() < rval.getWeight())
                r = r.getLeft();
            else if (val.getWeight() > rval.getWeight())
                r = r.getRight();
            else
            {
                found = true;
                break;
            }
            found = search(r, val);
        }
        return found;
    }

    public void printInOrder(){
       printInOrder(root);
    }
  
    private void printInOrder(BSTNode r){
        if (r != null){
           printInOrder(r.getLeft());
            System.out.print(r.getData() +" ");
            printInOrder(r.getRight());
        }
    }

    public void printPreOrder(){
       printPreOrder(root);
    }
  
    private void printPreOrder(BSTNode r){
        if (r != null){
            System.out.print(r.getData() +" ");
            printPreOrder(r.getLeft());           
            printPreOrder(r.getRight());
        }
    }

    public void printPostOrder(){
       printPostOrder(root);
    }

    private void printPostOrder(BSTNode r){
        if (r != null) {
           printPostOrder(r.getLeft());           
           printPostOrder(r.getRight());
            System.out.print(r.getData() +" ");
        }
    }
    public boolean isEmpty(){
        return root == null;
    }
}

//============================ BSTNode.java ================================//

public class BSTNode{
    BSTNode left;
    BSTNode right;
    Fruit data;
    public BSTNode(){
        left = null;
        right = null;
        data = null;
    }
    public BSTNode(Fruit n){
        left = null;
        right = null;
        data = n;
    }
    public void setLeft(BSTNode n){
        left = n;
    }

    public void setRight(BSTNode n){
        right = n;
    }
    public BSTNode getLeft(){
        return left;
    }

    public BSTNode getRight(){
        return right;
    }
    public void setData(Fruit d){
        data = d;
    }
    public Fruit getData(){
        return data;
    }
    public String toString(){
       return "[ "+data.getType()+" "+data.getWeight()+" ]";
    }
}

//============================= Fruit.java=====================================//

public class Fruit {
   private double weight;
   private String type;
  
   public Fruit(String t,double w) {
       setWeight(w);
       setType(t);
   }

   public double getWeight() {
       return weight;
   }

   public void setWeight(double weight) {
       this.weight = weight;
   }

   public String getType() {
       return type;
   }

   public void setType(String type) {
       this.type = type;
   }
   public String toString(){
       return "["+getType()+" "+getWeight()+"]";
    }
}

//=================================== FruitDriver.java ==================================//

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class FruitDriver {

   public static void main(String[] args) {
       Scanner sc =null;
       BST bst = new BST();
       try{
           sc = new Scanner(new File("input.txt"));
       }catch(FileNotFoundException e){
           e.printStackTrace();
       }
       while(sc.hasNext()){
           String type = sc.next();
           double weight = sc.nextDouble();
           bst.insert(new Fruit(type, weight));
       }
       System.out.println(" InOrder :");
       bst.printInOrder();
       System.out.println(" PostOrder :");
       bst.printPostOrder();
       System.out.println(" PreOrder :");
       bst.printPreOrder();
   }
}

//======================== input.txt =========================//

Apple   30
Mango   10
Graps   70
Papaya   50

//============================= OUTPUT ==============================//
InOrder :
[Mango 10.0] [Apple 30.0] [Papaya 50.0] [Graps 70.0]
PostOrder :
[Mango 10.0] [Papaya 50.0] [Graps 70.0] [Apple 30.0]
PreOrder :
[Apple 30.0] [Mango 10.0] [Graps 70.0] [Papaya 50.0]