简体   繁体   中英

Why is the time complexity of inserting an element into a set O(log N) and deletion O(N)?

有人可以简要解释一下,我用谷歌搜索,但只发现时间复杂性而没有任何解释。

Why is the time complexity of inserting an element into a set O(log N) and deletion O(N)?

The former is accurate, but the latter is not true; both removal and insertion of elements is O(log N) time complexity :

std::set is an associative container that contains a sorted set of unique objects of type Key . Sorting is done using the key comparison function Compare . Search, removal, and insertion operations have logarithmic complexity . Sets are usually implemented as red-black trees.

The std::set is typically implemented as a red-black tree , which fulfills the complexity requirement placed on the std::set associative container:

[...] The insertion and deletion operations, along with the tree rearrangement and recoloring, are also performed in O(log n) time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM