Python Library for Studying Binary Trees
Binarytree version 3.0.0 silently placed a restriction allowing only integers as node values. This was to avoid edge cases that couldn't be handled gracefully (e.g. user-supplied values without random generation or comparison). Pretty printing the tree was also an issue when the __repr__
of these custom values were too lengthy (tree rendering would break). There were many other technical issues with accepting custom values (which, by the way, is why binarytree.setup
function was removed). Hopefully in the future, all these challenges are solved so binarytree.setup
method can be added back again. In the meantime, integers were thought to be enough for the purpose of practicing tree algorithms. But the restriction seemed unnecessarily strong, which is why in this version, support for all number types (e.g. int, float, decimal.Decimal) have been added back.
Refactored exception names and messages for consistency and clarity.
Node.__iter__
now yields node instances instead of node values (see here for details).
Added new method Node.values
, which returns the list representation of the binary tree (i.e. node values in level order). This is essentially the replacement for the old Node.__iter__
(see here for details).
Full documentation is now here!
Complete overhaul (with non-backward compatible API changes):
binarytree.convert
method with binarytree.build and builtin iter method
binarytree.Node.show
to binarytree.Node.pprint
binarytree.subtree
, binarytree.prune
and binarytree.leafs
. Now all introspection & management of trees and nodes are done through methods provided by binarytree.Node.customize
method as it was unnecessarily complicating things. If you want to use your own custom node, you can always subclass binarytree.Node.is_perfect
boolean parameter.balanced
to is_balanced
for function binarytree.tree
.max
to is_max
for function binarytree.heap
.binarytree.pprint
to binarytree.show
.binarytree.setup
to binarytree.customize
.binarytree.show_ids
, binarytree.show_all
, binarytree.subtree
, binarytree.prune
(view the README for more details and examples).binarytree.leafs
.print
keyword (e.g. print(mytree)
). New ways to print them are mytree.show()
or show(mytree)
._build_str
function._build_str
function to improve clarity for the readers.Binary trees in a tree structure (as opposed to a list represenation) using binarytree.Node
can now be displayed using Python's builtin print
command, and has new methods inspect
and to_list
, which are equivalent to external functions inspect
and convert
respectively:
from binarytree import tree
my_tree = tree()
my_tree.to_list()
my_tree.inspect()
print(my_tree) # No need to use pprint
Docstrings and documentation are also updated.
Initial release