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
public Node(E data) { this.data = data; this.right = null; this.left = null; }
public void setNext(Node
public void setPrevious(Node
public E getData() { return data; }
public Node
public Node
***************************************************************
TreeManager.java
package coffeetree;
public class TreeManager
private Node
int size;
TreeManager() {
size = 0; }
public void remove(Node
Node
Node
Node
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
if (size == 0) {
first = data;
} else {
Node
Node
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
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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
