2017-06-04 34 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

相關問題