我一直在将我的代码从int更改为字符串。我已经转换了所有内容但只有两行,我知道我应该能够使用compareTo来使它工作,但我不明白如何改变
if(key<insNode.getKey()){
有关如何执行此操作的任何提示?或者是比较改变这个的最好方法吗?
public class BSTcode {
public class Node {
private Node left;
private Node right;
private String key;
public Node(String key){
this.key = key;
left = null;
right = null;
}
public void setLeftLeaf(Node left){
this.left = left;
}
public void setRightLeaf(Node right){
this.right = right;
}
public Node getLeftLeaf(){
return left;
}
public Node getRightLeaf(){
return right;
}
public void setKey(String k){
this.key = k;
}
public String getKey(){
return key;
}
public void print(){
System.out.println("The number is "+ getKey());
}
}
Node root;
public Node getRoot(){
return root;
}
public BSTcode(){
root = null;
}
public void insert (String key){
Node n1 = new Node(key);
if(root == null){
root = n1;
}
else{
Node parIns = root;
Node insNode = root;
while(insNode!= null){
parIns = insNode;
if(key<insNode.getKey()){
insNode =insNode.getLeftLeaf();
}
else{
insNode = insNode.getRightLeaf();
}
}
if(key<parIns.getKey()){
parIns.setLeftLeaf(n1);
}
else{
parIns.setRightLeaf(n1);
}
}
}
public void printInorder(Node n){
if(n!= null){
printInorder(n.getLeftLeaf());
n.print();
printInorder(n.getRightLeaf());
}
}
}