Skip to content

Instantly share code, notes, and snippets.

@graingert
Forked from anonymous/gist:4163045
Created November 28, 2012 18:25
Show Gist options
  • Select an option

  • Save graingert/4163054 to your computer and use it in GitHub Desktop.

Select an option

Save graingert/4163054 to your computer and use it in GitHub Desktop.
class Tree(object):
def __init__(self, cargo, left=None, right=None):
self.cargo = cargo
self.left = left
self.right = right
def __str__(self):
return str(self.cargo)
#Traversing through the tree
def total(self):
return self.total(self.left) + self.total(self.right) + self.cargo
#Allocating the child nodes
#left = Tree(2)
#right = Tree(3)
tree = Tree(1, Tree(2), Tree(3))
print tree.total()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment