Heads up, my code is 100% functional it was working on Tuesday. I open my java code today given I have not touched it since Tuesday, in VScode only for it to have red squiggly lines under everything.
Errors such as the "The import java.util cannot be resolved", "String cannot be resolved to a type", "Object cannot be resolved to a type". I ran the Debugging Tool the output was:
"Error: Unable to initialize main class DSABinarySearchTree Caused by: java.lang.NoClassDefFoundError: [LString;"
I have checked my Java version, it is 19.0.2. I have checked my Javac version it is 19.0.2.
I ran the code within terminal compiled with javac and then ran with java. The code is fully functional and working.
I have tried restarting the editor and my computer.
I have C and Python programs, they all work fine. None of my Java programs are actually compiling and running within VSCode.
The problem seems to lie within VSCode.
Note: This was all working on Tuesday!
Below I have included proof of error and list of some errors. I am actually not sure what is happening.
Here is the code (It is fully working!):
//Outter public class DSABinaryTree
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class DSABinarySearchTree
{
//Inner private class DSATreeNode
private class DSATreeNode
{
private String m_key; //Unique identifier (String or int), that is used to disinguish nodes
private Object m_value; //Value of the node
private DSATreeNode m_leftChild; //Where the left child has is less than the right child
private DSATreeNode m_rightChild;
public DSATreeNode(String inKey, Object inVal)
{
if (inKey == null)
{
throw new IllegalArgumentException("Key cannot be null.");
}
m_key = inKey;
m_value = inVal;
m_leftChild = null;
m_rightChild = null;
}
//Accessors:
public String getKey() {return m_key;}
public Object getValue() {return m_value;}
public DSATreeNode getLeft(){return m_leftChild;}
public DSATreeNode getRight(){return m_rightChild;}
//Mutators:
public void setLeft(DSATreeNode newLeft)
{
m_leftChild = newLeft;
}
public void setRight(DSATreeNode newRight)
{
m_rightChild = newRight;
}
} //Outside of private inner class DSATreeNode
//Back within outter public class DSABinaryTree
private DSATreeNode m_root;
public DSABinarySearchTree()
{
m_root = null;
}
//Accessor
public Object find(String key)
{
return findRec(key, m_root); //calls the recursive function (Acts as a wrapper), passes a key to find
}
//Private recusrive accessor
private Object findRec(String key, DSATreeNode currNode)
{
Object value = null; //Value to find
//Start at root node
if (currNode == null) //
{
throw new NoSuchElementException("Key " + key + " not found."); //If no child exists (Null), throw an exception
}
else if (key.equals(currNode.getKey())) //If the keys match, grab value
{
value = currNode.getValue();
}
else if (key.compareTo(currNode.getKey()) < 0) //If key to find is less than node key, go to the left child
{
value = findRec(key, currNode.getLeft()); //Call recursively to begin the process again, until a matching value is found
}
else //If the key to find is greater than node key, then go to right child
{
value = findRec(key, currNode.getRight()); //Another recursive call
}
return value;
}
public void insert(String key, Object data) //Wrapper function
{
m_root = insertRec(key, data, m_root); //Pass in the key to insert, the data/value to insert and the root
}
private DSATreeNode insertRec(String key, Object data, DSATreeNode currNode)
{
DSATreeNode updateNode = currNode; //store the current node in a tmp variable
if(currNode == null) // If the current node is null (Nothing in the tree), insert the node
{
//System.out.println("A");
DSATreeNode newNode = new DSATreeNode(key, data); //Create a new node to insert
updateNode = newNode; //new node will be returned
}
else if (key.equals(currNode.getKey())) //If the key matches a key within the tree, do nothing
{
//System.out.println("B");
}
else if (key.compareTo(currNode.getKey()) < 0) //Compare key to insert with the current key
{
//System.out.println("C");
currNode.setLeft(insertRec(key, data, currNode.getLeft())); //If the compareTo returns a -ve then key is less than current key
}
else
{
//System.out.println("D");
currNode.setRight(insertRec(key, data, currNode.getRight()));
}
//System.out.println(updateNode.getKey());
return updateNode;
}
public DSATreeNode delete(String key) //Wrapper function
{
return deleteRec(key, m_root);
}
private DSATreeNode deleteRec(String key, DSATreeNode currNode)
{
DSATreeNode updateNode = currNode;
if (currNode == null)
{/* abort */}
else if (key.equals(currNode.getKey()))
{
updateNode = deleteNode(key, currNode);
}
else if (key.compareTo(currNode.getKey()) < 0)
{
currNode.setLeft(deleteRec(key, currNode.getLeft()));
}
else
{
currNode.setRight(deleteRec(key, currNode.getRight()));
}
return updateNode;
}
private DSATreeNode deleteNode(String key, DSATreeNode delNode)
{
DSATreeNode updateNode = null;
if ((delNode.getLeft() == null) && (delNode.getRight() == null)) //Case: No childs
{
updateNode = null;
}
else if ((delNode.getLeft() != null) && (delNode.getRight() == null)) //Case: No right child
{
updateNode = delNode.getLeft();
}
else if ((delNode.getLeft() == null) && (delNode.getRight() != null)) //Case: No left child
{
updateNode = delNode.getRight();
}
else //Case: 2 childs
{
System.out.println("I am in the case: 2 childs");
updateNode = promoteSuccessor(delNode.getRight());
if (updateNode != delNode.getRight())
{
updateNode.setRight(delNode.getRight());
}
updateNode.setLeft(delNode.getLeft());
}
return updateNode;
}
private DSATreeNode promoteSuccessor(DSATreeNode curr)
{
DSATreeNode successor = curr;
if (curr.getLeft() != null)
{
successor = promoteSuccessor(curr.getLeft());
if (successor == curr.getLeft())
{
curr.setLeft(successor.getRight());
}
}
System.out.println(successor.getKey());
return successor;
}
public void preOrder()
{
preOrderRec(m_root);
// list.displayListForwards();
}
private void preOrderRec(DSATreeNode currNode)
{
if (currNode != null)
{
System.out.println(currNode.getKey() + " ");
preOrderRec(currNode.getLeft());
preOrderRec(currNode.getRight());
}
}
public void inOrder()
{
inOrderRec(m_root);
// list.displayListForwards();
}
private void inOrderRec(DSATreeNode currNode)
{
if (currNode != null)
{
inOrderRec(currNode.getLeft());
System.out.println(currNode.getKey() + " ");
inOrderRec(currNode.getRight());
}
}
public void postOrder()
{
postOrderRec(m_root);
// list.displayListForwards();
}
private void postOrderRec(DSATreeNode currNode)
{
if (currNode != null)
{
postOrderRec(currNode.getLeft());
postOrderRec(currNode.getRight());
System.out.println(currNode.getKey() + " ");
}
}
public void min()
{
DSATreeNode minNode = minRec(m_root);
System.out.println("Key: "+minNode.getKey() +", "+"Node: "+ minNode.getValue());
}
private DSATreeNode minRec(DSATreeNode currNode)
{
DSATreeNode minNode = currNode;
if(currNode.getLeft() != null)
{
minNode = minRec(minNode.getLeft());
}
return minNode;
}
public void max()
{
DSATreeNode maxNode = maxRec(m_root);
System.out.println("Key: "+maxNode.getKey() +", "+"Node: "+ maxNode.getValue());
}
private DSATreeNode maxRec(DSATreeNode currNode)
{
DSATreeNode maxNode = currNode;
if(currNode.getLeft() != null)
{
maxNode = minRec(maxNode.getRight());
}
return maxNode;
} //
public int height()
{
return heightRec(m_root);
}
private int heightRec(DSATreeNode currNode)
{
int htSoFar, iLeftHt, iRightHt;
if(currNode == null)
{
htSoFar = -1;
}
else
{
iLeftHt = heightRec(currNode.getLeft());
iRightHt = heightRec(currNode.getRight());
if(iLeftHt > iRightHt)
{
htSoFar = iLeftHt + 1;
}
else
{
htSoFar = iRightHt + 1;
}
}
return htSoFar;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int selection = -1;
String key;
int value;
DSABinarySearchTree tree = new DSABinarySearchTree();
while(selection != 0)
{
try{
System.out.println("-----------------------------");
System.out.println("What would you like to do?");
System.out.println("[1] Add node");
System.out.println("[2] Delete node");
System.out.println("[3] Find node");
System.out.println("[4] Min, Max & Height");
System.out.println("[5] Display the tree");
System.out.println("[0] Exit Program");
System.out.println("-----------------------------");
selection = sc.nextInt();
switch(selection)
{
case 1:
sc.nextLine();
System.out.println("Enter a key (String Value): ");
key = sc.nextLine();
System.out.println("Enter a value (integer value)");
value = sc.nextInt();
tree.insert(key, value);
break;
case 2:
sc.nextLine();
System.out.println("Enter a key to delete (String value): ");
key = sc.nextLine();
tree.delete(key);
break;
case 3:
try{
sc.nextLine();
System.out.println("Enter a key to find (String value): ");
key = sc.nextLine();
System.out.println("The value for Key = " + key + " is " + tree.find(key));
}
catch(NoSuchElementException e)
{
System.out.println("This key does not exist!");
}
break;
case 4:
try{
System.out.println("Min: ");
tree.min();
System.out.println("Max: ");
tree.max();
System.out.println("Height: " + tree.height());
}
catch(NullPointerException e){
System.out.println("Error: Null pointer exception, nothing in tree");
}
break;
case 5:
sc.nextLine();
System.out.println("Choose display type: ");
System.out.println("[1] Pre-order");
System.out.println("[2] In-order");
System.out.println("[3] Post-order");
int x = sc.nextInt();
if(x == 1)
{
tree.preOrder();
}
else if (x == 2)
{
tree.inOrder();
}
else if (x == 3)
{
tree.postOrder();
}
else
{
System.out.println("Invalid option.");
}
break;
case 0:
System.out.println("Good-bye!");
break;
default:
System.out.println("Invalid input.");
break;
}
}
catch (InputMismatchException e)
{
System.out.println("Error: Incorrect data type entry.");
sc.nextLine();
}
}
}
/*
public static void main(String[] args)
{
DSABinarySearchTree tree = new DSABinarySearchTree();
tree.insert("n", 5);
tree.insert("l", 7);
tree.insert("j", 3);
tree.insert("q", 3);
tree.insert("p", 3);
tree.insert("m", 3);
tree.insert("k", 3);
tree.insert("o", 3);
tree.insert("r", 3);
tree.min();
tree.max();
tree.inOrder();
System.out.println(tree.height());
//tree.preOrder();
//tree.postOrder();
//Debugging
/*
if(tree.m_root != null)
{
tree.m_root.getValue();
if(tree.m_root.getLeft() != null)
{
tree.m_root.getLeft().getValue();
}
else{
System.out.println("Left child is null");
}
if(tree.m_root.getRight() != null)
{
tree.m_root.getRight().getValue();
System.out.println("Hello");
}
else{
System.out.println("Right child is null");
}
}
else{
System.out.println("Root is null");
}
}
*/
}
Fix: Intellisense and Intellicode happened to require a reload just shortly after this post. Just needed to wait for the detection.