2020-01-02 TreeMap 常见操作时间复杂度

Reference: https://stackoverflow.com/questions/20487619/complexity-of-treemap-insertion-vs-hashmap-insertion

The backing store is a Tree (Red-black tree).

Each Operations TC(best case)
get o(logn)
put o(logn)
remove o(logn)

For a tree with total k elements, on an average, the time to find the location is O(Log k).

Analysis:

Time to insert first element = O(1)
Time to insert second element = O(Log 1) = 0 = O(1)
Time to insert third element = O(Log 2)
.
.
Time to insert nth element = O(Log (n-1))
Total time = Log 1 + Log 2 + Log 3 + ... + Log (n-1)

Now, Log 1 <= Log n, Log 2 <= Log n ... Log (n-1) <= Log n, leading us to n-1 values each of which is less than or equal to Log n.

This means that the timing for insertion in a treeMap sum to a value <= (n-1) * Log (n-1), leading to the complexity of O(n Log (n)). (插入n个)

你可能感兴趣的:(2020-01-02 TreeMap 常见操作时间复杂度)