0

I am trying to implement a Binary Tree class.Here's the insert method.When I compile it I get an error as "uses unchecked or unsafe operations.Recompile with -Xlint:unchecked for details".
Can some on please tell me how I can correct this?

public class BinaryNode{
    Comparable element;
    BinaryNode left;
    BinaryNode right;
    BinaryNode(Comparable theElement,BinaryNode lt,BinaryNode rt){
        element=theElement;
        left=lt;
        right=rt;       
    }
    BinaryNode(Comparable theElement){
        this(theElement,null,null);
    }

}  
public class BinaryTr  {
    private BinaryNode root;
    BinaryTr(){
        root=null;
    }
    public void insert(Comparable x){
        root=insert(root,x);
    }
    public BinaryNode insert(BinaryNode current,Comparable x){
        if (current==null)
            current=new BinaryNode(x);
        else{
            if(current.element.compareTo(x)>0)
                current.left=insert(current.left,x);
        else if (current.element.compareTo(x)<0)
            current.right=insert(current.right,x);
        else{
            System.out.println("Duplicates not allowed");
        }
    }
    return current;
}

public static void main (String args[]){
    BinaryTr t=new BinaryTr();
    t.insert(5);
            t.insert(4);
            t.insert(5);
    }
}
Durgesh Chaudhary
  • 1,075
  • 2
  • 12
  • 31
sam_rox
  • 739
  • 3
  • 14
  • 29
  • 4
    That's...not an error. That's just a warning. Recompile with `-Xlint:unchecked` to see what the warning is telling you. It may be important. – Makoto Jun 04 '14 at 05:45
  • 3
    Did you follow the instructions the compiler explicitly gave you? Try that, and if you're still stumped, come back here. Not before. – torquestomp Jun 04 '14 at 05:45
  • use generic will solve this – Fabricator Jun 04 '14 at 05:47
  • @Makoto I read the other question.I don't know where to use generics.If I specify as would I be able to compare only String.So then wouldn't I be able to insert int. – sam_rox Jun 04 '14 at 05:54
  • *That's the point.* You're bounding the type to a `String`. You can't just arbitrarily enter in any old type to it, or you're going to have a bad time. – Makoto Jun 04 '14 at 05:56
  • @Makoto Then if I can only use the tree for a specific type, either int or String without using Compare I could have used just`int element` and not use `CompareTo` method.What's the purpose of using Comparable.Also can you please tell me where I should use the generic.Because I am not familiar with it – sam_rox Jun 04 '14 at 06:06
  • 1
    Such a conversation would be too long for this format, but I would encourage you to read up on generics; the Java Trails provides a ton of useful information for beginners. (By the way: I would imagine that your type parameter is defined as `>`, since you *are* doing `compareTo`. – Makoto Jun 04 '14 at 06:07
  • @Makoto No I haven't used `>`.What's the type parameter,Is it the parameter inside `compareTo()`.Then I am giving a parameter of type `Comparable` – sam_rox Jun 04 '14 at 06:17

0 Answers0