Built-in binary search tree in Python? -


are there self-balancing binary search tree (red-black, avl or others) built-in types in python 2.7 or python 3.x?

i looking equivalent java's treemap or treeset.

if there no such built-ins, why have been ommited? there special reason, not including such tools?

there's no special reason, knowledge - i'd guess reason many applications highly-tuned dict , set implementations (which hash tables) work well. they're enough in cases. there situations need performance characteristics of balanced binary search trees (like ordered traversal based on key- rather addition-order), far enough off beaten path people happy grabbing third-party package in case.

i've had experience using bintrees package on pypi. has implementations of unbalanced, avl , red-black binary trees, in both pure python , extensions written in cython.

i think rest of reason historical accident. if person wrote bintrees lobbied inclusion in stdlib, , willing put constraints imposes on maintenance , releases, go in. (although cython dependency cause problem, i'd guess.)

algorithmic complexity:

for hash tables (like dicts or sets), insertion , lookup o(1), while balanced tree these o(log(n)). in-order traversal of keys o(n) in tree, same thing hash table need sort keys first, it's o(n*log(n)). when you're picking kind of data structure use, need think operations you're going using, , pick tradeoff makes sense in application.


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -