I'm using this as a playground of implementation of trees in Python. Hopefully you find it useful.
Python Trees:
- Binary Tree
- Binary Search Tree
- N-ary Tree
fromforest.BinaryTreeimportBinaryTreetree=BinaryTree(key=1, item='object',
left=BinaryTree(key=2, item='left'),
right=BinaryTree(key=3, item='right'),
)
defdo_something(node):
printnodetree.pre_order(visit=do_something)fromforest.BinaryTreeimportBinarySearchTreetree=BinarySearchTree(10, 'b')
tree[15] ='k'tree[17] ='m'# searchtree[15]
# removaldeltree[15]It also contains a Red Black Tree implementation with a similar interface.
fromforest.NaryTreeimportNaryTreetree=NaryTree(key='1')
branch1=tree.add_child(key='1.1')
branch2=tree.add_child(key='1.2')
branch11=branch1.add_child(key='1.1.1')
defdo_something(node):
printnodetree.traversal(visit=do_something)