Skip to content

Instantly share code, notes, and snippets.

@joelburton
Forked from anonymous/file1.py
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelburton/439d801faed4f8a69c6f to your computer and use it in GitHub Desktop.
Save joelburton/439d801faed4f8a69c6f to your computer and use it in GitHub Desktop.
A neat idea
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