0

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.

  • The type `Object` does not have a `compareTo` method. – Sotirios Delimanolis Apr 29 '16 at 15:25
  • I was under the impression that Strings were an abstract data type and because I had specified what type of data in my insert method in my BST class, that it would be fine. Is this not the case? Thank you for your help – FueledByJordan Apr 29 '16 at 15:32
  • `theTreeKey` might hold a `String` value, but you're referring to it as an `Object`. Methods are resolved at compile time based on the static type of a reference expression. – Sotirios Delimanolis Apr 29 '16 at 15:34
  • Is there a solution where I can wrap the object as a string at this location? I have attempted to call toString() on theTreeKey, but that gave an error that I didn't fully understand. – FueledByJordan Apr 29 '16 at 15:47

0 Answers0