You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A TreeMap stores key-value pairs in sorted key order, typically backed by a Red-Black tree or AVL tree. Python has no built-in TreeMap; the sortedcontainers package provides SortedDict.
Complexity
Operation
Time
Lookup / Insert / Delete
O(log n)
Iteration (sorted order)
O(n)
Python Usage
# pip install sortedcontainersfromsortedcontainersimportSortedDicttm=SortedDict()
tm['c'] =3; tm['a'] =1; tm['b'] =2forkeyintm: # a, b, c in orderprint(key, tm[key])
print('b'intm) # Truedeltm['a']