his.leftNode = leftNode;
}
}
public class SearchTree {
private Node root;
public Node getRoot() {
return root;
}
public void setRoot(Node root) {
this.root = root;
}
public char firstSameva lue(String str) {
if (str == null||str.trim().length()==0) {
return ‘ ‘;
} else if (str.length()==1){
return str.toCharArray()[0];
}else {
char charNodes[] = str.toCharArray();
root = new Node(charNodes[0]);
for (int i = 1; i < charNodes.length; i++) {
Node node = new Node(charNodes[i]);
if (!insertNode(root,node)){
return node.getValue();
}
}
return ‘ ‘;
}
}
//如果树中插入失败,表示树中已经存在这个节点了
public boolean insertNode(Node root, Node node) {
if (root.getValue() == node.getValue()) {
return false;
} else if (root.getValue() < node.getValue()) {
if (root.getRightNode() != null) {
if (!insertNode(root.getRightNode(), node)){
return false;
}
} else {
root.setRightNode(node);
}
} else {
if (root.getLeftNode() != null) {
if(!insertNode(root.getLeftNode(), node)){
return false;
}
} else {
root.setLeftNode(node);
}
}
return true;
}
public static void main(String[] args) {
String str=”aliresaearch.com”;
SearchTree tree=new SearchTree();
System.out.println(tree.firstSameva lue(str));
}
}