7. Heap
Insert and delete: O(logn)
lookup: O(1)
k largest elements, store in minHeap. If element > k, drop the smallest
PriorityQueue
add
poll: retrieve and remove head of the queue
peek: retrieve but not remove the head of queue
Comparator: 指定comparator物件告知如何排序,改寫compare
PriorityQueue
Comparable:
物件本身繼承comparable,在class裡改寫compareTo. e.g. Arrays.sort(A), Collections.sort(C)
Python
class 必須實作 def __lt__(self, other)
Last updated
Was this helpful?