English 中文(简体)
优先程度的内部分类
原标题:Internal sorting of PriorityQueue
问题回答

优先照料办法的实施是一种肥胖症,在这种疾病中,所有儿童享有比父母更优先的地位,但不一定是兄弟姐妹。

这些元素储存在阵列(我怀疑)如下:

每个节点都储存在2个床上和2个床上。 因此,[1、2、3]:

element 1 (value 1), children are 2 and 9.
element 2 (value 2), children are 6 and 3
element 3 (value 9), 4 (value 6) and 5 (value 3) have no children...

如果你把母子从胎圈中除去,则以最优先的儿童之一取代母子,再由子女之一取而代之。 (运作非常理想,只有O(log n)运行时间。) 例如:

[1, 2, 9, 6, 3]
[2, 9, 6, 3]
[3, 9, 6]
[6, 9]
[6]

这份清单非常细致,它只是在我们作为清单印刷时所看到的微不足道。

rel=“nofollow”> javadoc说:

This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces. The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

如果是,

while (!q.isEmpty()) {
    System.out.println(q.poll());
}

你认为这些要素确实是正确的。

A PriorityQueue maintains things in order of priority. The first element you pull out is the highest priority. The likelhood is that this is implemented underneath using a heap data structure which provides the data in an order but without the full sorting (this allows more efficient insertion and deletion than resorting the contents each time).

作为消费者,内部秩序对于你来说对于优先秩序来说并不重要。 你们应当只是贪 gr,并确信他们是最优先事项。 内部是你应当关注的事项(见Java doc, JB Nizet指出)。

Basically, it s not sorted. Priority queue implementations usually amortize the sorting cost - they do the minimum amount of effort needed to ensure that elements are retrieved in the right order. It s partially sorted each time an element is extracted just enough to choose the highest priority element. The queue is never fully sorted.

如果你打电话poll()以提取数字,你将按你所期望的顺序重报。

 That particular method is used to sort the priority based index.
 When that remove method will call it will find the highest priority index.
       ```java  public Comparable remove(){
                if(index == 0){
                    System.out.println("The priority queue is empty!!");
                    return null;
                }
                int maxIndex = 0;
                // find the index of the item with the highest priority 
                for (int i=1; i<index; i++) { 
                    if (pQueue[i].compareTo (pQueue[maxIndex]) > 0) { 
                        maxIndex = i; 
                    } 
                } 
                Comparable result = pQueue[maxIndex]; 
                System.out.println("removing: "+result);
                // move the last item into the empty slot 
                index--; 
                pQueue[maxIndex] = pQueue[index]; 
                return result;
            }
 ```




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签