- #1
Smiles1
- 8
- 0
I've tackled a lot so far! I've created methods to delete, get the weight, and sort the tree by name.
My goal is to have my switch criteria change how the tree is sorted, either by weight or by name.
I haven't figured out how to implement it within my switchCriteria() method, so I've been trying to use my addDogObject() method to sort them as they are added to the tree.
When I run my program and enter to sort by weight first, all of my numbers are sorted correctly.
However, if I sort by name first, then sort by weight, all of the weights are ordered incorrectly?
This makes me think that my name sort is incorrect.
How can I reassemble each node in my tree to be sorted by name, and then by weight and vice versa?
Would I need to reset the tree to null each time and then add in the dog objects again?
Is there way to do this without adding all the objects in again and starting from scratch each time?
I'm currently looking through Big Java Late Objects but can't find a solution that makes sense with what I have currently.
App:
BSTree:
Dog class:
Any help, hints or advice is greatly appreciated!
My goal is to have my switch criteria change how the tree is sorted, either by weight or by name.
I haven't figured out how to implement it within my switchCriteria() method, so I've been trying to use my addDogObject() method to sort them as they are added to the tree.
When I run my program and enter to sort by weight first, all of my numbers are sorted correctly.
However, if I sort by name first, then sort by weight, all of the weights are ordered incorrectly?
This makes me think that my name sort is incorrect.
How can I reassemble each node in my tree to be sorted by name, and then by weight and vice versa?
Would I need to reset the tree to null each time and then add in the dog objects again?
Is there way to do this without adding all the objects in again and starting from scratch each time?
I'm currently looking through Big Java Late Objects but can't find a solution that makes sense with what I have currently.
App:
Code:
import java.util.Scanner;public class App {
public static void main(String[] args) {
BSTree dogOTree = new BSTree();
Scanner scan = new Scanner(System.in);
System.out.println("Sort by! name || weight ?");
String criteria = scan.nextLine();
dogOTree.switchSortCriteria(criteria);
Dog dogO1 = new Dog("Jackson", 65.28);
Dog dogO2 = new Dog("Tick", 69.88);
Dog dogO3 = new Dog("Nia", 75.44);
//Layla is my dog! She was named after the Eric Clapton song.
Dog dogO4 = new Dog("Layla", 32.02);
Dog dogO5 = new Dog("Bagel", 21.06);
Dog dogO6 = new Dog("Milo", 24.73);
//Populate DogOTree
dogOTree.addDogObject(dogO1);
dogOTree.addDogObject(dogO2);
dogOTree.addDogObject(dogO3);
dogOTree.addDogObject(dogO4);
dogOTree.addDogObject(dogO5);
dogOTree.addDogObject(dogO6);
dogOTree.addDogInfo("Baconater", 33.44);
dogOTree.addDogInfo("FoodBurgular", 22.32);
dogOTree.addDogInfo("SnackSnatcher", 66.92);
dogOTree.addDogInfo("ActuallyABlowHorn", 88.26);
dogOTree.addDogInfo("Clifford", 555.02);
System.out.println("Sorted by" + criteria);
dogOTree.displayInOrder();
//Testing .containsDog()
System.out.println("Exists: " + dogOTree.containsDog("Layla"));
System.out.println("Exists: " + dogOTree.containsDog("NonexistantDogO"));
//Delete successful
//dogOTree.deleteDog(dogO4);
//System.out.println("List without DogO4, Baconater");
//dogOTree.displayInOrder();
//getDogWeight() successful
System.out.println(dogOTree.getDogWeight("Layla"));
System.out.println(dogOTree.getDogWeight("Nia"));
System.out.println(dogOTree.getDogWeight("Clifford"));
System.out.println(dogOTree.getDogWeight("Jackson"));
System.out.println(dogOTree.getDogWeight("Non-existantDogO"));
System.out.println("Sort by! name || weight ?");
criteria = scan.nextLine();
dogOTree.switchSortCriteria(criteria);
dogOTree.displayInOrder();
//getDogWeight() successful
System.out.println(dogOTree.getDogWeight("Layla"));
System.out.println(dogOTree.getDogWeight("Nia"));
System.out.println(dogOTree.getDogWeight("Clifford"));
System.out.println(dogOTree.getDogWeight("Jackson"));
System.out.println(dogOTree.getDogWeight("Non-existantDogO"));
}
}
BSTree:
Code:
public class BSTree {
class Node {
private Dog data;
private Node leftChild;
private Node rightChild;
Node(Dog data) {
this.data = data;
this.leftChild = null;
this.rightChild = null;
}
}
private Node root;
private Node tempNode;
private String criteria;
public BSTree() {
root = null;
tempNode = null;
criteria = "";
}
public boolean addDogObject(Dog data) {
if(criteria.equalsIgnoreCase("weight")) {
root = addRecursiveByWeight(root, data);
}
else if(criteria.equalsIgnoreCase("name")){
root = addRecursiveByName(root, data);
}
return true;
}
//Adds a Dog object to the tree. Returns true on success or false on failure (if Dog with that name already exists).
public boolean addDogInfo(String name, double weight) {
return this.addDogObject(new Dog(name, weight));
}
private Node addRecursiveByWeight(Node current, Dog data) {
if (current == null) { // If it's root
return new Node(data);
} else if (data.getWeight() < current.data.getWeight()) { //navigate left
//Vrrooom vrroom go left
current.leftChild = addRecursiveByWeight(current.leftChild, data);
return current;
} else if (data.getWeight() > current.data.getWeight()) {
current.rightChild = addRecursiveByWeight(current.rightChild, data);
return current;
} else {
return current;
}
}
private Node addRecursiveByName(Node current, Dog data) {
if (current == null) {
return new Node(data);
} else if (data.getName().compareToIgnoreCase(current.data.getName()) < 0) { //navigate left
//Vrrooom vrroom go left
current.leftChild = addRecursiveByWeight(current.leftChild, data);
return current;
} else if (data.getName().compareToIgnoreCase(current.data.getName()) > 0) {
current.rightChild = addRecursiveByWeight(current.rightChild, data);
return current;
} else {
return current;
}
}
public void switchSortCriteria(String userCriteria) {
//Specifies whether your tree is sorted by the dog's name or weight. If the tree contains elements already,
//it will re-order those elements using the criteria specified. The criteria parameter may be equal to name or weight.
// if(criteria.equalsIgnoreCase("weight")){
// ///Silently screaming
//
// }
// else if(criteria.equalsIgnoreCase("name")){
// //Also silently screaming
// }
criteria = userCriteria;
}
public boolean containsDog(String name) {
return containsRecursive(root, name);
//Start at the root and looks for data
}
private boolean containsRecursive(Node current, String name) {
if (current == null) {
return false;
}
if (name.compareToIgnoreCase(current.data.getName()) == 0) {
tempNode = current;
return true;
}
boolean inLeft = containsRecursive(current.leftChild, name);
if (!inLeft) {
return (containsRecursive(current.rightChild, name));
}
return true;
} /*
While node is not null
recursively traverse left child
Display node's value
recursively traverse right child
*/
public void displayInOrder() {
inOrderTraversal(root);
}
private void inOrderTraversal(Node node) {
if (node != null) {
inOrderTraversal(node.leftChild);
System.out.println(" " + node.data);
inOrderTraversal(node.rightChild);
}
}
public double getDogWeight(String name) {
//Returns the weight of the dog with the specified name if it exists in the tree, or returns -1 otherwise.
if (containsRecursive(root, name) == true) {
return tempNode.data.getWeight();
} else {
return -1;
}
}
public void deleteDog(Dog data) {
root = deleteRecursive(root, data);
}
private Node deleteRecursive(Node current, Dog data) {
if (current == null) {
return current;
}
if (data.getWeight() < current.data.getWeight()) {
current.leftChild = deleteRecursive(current.leftChild, data);
} else if (data.getWeight() > current.data.getWeight()) {
current.rightChild = deleteRecursive(current.rightChild, data);
} else {
if (current.leftChild == null) {
return current.rightChild;
} else if (current.rightChild == null) {
return current.leftChild;
}
current.data.setWeight(findMinVal(current.rightChild));
current.rightChild = deleteRecursive(current.rightChild, current.data);
}
return current;
}
public double findMinVal(Node current) {
double min = current.data.getWeight();
while (current.leftChild != null) {
min = current.leftChild.data.getWeight();
current = current.leftChild;
}
return min;
}
}
Dog class:
Code:
public class Dog {
private String name;
private double weight;
public Dog(String name, double weight) {
this.name = name;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Dog {" + " name= " + name + ", weight= " + weight;
}
}
Any help, hints or advice is greatly appreciated!