I want my output to come out as follows:
(E: HAR@litfried L: R: (E: HAR@tika L: (E: HAR@LynConway L: R: ) R: ) ). I've tried everything possible but I keep getting this instead. (E: HAR@litfried L: R: (E: HAR@tika L: R: (E: HAR@LynConway L: R: ) ) ).
Here is my code:
public class BST {
private BSTNode root;
public BST(){
root = null;
}
public void insertColleague(Colleague c) {
BSTNode node = new BSTNode(c);
if (root == null){
root = node;
}else {
insertColleague(root, node);
}
}
private void insertColleague(BSTNode currNode, BSTNode node) {
if(currNode.getC().getUserName().compareTo(root.getC().getUserName())<0){
if (currNode.getL() == null){
currNode.setL(node);
} else {
insertColleague(currNode.getL(), node);
}
} else{
if (currNode.getR() == null){
currNode.setR(node);
}else {
insertColleague(currNode.getR(), node);
}
}
}
public void printInOrder() {
printInOrderRecursive(root);
//System.out.print("L: ");
//System.out.println("R: <null>)");
}
private void printInOrderRecursive(BSTNode current) {
if (current == null) {
System.out.print("<null>");
return;
}
System.out.print("(E: " + current.getC().getUserName() + " ");
System.out.print("L: ");
printInOrderRecursive(current.getL());
System.out.print(" R: ");
printInOrderRecursive(current.getR());
System.out.print(")");
}
}
To achieve the desired output, you should modify the insertColleague method to compare the usernames in a case-insensitive manner. Instead of using compareTo, use compareToIgnoreCase when comparing the usernames.
if(currNode.getC().getUserName().compareToIgnoreCase(root.getC().getUserName()) < 0)
You also need to update the printInOrder method to print the output in the desired format. Print the result in the order of (E: username L: (left subtree) R: (right subtree)) for each node in the tree.