Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java TreeMap analogue in Python3

As a java guy learning Python I'm trying to find similarities between the languages. I found collections module in python and also built-in structures similar to java collections framework (covered by this post What is/are the Python equivalent(s) to the Java Collections Framework?).

But I haven't found analogue of a TreeMap - a map based on red-black tree - a map sorted by keys and guaranteed log(n) time cost for the get, put and remove operations

Is there such a structure in Python 3.x?

like image 703
Alex M981 Avatar asked Oct 14 '25 07:10

Alex M981


1 Answers

looks like I've found an answer: https://pypi.org/project/sortedmap/. As author mentions it's backed by red-black tree.

Also in order to provide SortedMap functionality the following lib can be used http://www.grantjenks.com/docs/sortedcontainers/introduction.html . It's not using red-black tree but it's using a list of keys sorted using bisect module.

like image 189
Alex M981 Avatar answered Oct 16 '25 20:10

Alex M981



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!