I'm trying to make a binary tree that does not accept duplicates ironically I've been able to do the opposite:
public void insertLeaf(String a){
Node newNode = new Node(a);
if(node==null){
node=newNode;
return;
}
Node currentValue = node;
Node parent = null;
while(true){
parent = currentValue;
if(a.compareTo(currentValue.data)<0){
currentValue = currentValue.left;
if(currentValue==null ){
parent.left = newNode;
return;
}
}else{
currentValue = currentValue.right;
if(currentValue==null){
parent.right = newNode;
return;
}
}
}
}
Heres the Node class
class Node{
String data;
Node left;
Node right;
public Node(String data){
this.data = data;
left = null;
right = null;
}
}
Thanks for your help.