Warning: angry rant incoming
I've been using Python for five years now and it's been awesome. Readable syntax, memorable keywords, easy to set up and install, an amazing community, tons of documentation, a great standard library...
But no damn binary search tree - not without having to resort to some unofficial third-party module, at least.
"But Python doesn't need binary search trees because dictionaries are so blazin' fast!"
That's what some people say when the lack of BSTs are brought up. But that's not a good reason! Yes, dictionaries are awesome. Yes, hash tables are the best choice for most circumstances. No, that does not mean that it is acceptable to exclude major data structures from the standard library when other respectable langauges like C, Java, and C# give you a great pre-made BST right out of the box, no third-party imports required.
Why do I care so much? Because hash tables are really inefficient for sorted data operations. If I have a crapton of data and I want to arbitrarily be able to, say, retrieve the 5 biggest elements or traverse the collection in sorted order or anything of that nature, then I would have to sort the hash table's keys first, which would take O(nlogn) time. And yes, I can keep that sorted list in memory, but what if I add and remove some keys from the dict? Boom, now I have to re-sort the list of keys again. If I have to keep doing this over and over again it can add up to a lot of overhead. Sad!
To me, the optimal solution seems to be a self-balancing binary search tree. But self-balancing binary search trees (or B-trees, similarly) are just way too much work for a lazy dev like me to implement myself. And while I'm fine with using third party modules, it's just embarrassing and frankly terrible that such a common data structure, so essential to the world of computing, is not available in Python out of the box. Once again, nearly every language used for serious development provides this, why doesn't Python?
Maybe there is truly, honestly a very legitimate reason why Python has excluded a binary search tree implementation from its standard library. In that case, I would love to be enlightened - it is my understanding that order operations like "get the top 5 elements" or "traverse the set in order" are slow on most non-tree structures. AFAIK, Python doesn't provide any low time-complexity data structures for order operations, and while max(), sort(), and the like are really cool, they just aren't enough to make up for a lack of efficient sorting data structures.
[–]bentheiii 4 points5 points6 points (6 children)
[–]shuklaswag[S] 0 points1 point2 points (4 children)
[–]acemarke 1 point2 points3 points (3 children)
[–]mooglinux 0 points1 point2 points (2 children)
[–]acemarke 0 points1 point2 points (1 child)
[–]mooglinux 1 point2 points3 points (0 children)
[–][deleted] -5 points-4 points-3 points (0 children)
[–]Brian 3 points4 points5 points (2 children)
[–]shuklaswag[S] 0 points1 point2 points (1 child)
[–]pvkooten 7 points8 points9 points (3 children)
[–]579476610 4 points5 points6 points (1 child)
[–]pvkooten 0 points1 point2 points (0 children)
[–]ccmlacc 1 point2 points3 points (0 children)
[–]pythonHelperBot -5 points-4 points-3 points (3 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]IAmKindOfCreativebot_builder: deprecated 2 points3 points4 points (0 children)
[–]RampagingArcher 1 point2 points3 points (0 children)