泛型堆中的比较运算符

发布于 2024-11-02 23:08:19 字数 1468 浏览 1 评论 0原文

对于我的数据结构课程,我们的作业是创建一个通用堆 ADT。在 siftUp() 方法中,我需要进行比较,如果父级较小,我需要进行交换。我遇到的问题是比较运算符对泛型类型无效。我相信我需要使用 Comparable 接口,但从我读到的内容来看,与数组一起使用并不是一个好主意。我还搜索了这个网站,我找到了与这篇文章相关的好信息,但它们都没有帮助我找到解决方案

,我删除了一些不相关的代码 谢谢

public class HeapQueue<E> implements Cloneable  {   
  private int highest;
  private Integer manyItems;
  private E[] data; 

  public HeapQueue(int a_highest) {
      data = (E[]) new Object[10];
      highest = a_highest;

  } 

  public void add(E item, int priority) {
      // check to see is priority value is within range
      if(priority < 0 || priority > highest) {
        throw new IllegalArgumentException
          ("Priority value is out of range: " + priority);
      }     
      // increase the heaps capacity if array is out of space
      if(manyItems == data.length)
        ensureCapacity();
      manyItems++;
      data[manyItems - 1] = item;
      siftUp(manyItems - 1);
  }

  private void siftUp(int nodeIndex) {
      int parentIndex;
      E tmp;
       if (nodeIndex != 0) {
            parentIndex = parent(nodeIndex);
            if (data[parentIndex] < data[nodeIndex]) {  <-- problem ****
                  tmp = data[parentIndex];
                  data[parentIndex] = data[nodeIndex];
                  data[nodeIndex] = tmp;
                  siftUp(parentIndex);
            }
        }
      } 

  private int parent(int nodeIndex) {
      return (nodeIndex - 1) / 2;
  }
}

For my data structures class our homework is to create a generic heap ADT. In the siftUp() method I need to do comparison and if the parent is smaller I need to do a swap. The problem I am having is that the comparison operators are not valid on generic types. I believe I need to use the Comparable interface but from what I read it’s not a good idea to use with Arrays. I have also search this site and I have found good information that relates to this post none of them helped me find the solution

I removed some of the code that wasn’t relevant
Thanks

public class HeapQueue<E> implements Cloneable  {   
  private int highest;
  private Integer manyItems;
  private E[] data; 

  public HeapQueue(int a_highest) {
      data = (E[]) new Object[10];
      highest = a_highest;

  } 

  public void add(E item, int priority) {
      // check to see is priority value is within range
      if(priority < 0 || priority > highest) {
        throw new IllegalArgumentException
          ("Priority value is out of range: " + priority);
      }     
      // increase the heaps capacity if array is out of space
      if(manyItems == data.length)
        ensureCapacity();
      manyItems++;
      data[manyItems - 1] = item;
      siftUp(manyItems - 1);
  }

  private void siftUp(int nodeIndex) {
      int parentIndex;
      E tmp;
       if (nodeIndex != 0) {
            parentIndex = parent(nodeIndex);
            if (data[parentIndex] < data[nodeIndex]) {  <-- problem ****
                  tmp = data[parentIndex];
                  data[parentIndex] = data[nodeIndex];
                  data[nodeIndex] = tmp;
                  siftUp(parentIndex);
            }
        }
      } 

  private int parent(int nodeIndex) {
      return (nodeIndex - 1) / 2;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心旳酸楚 2024-11-09 23:08:20

如果我没读错的话,E 只需要扩展 Comparable 然后你的问题线就变成了...

if (data[parentIndex].compareTo(ata[nodeIndex]) < 0)

这并没有违反我所知道的任何投注实践规则。

If I am reading this right, E simply needs to extend Comparable and then your problem line becomes...

if (data[parentIndex].compareTo(ata[nodeIndex]) < 0)

This is not breaking any bet-practice rules that I know of.

岛徒 2024-11-09 23:08:19

从技术上讲,您正在使用项目上的类似接口,而不是数组。特别是数组中的一项。我认为这里最好的解决方案是在构造函数中接受一个比较器,用户可以传递该比较器来比较他的通用对象。

Comparator<E> comparator;
public HeapQueue(int a_highest, Comparator<E> compare)
{
    this.comparator = compare;

然后,您可以将该比较器存储在成员函数中,并使用

if (comparator.compare(data[parentIndex],data[nodeIndex]) < 0)  

In 代替小于运算符。

Technically you're using the comparable interface on on item, not an array. One item in the array specifically. I think the best solution here is to accept, in the constructor, a Comparator that the user can pass to compare his generic objects.

Comparator<E> comparator;
public HeapQueue(int a_highest, Comparator<E> compare)
{
    this.comparator = compare;

Then, you would store that comparator in a member function and use

if (comparator.compare(data[parentIndex],data[nodeIndex]) < 0)  

In place of the less than operator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文