Question: I need help with this java project. I have almost completed it. The only issues are that the remove method does not work correctly in

I need help with this java project. I have almost completed it. The only issues are that the remove method does not work correctly in the TreeManager class, and nodes still get added to the tree even if they are equal to a current node (that should not happen).

Here is the assignment:

Part 1:

1. Design and implement (write the code) a class for the node in your tree, this Node will need to be setup for Generics.

-This class will need to have references to both it's children.

-It will need set and get methods to retieve and set left and right child nodes.

-Methods to set and get the data

2. Design and begin to implement the class to hold the data (similar to song class in the linked list example I gave you.)

-The data for this tree will be a Coffee class.

-The Coffee class will have four instance fields: Price, Color (dark, medium, light), Company/Distributer

-The Coffee class will need to be comparable based on info that is listed in the outlab section below. You can leave this method blank for inlab.

-It will also need a toString method.

-One data member (or attribute) in this class should be a reference variable to point to the root.

Part 2:

1. Implement the TreeManager class you designed. Make sure you have the following:

-You will need to implement an add method to add nodes to the tree by the following rules

--You will compare the coffee instances by price,

--if price is the same you will go to Distributor name.

--if same price and distributor then you will decipher by color......

--if all are the same do not duplicate it in the tree, do not add.

-Three methods for inOrder, preOrder and postOrder traversal of the trees like I went over last Week.

-Next implement a delete method. User will need to give a price/color/company combination. If the data is found in the ree then remove it from the tree. Make sure you use the rules of the psuedo code I gave in class.

2. Write a driver to call your methods, add nodes, traverse tree and delete nodes

3. Your completed project should have four classes

-Driver

-TreeManager

-Node

-Coffee

4. You must be able to add nodes properly, using the comparable interface. Traverse with the three traversals and then delete a node that doesn't break the traversals.

Below is the code that I have so far. Please use this to complete the assignment:

***************************************************************

Coffee.java

package coffeetree;

public class Coffee implements Comparable {

private double price; private String color; private String distributer;

Coffee(double price, String color, String distributer) { this.price = price; this.color = color; this.distributer = distributer; }

public int compareTo(Object coffee) { Coffee newCoffee = (Coffee) coffee; System.out.println("Compare to coffee " + price + " and " + newCoffee.price); if (price < newCoffee.price) { return -1; } else if (price == newCoffee.price) { if (distributer.compareTo(newCoffee.distributer) < 0) { return -1; } else if (distributer.compareTo(newCoffee.distributer) == 0) { if (color != newCoffee.color) { return -1; } else if (color == newCoffee.color) { return 0; } else { return 1; } } else { return 1; } } else { return 1; } }

public String toString() { return price + ", " + distributer + ", " + color + ", "; } }

***************************************************************

Node.java

package coffeetree;

public class Node {

public E data; public Node right; public Node left;

public Node(E data) { this.data = data; this.right = null; this.left = null; }

public void setNext(Node what) { this.right = what; }

public void setPrevious(Node what) { this.left = what; }

public E getData() { return data; }

public Node getNext() { return right; }

public Node getPrevious() { return left; } }

***************************************************************

TreeManager.java

package coffeetree;

public class TreeManager {

private Node first;

int size;

TreeManager() {

size = 0; }

public void remove(Node data) {

Node delete = first.getNext();

Node before;

Node after;

while ((data.getData().compareTo(delete.getData()) != 0) && (delete != first)) {

delete = delete.getNext();

}

if (delete != first) {

before = delete.getPrevious();

after = delete.getNext();

before.setNext(after);

after.setPrevious(before);

}

}

public void add(Node data) {

if (size == 0) {

first = data;

} else {

Node temp = first;

Node before = null;

int cnt = 0;

while (temp != null) {

if (data.getData().compareTo(temp.getData()) < 0) {

cnt = 1;//left

before = temp;

temp = temp.getPrevious();

} else if (data.getData().compareTo(temp.getData()) > 0) {

cnt = 2;//right

before = temp;

temp = temp.getNext();

} else { break; }

}

if (temp == null) {

if (cnt == 1) {

before.setPrevious(data);

} else {

before.setNext(data);

}

}

}

size++;

}

void inOrder(Node node) {

if (node == null) { return; }

inOrder(node.left);

System.out.print(node.data + " ");

inOrder(node.right);

}

void preOrder(Node node) {

if (node == null) { return; }

System.out.print(node.data + " ");

preOrder(node.left);

preOrder(node.right);

}

void postOrder(Node node) {

if (node == null) { return; }

postOrder(node.left);

postOrder(node.right);

System.out.print(node.data + " ");

}

Node getFirst() {

return first;

}

}

***************************************************************

Driver.java

package coffeetree;

import java.util.*;

public class Driver {

public static void main(String args[]) { int ch; Double input1; String input2; String input3; TreeManager list = new TreeManager(); Coffee coffee; Node node; do { System.out.println(" Coffee Tree Options"); System.out.println("1. Add Coffee"); System.out.println("2. Remove Coffee"); System.out.println("3. Traverse In-Order"); System.out.println("4. Traverse Pre-Order"); System.out.println("5. Traverse Post-Order"); System.out.println("6. Exit"); System.out.println("Enter your choice:"); Scanner sc = new Scanner(System.in); ch = sc.nextInt(); switch (ch) { case 1: System.out.println("Enter price of coffee"); input1 = sc.nextDouble(); System.out.println("Enter color of coffee"); input2 = sc.nextLine(); input2 = sc.nextLine(); System.out.println("Enter distributor of coffee"); input3 = sc.nextLine(); coffee = new Coffee(input1, input2, input3); node = new Node(coffee); list.add(node); break; case 2: System.out.println("Enter price of coffee to be deleted"); input1 = sc.nextDouble(); System.out.println("Enter color of coffee to be deleted"); input2 = sc.nextLine(); input2 = sc.nextLine(); System.out.println("Enter distributor of coffee to be deleted"); input3 = sc.nextLine(); coffee = new Coffee(input1, input2, input3); node = new Node(coffee); list.remove(node); break; case 3: System.out.println(" In-Order Traversing "); list.inOrder(list.getFirst()); break; case 4: System.out.println(" Pre-Order Traversing "); list.preOrder(list.getFirst()); break; case 5: System.out.println(" Post-Order Traversing "); list.postOrder(list.getFirst()); break; case 6: System.exit(0); break; default: System.out.println("Invalid entry "); } } while (ch != 6); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!