Created
June 17, 2015 15:21
-
-
Save anonymous/a615c03e9a0c81cdb449 to your computer and use it in GitHub Desktop.
Pasted from IPython
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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