Complexity of Treemap insertion vs HashMap insertion

前端 未结 4 1270
北海茫月
北海茫月 2021-02-05 10:08

I am confused with the time complexity of these two algorithms.

//time complexity O(nlog(n))
public void usingTreeMap(){
    Map map = ne         


        
相关标签:
4条回答
  • 2021-02-05 10:22

    Is the time complexity to the usingTreeMap algorithm correct.

    The time complexities of the basic TreeMap operations are specified correctly in the Javadoc.

    I do know in treemap the insertion time is log(n)

    Correct.

    but if we iterate over an array of 10 elements does it become nlog(n).

    If this means inserting those 10 elements the time complexity is M*log(N) where M is the size of the array and N is the size of the TreeMap. If it doesn't mean that, the question is unclear.

    0 讨论(0)
  • 2021-02-05 10:29

    Insertion time complexity is typically defined on a per instance basis.

    Average case:

    • HashMap O(1)
    • TreeMap O(logn) -- since the underlying structure is a red-black tree

    Worst case:

    • Hashmap O(n) -- in the case of a hashing collision
    • TreeMap O(logn)

    In your code above since you are inserting multiple items, we need to distinguish how many elements are in the maps (n) vs. how many elements are being added to the maps (m). If the maps are initially empty, then your runtime above is correct. If they already have some elements, then the runtimes would be:

                                    Avg      Worst
    Insert m elements into HashMap: O(m)     O(mn)
    Inset m elements into TreeMap:  O(mlogn) O(mlogn)
    
    0 讨论(0)
  • 2021-02-05 10:36

    Complexity with HashMap

    In the case of HashMap, the backing store is an array. When you try to insert ten elements, you get the hash, compute the specific array index from that hash, and since it's an array in the back, you inject in O(1).

    • For first element, time taken to insert = O(1)
    • For second element, time taken to insert = O(1)
    • .
    • .
    • For nth element, time taken to insert = O(1)

    So, total time for insertion of n elements in a HashMap = n * O(1) = O(n)


    Complexity with TreeMap

    In this case, the backing store is a Tree. For a tree with total k elements, on an average, the time to find the location is O(Log k).

    • 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), leading to the complexity of O(n Log (n)).


    One of the properties of logs is Log a + Log b = Log (ab). Using that, the insertion time in case of TreeMap sums to a lesser-known running time value of O(Log(n!)). But, since, O(Log(n!)) is bound by O(n Log(n)), the time complexity of insertion of n elements in a TreeMap is loosely written O(n Log(N)).

    0 讨论(0)
  • 2021-02-05 10:44

    It might not be. (i.e. when 4 elements out of 10 have same key, then N will be 7), so I believe more duplicate keys, better time for the insertion.

    0 讨论(0)
提交回复
热议问题