我正在尝试构建一个二进制搜索树来按字母顺序排列字符串。
我似乎在编译代码时遇到了问题。
BinarySearchTree.java:14: error: cannot find symbol
int compareResult = theTreeKey.compareTo(theRoot.NODEkey);
^
symbol: method getKey()
location: variable theTreeKey of type Object
我不明白为什么无法找到此符号,因为我将其作为方法中的参数传递。我想不出解决办法,因为我不清楚这个问题。
//Start of BST Class
public class BST {
TreeCell<String> root;
public BST() {
root = null;
}
public void insert(String string) {
root = insert(string, root);
}
private static TreeCell<String> insert(String string, TreeCell<String> node) {
if (node == null)
return new TreeCell<String>(string);
int compare = string.compareTo(node.datum);
if (compare < 0)
node.left = insert(string, node.left);
else if (compare > 0)
node.right = insert(string, node.right);
return node;
}
}//End of BST Class
//Start of BinaryTree Class
import java.lang.String;
class BinaryTree
{
Node TREEroot;
public void insert(Object treeKey){
TREEroot=insertNewNode(TREEroot,treeKey);
}
public Node insertNewNode(Node theRoot,Object theTreeKey){
if(theRoot==null)
return new Node(theTreeKey);
int compareResult = theTreeKey.compareTo(theRoot.NODEkey);
if(compareResult < 0)
theRoot.NodeLeftChild=insertNewNode(theRoot.NodeLeftChild,theTreeKey);
else if(compareResult > 0)
theRoot.NodeRightChild=insertNewNode(theRoot.NodeRightChild,theTreeKey);
else if(compareResult == 0)
theRoot.count=theRoot.count++;
return theRoot;
}
}//End of BinaryTree class
//Start of Node class
import java.io.*;
import java.util.*;
class Node{
Object NODEkey;
int count;
Node NodeLeftChild;
Node NodeRightChild;
Node(Object key){
NODEkey=key;
count=1;
}
public Node getNodeLeftChild(){
return this.NodeLeftChild;
}
public Node getNodeRightChild(){
return this.NodeRightChild;
}
public Object getKey(){
return this.NODEkey;
}
}//end of Node class
非常感谢任何见解。