【问题标题】:compareTo with generics for heapSortcompareTo 与 heapSort 的泛型
【发布时间】:2013-12-03 16:35:24
【问题描述】:

对于类,我必须实现 BST 或 heapSort。我做了 BST,但认为知道这一点也很好,但现在我被困住了。这是我第一次使用堆(并且真正使用泛型/实现 Comparable 进行编码,所以我为所有错误道歉)并且我遇到了实现 compareTo 的问题。

基本上我希望能够将通用对象添加到我的堆数组中,然后比较它们以进行堆排序。在添加到堆和交换 reheap 方法时,我使用 compareTo 检查新条目。

我的错误返回:

Heap.java:64: error: bad operand types for binary operator '<'
if (this  < other)
          ^
first type:  Heap<T>
second type: Heap<T>

where T is a type-variable:
T extends Comparable<T> declared in class Heap

我不确定如何解决这个问题。我知道我的二元运算符不适用于泛型,但我不知道如何解决它。 感谢您的任何意见。对于您可能发现的所有初学者错误,我们深表歉意! 这是我的代码:

import java.util.*;

class Heap<T extends Comparable <T>>  implements Comparable<Heap<T>>{

private T[] heap;
private int lastIndex;
private static final int CAPACITY = 25;

public Heap(){

    this(CAPACITY);

}

public Heap(int capacity){

    heap = (T[])new Comparable[capacity+1];
    lastIndex = 0;
}

public void add(T newEntry){

    lastIndex++;
    if(lastIndex>=heap.length)
    doubleArray();

    int newIndex = lastIndex;
    int parentIndex = newIndex/2;

    while((parentIndex>0)&&(heap[parentIndex].compareTo(newEntry)>0))
    {
        heap[newIndex] = heap[parentIndex];
        newIndex = parentIndex;
        parentIndex = newIndex/2;
    }  
    heap[newIndex] = newEntry;
}

public void display()
{
    for(int i=1;i<heap.length;i++)
    {
        System.out.println(heap[i]);
    }

}

private void doubleArray()
{
T[] oldHeap = heap;
int oldSize = heap.length;

heap = (T[]) new Object[2*oldSize];

    for(int i =0; i < oldSize-1;i++)
    {
        heap[i] = oldHeap[i];
    }
}

public int compareTo(Heap<T> other)
{
    int sort = 0;
    if (this  < other)
    {
            sort = -1;
    }
    else if (this> other)
    {
            sort = 1;
    }
    else
    {
            sort = 0;
    }
    return sort;
}

private <T extends Comparable<T>> void reheap(T[] heap, int rootIndex, int lastIndex)
{
    boolean done=false;
    T orphan = heap[rootIndex];
    int leftChildIndex = 2 * rootIndex + 1;

    while(!done && (leftChildIndex<=lastIndex))
    {
    int largerChildIndex = leftChildIndex;
    int rightChildIndex = leftChildIndex + 1;

        if(rightChildIndex<=lastIndex &&     (heap[rightChildIndex].compareTo(heap[largerChildIndex])>0))
            largerChildIndex = rightChildIndex;
        if(orphan.compareTo(heap[largerChildIndex])<0)
        {
        //  System.out.println(orphan+ "--" + largerChildIndex);

            heap[rootIndex] = heap[largerChildIndex];
            rootIndex = largerChildIndex;
            leftChildIndex = 2 * rootIndex+1;
        }
        else
            done = true;
    }
heap[rootIndex] = orphan;
}

public <T extends Comparable<T>> void heapSort(int n)
{
    for(int rootIndex = n/2-1;rootIndex >=0;rootIndex--)
        reheap(heap,rootIndex,n-1);

    swap(heap,0,n-1);

    for(int lastIndex = n-2;lastIndex > 0;lastIndex--)
    {   
        reheap(heap,0,lastIndex);
        swap(heap,0,lastIndex);
    }
}

private <T extends Comparable<T>> void swap(T[] a,int first, int last)
{
T temp;

temp = a[first];
a[first] = a[last];
a[last] = temp;
}

}

非常感谢您对此的任何帮助

【问题讨论】:

  • 小于和大于运算符仅适用于原始数字类型。您无法将对象与它们进行比较。
  • 为了获得最佳效果,请使用&lt;T extends Comparable&lt;? super T&gt;&gt;

标签: java generics compareto heapsort


【解决方案1】:
  1. 你不希望你的Comparable;你想比较它的成员。因此,从你的类声明中删除 implements Comparable&lt;Heap&lt;T&gt;&gt; 并删除 compareTo 方法。

  2. 1234563删除这些声明。

【讨论】:

  • 哇,谢谢。当您简单地说希望成员具有可比性而不是堆时,这确实很有帮助。我经历了很多这样的事情的最初原因是这一行:if(orphan.compareTo(heap[largerChildIndex])
【解决方案2】:

我认为您需要在您的 T 对象上实现 compareTo,而不是在堆本身上。你必须 确保 T 在堆中具有可比性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    相关资源
    最近更新 更多