k
elements and is ordered by frequency and lexicographical order.k
, add the current word-frequency pair to the heap.
b. Otherwise, if the current frequency is higher than the root of the heap, or the frequency is the same but the word is lexicographically smaller, replace the root with the current word-frequency pair and heapify.