Does python have a sorted list?

前端 未结 7 1889
温柔的废话
温柔的废话 2020-11-29 20:16

By which I mean a structure with:

  • O(log n) complexity for x.push() operations
  • O(log n) complexity to find an element
  • O(n) comple
7条回答
  •  北海茫月
    2020-11-29 20:49

    The standard Python list is not sorted in any form. The standard heapq module can be used to append in O(log n) to an existing list and remove the smallest one in O(log n), but isn't a sorted list in your definition.

    There are various implementations of balanced trees for Python that meet your requirements, e.g. rbtree, RBTree, or pyavl.

提交回复
热议问题