Skip to content

Latest commit

 

History

History
62 lines (42 loc) · 1.01 KB

README.md

File metadata and controls

62 lines (42 loc) · 1.01 KB

forest

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

Binary Tree

from forest.BinaryTree import BinaryTree

tree = BinaryTree(key=1, item='object',
                  left=BinaryTree(key=2, item='left'),
                  right=BinaryTree(key=3, item='right'),
)

def do_something(node):
    print node

tree.pre_order(visit=do_something)

Binary Search Tree

from forest.BinaryTree import BinarySearchTree

tree = BinarySearchTree(10, 'b')
tree[15] = 'k'
tree[17] = 'm'

# search
tree[15]

# removal
del tree[15]

It also contains a Red Black Tree implementation with a similar interface.

N-ary Tree

from forest.NaryTree import NaryTree

tree = 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')

def do_something(node):
    print node

tree.traversal(visit=do_something)