Python standard library - is there a module for balanced binary tree?

Is there a module for AVL or Red-Black, or some other type of balanced binary tree in the Python standard library? I tried to find one, but to no avail (I'm relatively new to Python).

+54
python standard-library tree
Feb 19 '10 at 17:06
source share
6 answers

No, there is no balanced binary tree in stdlib. However, from your comments, it seems that you may have other options:

  • You say you want to use BST instead of a list to search for O(log n) . If search is all you need and your data is already sorted, the bisect module provides a binary search algorithm for lists.
  • Mike DeSimone recommended dialing and dicts, and you explained why lists are too algorithmically slow. Sets and dicts are implemented as hash tables that have O (1) lookups. Solving most of the problems in Python is actually "using a dict."

If none of the solutions is right for you, you will have to upgrade to a third-party module or implement your own.

+27
Feb 19 '10 at 17:26
source share
+14
Feb 19 '10 at 17:10
source share

There were several cases where I found the heapq package (in the stadndard library) to be useful, especially if at any given time that you want O (1) to access the smallest item in your collection.

For me, I kept track of a set of timers and, as a rule, I was just interested in checking if the minimum time (the one that should be executed first) was ready.

+8
Feb 22 '10 at 6:13
source share

There is a new package called "bintrees" that supports ubalanced, AVL and RB trees. You can find it here .

+4
May 21 '11 at 11:56
source share

No, but there AVL Tree Objects for Python (very old!) And a (closed) SourceForge project are avl trees for Python .

+2
Feb 19 '10 at 17:13
source share

Check out also the Sorted Containers project.

Here PyCon talks about it: https://www.youtube.com/watch?v=7z2Ki44Vs4E

+1
Feb 25 '18 at 19:41
source share



All Articles