2017-06-04 8 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 ...ああ、ごめんなさい...しかし、それは私のコードです...実際には編集されていないコードを投稿しました...もちろんコンパイルされません...実際にcomiledされたものはPriorityQueueでした。 .. だから... とにかくそれを指摘するための高齢者! – pkenil96

答えて

1

ペアがComparableを実装していないためです。

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

をしたり、プライオリティキュー

+0

それはうまくいった! 答えが気に入っています! しかし、私はまだ比較可能なインターフェイスを実装し、compareToメソッドをオーバーライドする必要があるのか​​どうかについてはっきりしていません! – pkenil96

+0

エラーが発生した場合は、優先クラスを使用しているようですが、このクラスで比較するとコンパレータを使用して2つの要素を比較します –

+0

ええ、コンパレータを使用する必要はありません。私のオブジェクトをキューに保管していますか? – pkenil96

1

あなたはPriorityQueuePairインスタンスを追加しようとしているので、あなたのPairクラスがComparableでなければなりません。合理的な実装では、自分の右側にComparableするFSを強制した後、最初の要素で比較し、次いで第2の1にすることができます

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

しかし、私はまだ1つのdoutを持っています。私の方法はどこにcomparsionsを作っていないので、なぜo.firstとo.secondを比較する必要があるのですか? – pkenil96

+0

'PriorityQueue'は自然な順序で要素をソートします。それをソートする)。これを達成するためには、要素は同等でなければならない。 – Mureinik

関連する問題