2017-06-04 79 views
0

虽然这个问题已经被问到,但我有一个实现的具体疑问。不能转换为java.lang.Comparable

我要打印的二叉树的俯视图,下面是完整的代码吧:

import java.util.*; 

class Node{ 
    int data; 
    Node right; 
    Node left; 
    Node(int data){ 
     this.data = data; 
    } 
} 

class Pair<F,S>{ 
    private F first; 
    private S second; 
    public Pair(F first, S second){ 
     this.first = first; 
     this.second = second; 
    } 

    public F getFirst(){return first;} 
    public S getSecond(){return second;} 
} 

class BinaryTreeTopView{ 

    public static void printTopView(Node root){ 

     if(root == null) 
      return; 

    Queue <Pair<Node,Integer>> q = new Queue<>(); 
    Map <Integer,Node> map = new HashMap<>(); 
    Pair<Node,Integer> p = new Pair<>(root, 0); 
    q.add(p); 

    /* 
    I am storing nodes and the corresponding horizontal distances 
    in the form of a pair which then are being stored in the queue 
    to ensure level order traversal 
    */ 

    while(!q.isEmpty()){ 
     Pair<Node,Integer> temp = q.peek(); 
     q.remove(); 

     if(map.containsKey(temp.getSecond())==true){ 
      map.put(temp.getSecond(),temp.getFirst()); 
     } else { 
      System.out.println(temp.getFirst().data); 
      map.put(temp.getSecond(),temp.getFirst());     
     } 

     if(temp.getFirst().left!=null){ 
      Pair<Node,Integer> left = new Pair<>(temp.getFirst().left, temp.getSecond()-1); 
      q.add(left); 
     } 

     if(temp.getFirst().right!=null){ 
      Pair<Node,Integer> right = new Pair<> (temp.getFirst().right, temp.getSecond()+1); 
      q.add(right); 
     } 

    } 
} 
public static void main(String[] args) { 
    Node root = new Node(1); 
    root.left = new Node(2); 
    root.right = new Node(3); 
    root.left.right = new Node(5); 
    root.left.left = new Node(4); 
    root.right.left = new Node(6); 
    root.right.right = new Node(7); 
    root.right.left.right = new Node(8); 
    root.right.right.left = new Node(10); 
    root.right.right.right = new Node(9); 
    root.right.right.left.right = new Node(11); 
    root.right.right.left.right.right = new Node(12); 

    printTopView(root); 
} 
} 

它编译罚款,但一个例外是在运行时被提出。 现在我已经收到以下异常,我无法找出问题所在:

Exception in thread "main" java.lang.ClassCastException: 
    Pair cannot be cast to java.lang.Comparable at java.util.PriorityQueue.siftUpComparable(PriorityQueue.java:652) 
    at java.util.PriorityQueue.siftUp(PriorityQueue.java:647) 
    at java.util.PriorityQueue.offer(PriorityQueue.java:344) 
    at java.util.PriorityQueue.add(PriorityQueue.java:321) 
+3

什么是** **完整的堆栈跟踪使用比较?即代码中的哪一行对应于? –

+1

另外,我不相信这是你真正的代码,因为'new Queue <>()'不能编译。 –

+0

@ OliverCharlesworth ..我很抱歉...但它是我的代码....我实际上发布了未经编辑的代码...当然,它不会编译... bt实际上我已经启用了PriorityQueue。 .. 所以... 反正thanx指出! – pkenil96

回答

1

这是因为对没有实现可比。无论是实现它:

public class Pair implements Comparable<Pair> { 
    public int compareTo(Pair o) { 
     // ... 
    } 
} 

或在你的优先级队列

+0

好的,工作! 非常感谢您的回答! 但我仍然不清楚为什么我需要实现可比的接口并重写compareTo方法! – pkenil96

+0

从错误看来你使用的是优先级队列,如果你看到这个类使用可比较的或比较器之间的比较两个元素 –

+0

是的,但是当我不使用它时需要使用比较器......我所有我我在做的是将我的对象存储在队列中......? – pkenil96

1

你试图Pair实例添加到PriorityQueue,所以你Pair类必须Comparable。一个合理的实现可能是迫使FSComparable自己是对的,然后由第一要素比较,然后第二个:

class Pair<F extends Comparable<F>, S extends Comparable<S>> 
    implements Comparable<Pair<F, S>> { 

    // All the code you already have is fine 

    @Override 
    public int compareTo(Pair<F, S> o) { 
     int retVal = getFirst().compareTo(o.getFirst()); 
     if (retVal != 0) { 
      return retVal; 
     } 
     return getSecond().compareTo(o.getSecond()); 
    } 
} 
+0

好吧..但我仍然有一个dout ..我的方法是不做任何比较任何地方,为什么我需要比较o.first和o.second – pkenil96

+0

'PriorityQueue'按其自然顺序排序其元素(即,它对它们进行排序)。为了做到这一点,这些要素必须具有可比性。 – Mureinik

相关问题