【PriorityQueue】PriorityQueue的toString打印时的排序问题

渣渣平时很少用到这种高大上的类型,今天突然心血来潮试了一下,发现PriorityQueue的toString方法输出的竟然不是规定好的的排序;

import java.util.PriorityQueue;

public class PriorityTest {
    public static void main(String[] args) {
        PriorityQueue<Integer> queue=new PriorityQueue<>((a,b)->a-b);
        queue.add(2);
        queue.add(1);
        queue.add(3);
        queue.add(5);
        queue.add(0);
        System.out.println(queue);
    }
}

输出的结果是

[0, 1, 3, 5, 2]

并不是定义好的比较器规则,于是,我就开始纠结了。。。查了半天也没有发现别人遇到这种问题,大概是我的基础太渣,只有像我这样的渣渣才回遇到这种脑残问题吧。

我又采用迭代器的方式进行取值:

import java.util.PriorityQueue;

public class PriorityTest {
    public static void main(String[] args) {
        PriorityQueue<Integer> queue=new PriorityQueue<>((a,b)->a-b);
        queue.add(2);
        queue.add(1);
        queue.add(3);
        queue.add(5);
        queue.add(0);

        while (!queue.isEmpty()){
            System.out.print(queue.poll()+"\t");
        }
    }
}

 

输出结果为:

 

0	1	2	3	5

这样就突然正常了,我突然想到这是一个最小堆结构,

插入的顺序是这样的,

2

 

        2
    /
1

调整顺序后为

        1
    /
2
        1
    /       \
2               3
                1
            /       \
        2               3
    /
5
               1
            /       \
        2               3
    /       \
5               0

顺序调整:

               1
            /       \
         0              3
    /       \
5              2 
               0 
            /       \
        1              3
    /       \
5              2 

 

最后输出的是按行进行输出,所以输出的结果为[0, 1, 3, 5, 2],但是如果进行迭代器输出还是正常的。

 

大概也只有我会遇到这种我问题了。。。

 

 

 

 

 

 

 

 

 

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
在 C# 中,PriorityQueue 可以通过使用 System.Collections.Generic 命名空间下的 PriorityQueue 类来实现。需要注意的是,PriorityQueue 类并不是 C# 标准库中的一部分,因此需要使用第三方库或者自己实现该类。 以下是一个使用 System.Collections.Generic 命名空间下的 PriorityQueue 类的示例代码: ```csharp using System; using System.Collections.Generic; public class PriorityQueue<T> where T : IComparable<T> { private List<T> data; public PriorityQueue() { this.data = new List<T>(); } public void Enqueue(T item) { data.Add(item); int ci = data.Count - 1; // child index; start at end while (ci > 0) { int pi = (ci - 1) / 2; // parent index if (data[ci].CompareTo(data[pi]) >= 0) break; // child item is larger than (or equal) parent so we're done T tmp = data[ci]; data[ci] = data[pi]; data[pi] = tmp; ci = pi; } } public T Dequeue() { // assumes pq is not empty; up to calling code int li = data.Count - 1; // last index (before removal) T frontItem = data[0]; // fetch the front data[0] = data[li]; data.RemoveAt(li); --li; // last index (after removal) int pi = 0; // parent index. start at front of pq while (true) { int ci = pi * 2 + 1; // left child index of parent if (ci > li) break; // no children so done int rc = ci + 1; // right child if (rc <= li && data[rc].CompareTo(data[ci]) < 0) ci = rc; // prefer smallest child if (data[pi].CompareTo(data[ci]) <= 0) break; // parent is smaller than (or equal to) smallest child so done T tmp = data[pi]; data[pi] = data[ci]; data[ci] = tmp; // swap parent and child pi = ci; } return frontItem; } public int Count { get { return data.Count; } } public override string ToString() { string s = ""; for (int i = 0; i < data.Count; ++i) s += data[i].ToString() + " "; s += "count = " + data.Count; return s; } public bool IsConsistent() { // is the heap property true for all data? if (data.Count == 0) return true; int li = data.Count - 1; // last index for (int pi = 0; pi < data.Count; ++pi) // each parent index { int lci = 2 * pi + 1; // left child index int rci = 2 * pi + 2; // right child index if (lci <= li && data[pi].CompareTo(data[lci]) > 0) return false; // if lc exists and it's greater than parent then bad. if (rci <= li && data[pi].CompareTo(data[rci]) > 0) return false; // check the right child too. } return true; // passed all checks } } ``` 以上代码实现了一个基本的 Priority Queue 类,可以用于存储任意实现了 IComparable<T> 接口的类型。在该类中,Enqueue() 方法用于将元素加入队列,Dequeue() 方法用于取出队首元素并移除,Count 属性返回队列中元素数量,IsConsistent() 方法用于检查队列是否符合堆的定义。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值