Skip to content

lianemeth/forest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)

About

A bunch of python trees

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages