Hàng đợi ưu tiên là một loại hàng đợi đặc biệt trong đó mỗi phần tử được liên kết với một giá trị ưu tiên. Và, các yếu tố được phục vụ trên cơ sở ưu tiên của họ. Tức là các phần tử có mức độ ưu tiên cao hơn sẽ được phục vụ trước Show Tuy nhiên, nếu các phần tử có cùng mức độ ưu tiên xuất hiện, chúng sẽ được phục vụ theo thứ tự của chúng trong hàng đợi Gán giá trị ưu tiên Nói chung, giá trị của chính phần tử được xem xét để gán mức độ ưu tiên. Ví dụ, Phần tử có giá trị cao nhất được coi là phần tử ưu tiên cao nhất. Tuy nhiên, trong các trường hợp khác, chúng ta có thể coi phần tử có giá trị thấp nhất là phần tử ưu tiên cao nhất Chúng tôi cũng có thể thiết lập các ưu tiên theo nhu cầu của chúng tôi Xóa phần tử ưu tiên cao nhấtSự khác biệt giữa Hàng đợi Ưu tiên và Hàng đợi Bình thườngTrong một hàng đợi, quy tắc nhập trước xuất trước được thực hiện trong khi đó, trong hàng đợi ưu tiên, các giá trị được loại bỏ trên cơ sở mức độ ưu tiên. Phần tử có mức ưu tiên cao nhất sẽ bị xóa trước Triển khai hàng đợi ưu tiênHàng đợi ưu tiên có thể được triển khai bằng mảng, danh sách được liên kết, cấu trúc dữ liệu heap hoặc cây tìm kiếm nhị phân. Trong số các cấu trúc dữ liệu này, cấu trúc dữ liệu heap cung cấp triển khai hiệu quả các hàng đợi ưu tiên Do đó, chúng ta sẽ sử dụng cấu trúc dữ liệu heap để triển khai hàng đợi ưu tiên trong hướng dẫn này. Một heap tối đa được thực hiện trong các hoạt động sau. Nếu bạn muốn tìm hiểu thêm về nó, vui lòng truy cập max-heap và min-heap Một phân tích so sánh về các triển khai khác nhau của hàng đợi ưu tiên được đưa ra dưới đây OperationspeekinsertdeleteLinked List If nodeToBeDeleted is the leafNode remove the node Else swap nodeToBeDeleted with the lastLeafNode remove noteToBeDeleted heapify the array1 If nodeToBeDeleted is the leafNode remove the node Else swap nodeToBeDeleted with the lastLeafNode remove noteToBeDeleted heapify the array2 If nodeToBeDeleted is the leafNode remove the node Else swap nodeToBeDeleted with the lastLeafNode remove noteToBeDeleted heapify the array1 Binary Heap If nodeToBeDeleted is the leafNode remove the node Else swap nodeToBeDeleted with the lastLeafNode remove noteToBeDeleted heapify the array1 If nodeToBeDeleted is the leafNode remove the node Else swap nodeToBeDeleted with the lastLeafNode remove noteToBeDeleted heapify the array1 If nodeToBeDeleted is the leafNode remove the node Else swap nodeToBeDeleted with the lastLeafNode remove noteToBeDeleted heapify the array1 Binary Search Tree If nodeToBeDeleted is the leafNode remove the node Else swap nodeToBeDeleted with the lastLeafNode remove noteToBeDeleted heapify the array1 If nodeToBeDeleted is the leafNode remove the node Else swap nodeToBeDeleted with the lastLeafNode remove noteToBeDeleted heapify the array1 If nodeToBeDeleted is the leafNode remove the node Else swap nodeToBeDeleted with the lastLeafNode remove noteToBeDeleted heapify the array1 Hoạt động hàng đợi ưu tiênCác hoạt động cơ bản của hàng đợi ưu tiên là chèn, xóa và tìm kiếm các phần tử Trước khi tìm hiểu về priority queue, mời các bạn tham khảo cấu trúc dữ liệu heap để hiểu rõ hơn về binary heap vì nó được dùng để implement priority queue trong bài viết này 1. Chèn một phần tử vào hàng đợi ưu tiênViệc chèn một phần tử vào hàng đợi ưu tiên (max-heap) được thực hiện theo các bước sau
Thuật toán chèn phần tử vào hàng đợi ưu tiên (max-heap) If there is no node, create a newNode. else (a node is already present) insert the newNode at the end (last node from left to right.) heapify the array Đối với Min Heap, thuật toán trên được sửa đổi để If nodeToBeDeleted is the leafNode remove the node Else swap nodeToBeDeleted with the lastLeafNode remove noteToBeDeleted heapify the array6 luôn nhỏ hơn If nodeToBeDeleted is the leafNode remove the node Else swap nodeToBeDeleted with the lastLeafNode remove noteToBeDeleted heapify the array7 2. Xóa một phần tử khỏi hàng đợi ưu tiênViệc xóa một phần tử khỏi hàng đợi ưu tiên (max-heap) được thực hiện như sau
Thuật toán xóa một phần tử trong hàng đợi ưu tiên (max-heap) If nodeToBeDeleted is the leafNode remove the node Else swap nodeToBeDeleted with the lastLeafNode remove noteToBeDeleted heapify the array Đối với Min Heap, thuật toán trên được sửa đổi để cả hai If nodeToBeDeleted is the leafNode remove the node Else swap nodeToBeDeleted with the lastLeafNode remove noteToBeDeleted heapify the array8 đều nhỏ hơn If nodeToBeDeleted is the leafNode remove the node Else swap nodeToBeDeleted with the lastLeafNode remove noteToBeDeleted heapify the array9 3. Nhìn trộm từ Hàng đợi ưu tiên (Tìm tối đa/phút)Thao tác Peek trả về phần tử lớn nhất từ Max Heap hoặc phần tử nhỏ nhất từ Min Heap mà không xóa nút Đối với cả Heap tối đa và Heap tối thiểu If nodeToBeDeleted is the leafNode remove the node Else swap nodeToBeDeleted with the lastLeafNode remove noteToBeDeleted heapify the array0 4. Trích xuất tối đa/tối thiểu từ hàng đợi ưu tiênExtract-Max trả về nút có giá trị tối đa sau khi xóa nó khỏi Max Heap trong khi Extract-Min trả về nút có giá trị tối thiểu sau khi xóa nó khỏi Min Heap |