As Albin states, you'll need to use the
import heapq
heap = []
node1 = node['a', 3]
node2 = node['b', 2]
node3 = node['c', 1]
heapq.heappush[heap, node1]
heapq.heappush[heap, node2]
heapq.heappush[heap, node3]
# Should print 'a', 'b', 'c' in that order.
for i in range[3]:
print[heapq.heappop[heap].label]
3 module [unless you want to write your own heap implementation from scratch]. One very important thing to note is that this library gives only an implementation of a min-heap, rather than a max-heap.To write a comparator for a custom object, you'll need to override the
import heapq
heap = []
node1 = node['a', 3]
node2 = node['b', 2]
node3 = node['c', 1]
heapq.heappush[heap, node1]
heapq.heappush[heap, node2]
heapq.heappush[heap, node3]
# Should print 'a', 'b', 'c' in that order.
for i in range[3]:
print[heapq.heappop[heap].label]
4 function [the function that's called when the import heapq
heap = []
node1 = node['a', 3]
node2 = node['b', 2]
node3 = node['c', 1]
heapq.heappush[heap, node1]
heapq.heappush[heap, node2]
heapq.heappush[heap, node3]
# Should print 'a', 'b', 'c' in that order.
for i in range[3]:
print[heapq.heappop[heap].label]
5 operator is used]. So your node class would look like this:class node:
nodes = dict[]
def __init__[self, label, priority]:
self.label = label
self.priority = priority
def __lt__[self, other]:
# Note that we've reversed the operands here, since we want a max-heap.
return other.priority < self.priority
Then, we can make a heap as follows:
import heapq
heap = []
node1 = node['a', 3]
node2 = node['b', 2]
node3 = node['c', 1]
heapq.heappush[heap, node1]
heapq.heappush[heap, node2]
heapq.heappush[heap, node3]
# Should print 'a', 'b', 'c' in that order.
for i in range[3]:
print[heapq.heappop[heap].label]
Là một website được viết trên công nghệ web Flutter vì vậy hỗ trợ rất tốt cho người học, kể cả những người học khó tính nhất.
Hiện tại website đang tiếp tục được cập nhập nội dung cho phong phú và đầy đủ hơn. Mong các bạn nghé thăm và ủng hộ website mới của chúng tôi.
import heapq
heap = []
node1 = node['a', 3]
node2 = node['b', 2]
node3 = node['c', 1]
heapq.heappush[heap, node1]
heapq.heappush[heap, node2]
heapq.heappush[heap, node3]
# Should print 'a', 'b', 'c' in that order.
for i in range[3]:
print[heapq.heappop[heap].label]
0 Ví dụ: Với các phần tử không thể tự so sánh với nhau, bạn cần cung cấp một Comparator cho PriorityQueue.
0 là một lớp thi hành interface Queue nên nó có đầy đủ các đặc điểm của một hàng đợi và hỗ trợ tất cả các hoạt động tuỳ chọn của
Collection. Tuy nhiên khác với một hàng đợi thông thường, các phần tử của PriorityQueue được xắp xếp theo một thứ tự tăng dần dựa trên thứ tự tự nhiên của chúng hoặc theo một Comparator được cung cấp [Tuỳ vào constructor được sử dụng], vì vậy khi một phần tử mới được chèn vào PriorityQueue nó có thể không ở vị trí cuối cùng. import heapq
heap = []
node1 = node['a', 3]
node2 = node['b', 2]
node3 = node['c', 1]
heapq.heappush[heap, node1]
heapq.heappush[heap, node2]
heapq.heappush[heap, node3]
# Should print 'a', 'b', 'c' in that order.
for i in range[3]:
print[heapq.heappop[heap].label]
Nếu muốn duyệt qua tất cả các phần tử của một PriorityQueue mà không phải loại bỏ các phần tử, bạn có thể sử dụng Iterator hoặc Spliterator, tuy nhiên chúng không đảm báo thứ tự ưu tiên của các phần tử. Trong trường hợp này bạn nên sử dụng Arrays.sort[priorityQueue.toArray[]] hoặc Arrays.sort[priorityQueue.toArray[],comparator]. Queue:
- Về cơ bản, ngoài các đặc điểm nói trên PriorityQueue có đầy đủ các đặc điểm của một Queue. Các phương thức và các ví dụ liên quan về Queue được đề cập trong bài viết dưới đây: null.
public class PriorityQueue extends AbstractQueue implements java.io.Serializable
1- PriorityQueue là một hàng đợi không giới hạn [unbounded], các phần tử được xắp xếp theo thứ tự tăng dần và cho phép các phần tử trùng lặp.
PriorityQueue là một lớp thi hành interface Queue nên nó có đầy đủ các đặc điểm của một hàng đợi và hỗ trợ tất cả các hoạt động tuỳ chọn của Collection. Tuy nhiên khác với một hàng đợi thông thường, các phần tử của PriorityQueue được xắp xếp theo một thứ tự tăng dần dựa trên thứ tự tự nhiên của chúng hoặc theo một Comparator được cung cấp [Tuỳ vào constructor được sử dụng], vì vậy khi một phần tử mới được chèn vào PriorityQueue nó có thể không ở vị trí cuối cùng. PriorityQueue quản lý một mảng đối tượng [Object[]] để lưu trữ các phần tử của nó. Độ dài của mảng này lớn hơn so với số phần tử của hàng đợi, tuy nhiên mảng này sẽ được thay thế bởi một mảng khác có độ dài lớn hơn nếu cần thiết.
Các đặc điểm thừa kế từ Queue:
PriorityQueue[]
PriorityQueue[int initialCapacity]
PriorityQueue[int initialCapacity, Comparator