Skip to content

Instantly share code, notes, and snippets.

Created June 17, 2015 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/a615c03e9a0c81cdb449 to your computer and use it in GitHub Desktop.
Save anonymous/a615c03e9a0c81cdb449 to your computer and use it in GitHub Desktop.
Pasted from IPython
class Node:
def __init__(self, data, children=None):
self.data = data
self.children = children or []
def find(self, data):
for i in self.descendants():
if i.data == data:
return i
def descendants(self):
yield self
for i in self.children:
yield from i.descendants()
def __repr__(self):
return "Node({!r})".format(self.data)
def __iter__(self):
yield from self.children
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment