I see quite a few people saying "TreeMap look-up takes O(n log n)
"!! How come?
I don't know how it has been implemented but in my head it takes O(log n)
.
This is because look-up in a tree can be done in O(log n)
. You don't sort the entire tree every time you insert an item in it. That's the whole idea of using a tree!
Hence, going back to the original question, the figures for comparison turn out to be:
HashMap approach: O(n + k log k)
average case, worst case could be much larger
TreeMap approach: O(k + n log k)
worst case
where n = number of words in the text , k = number of distinct words in the text.