I am attempting to build a binary search tree to alphabetize strings.
I seem to be having an issue compiling the code.
BinarySearchTree.java:14: error: cannot find symbol
int compareResult = theTreeKey.compareTo(theRoot.NODEkey);
^
symbol: method getKey()
location: variable theTreeKey of type Object
I do not understand why this symbol cannot be found as I'm passing it in as a parameter in the method. I cannot think of a solution because the problem is not clear to me.
//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
Any insight is greatly appreciated.