Is there a Heap in java?

后端 未结 6 2100
一整个雨季
一整个雨季 2021-01-31 13:00

I am porting a C++ library to Java and I need a heap data structure. Is there a standard implementation or will I need to do it myself?

6条回答
  •  忘了有多久
    2021-01-31 13:49

    Min heap:

    PriorityQueue minHeap = new PriorityQueue();
    

    Max heap:

    PriorityQueue maxHeap = new PriorityQueue(new Comparator() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return - Integer.compare(o1, o2);
        }
    });
    

提交回复
热议问题