Python implementation of the Java TreeMap/Tree.
Install with pip:
pip install pytreemap
Click here to access the documentation
This demo aims to show you the basic operations available in this package. Consult the documentation for more details.
>>> from pytreemap import TreeMap
>>> tm = TreeMap()
>>> tm[5] = 'Python is great!'
>>> print(tm)
{5=Python is great!}
>>> tm[10] = 'Java is also nice!'
>>> print(tm)
{5=Python is great!, 10=Java is also nice!}
>>> tm.put(-1, 'We love them both!')
>>> print(tm)
{-1=We love them both!, 5=Python is great!, 10=Java is also nice!}
>>> tm[5]
'Python is great!'
>>> tm[2]
KeyError: 'key not found'
>>> tm.get(2) # No error is raised
>>> del tm[10]
>>> print(tm)
{-1=We love them both!, 5=Python is great!}
>>> del tm[2]
KeyError: 'key not found'
>>> tm.remove(2) # No error is raised
>>> 2 in tm
False
>>> -1 in tm
True
>>> tm.contains_key(-1)
True
>>> [key for key in tm]
[-1, 5]
>>> [key for key in tm.key_set()]
[-1, 5]
>>> [value for value in tm.values()]
['We love them both!', 'Python is great!']
>>> [entry for entry in tm.entry_set()]
[-1=We love them both!, 5=Python is great!]
Most of the tests from Java that concerned TreeMap are passed. Check out the tests/ directory for more details.
All benchmarks are done on a laptop with Intel Core i7-7700HQ CPU and 16GB of RAM.
Since this package is an implementation of the Java TreeMap, the benchmarks are focused on comparing the performance between this package and Java’s TreeMap.
This package is currently written in pure Python and it should come at no surprise that it is much slower than Java, especially when the size of the tree is large.
A Cython version is in the works.
Benchmark procedure:
-
Prepare n entries with distinct keys. (n ranges from 1000 to 60000 with 1000 interval.)
-
Insert/Remove/Search them into the map in random order and record the completion time.
-
Repeat step 1-2 two more times and average the result.
Here is result using Java TreeMap:
And here is the result using pytreemap:
Overlay the plots together, we can see that pytreemap is ~30x slower: