0

我正在尝试实现一个优先级队列,该队列将按其大小顺序对 HashSet 进行排序(即最小的 HashSet 将具有最高优先级)。

我如何在 Java 中实现它?

下面是我尝试按优先级编号(最高优先)成功排序 HashSet。

我的主要方法:

        System.out.print("Enter size of priority queue: ");
        int inputSize = scanner.nextInt();

        HashSetQueue pq = new HashSetQueue(inputSize);

        System.out.println("1. insert");
        System.out.println("2. remove");
        System.out.println("3. check empty");
        System.out.println("4. check full");
        System.out.println("5. empty");
        System.out.println("6. check size");
        System.out.println("7. print");
        System.out.print("Please enter a number: ");

        int choice = scanner.nextInt();
        switch (choice) {
        case 1:
          System.out.print("Please enter a value: ");
          int userInput = scanner.nextInt();

          HashSet<Integer> set = new HashSet<Integer>();
          set.add(userInput);

          System.out.println("Please enter a priority: ");
          int priority = scanner.nextInt();

          pq.insert(priority, set);
          break;
        case 2:
          System.out.println("\nJob removed \n\n" + pq.remove());
          break;
        case 3:
          System.out.println("\nEmpty Status: " + pq.isEmpty());
          break;
        case 4:
          System.out.println("\nFull Status: " + pq.isFull());
          break;
        case 5:
          System.out.println("\nPriority Queue Cleared!");
          pq.clear();
          break;
        case 6:
          System.out.println("\nSize = " + pq.size());
          break;
        case 7:
          pq.print();
          break;
        default:
          System.out.println("\nPlease enter a valid number on the list!");
          break;

我的 PriorityQueue 课程

import java.util.HashSet;

/** class Task **/
class Task {
  int priority;
  HashSet<Integer> job;

  /** Constructor **/
  public Task(int priority, HashSet<Integer> job) {
    this.job = job;
    this.priority = priority;
  }

  /** toString() **/
  public String toString() {
    return "\nPriority : " + priority + "\nJob Name : " + job;
  }
}

/** Class PriorityQueue **/
class HashSetQueue {
  private Task[] heap;
  private int heapSize, capacity;

  /** Constructor **/
  public HashSetQueue(int capacity) {
    this.capacity = capacity++;
    heap = new Task[this.capacity];
    heapSize = 0;
  }

  /** function to clear **/
  public void clear() {
    heap = new Task[capacity];
    heapSize = 0;
  }

  /** function to check if empty **/
  public boolean isEmpty() {
    return heapSize == 0;
  }

  /** function to check if full **/
  public boolean isFull() {
    return heapSize == capacity - 1;
  }

  /** function to get Size **/
  public int size() {
    return heapSize;
  }

  public void print() {
    Task item;

    for (int i = 1; i <= heapSize; i++) {
      item = heap[i];
      System.out.println(item);
    }
  }

  /** function to insert task **/
  public void insert(int priority, HashSet<Integer> job) {
    Task newJob = new Task(priority, job);

    heap[++heapSize] = newJob;
    int pos = heapSize;
    while (pos != 1 && newJob.priority > heap[pos / 2].priority) {
      heap[pos] = heap[pos / 2];
      pos /= 2;
    }
    heap[pos] = newJob;
  }

  /** function to remove task **/
  public Task remove() {
    int parent, child;
    Task item, temp;
    if (isEmpty()) {
      System.out.println("Heap is empty");
      return null;
    }

    item = heap[1];
    temp = heap[heapSize--];

    parent = 1;
    child = 2;
    while (child <= heapSize) {
      if (child < heapSize && heap[child].priority < heap[child + 1].priority)
        child++;
      if (temp.priority >= heap[child].priority)
        break;

      heap[parent] = heap[child];
      parent = child;
      child *= 2;
    }
    heap[parent] = temp;

    return item;
  }
}
4

2 回答 2

0

你可以使用这个:

PriorityQueue<HashSet> priorityQueueOfHashSets = new PriorityQueue<>(Comparator.comparingInt(set -> set.size()));
于 2020-04-17T03:01:04.213 回答
0

我认为您需要更改此行:

while (pos != 1 && newJob.priority > heap[pos / 2].priority) {

至:

while (pos != 1 && newJob.job.size() < heap[pos / 2].job.size()) {

这将在具有更多作业的另一个任务之前插入新任务。

另一方面,如果它不是出于教育目的,那么不要试图重新发明轮子,只需将 aPriorityQueue与您的Comparator

于 2016-10-09T19:26:40.970 回答