Last active
December 9, 2018 17:28
-
-
Save kadirmalak/5225f46e45287139cfe1541fff5df64f to your computer and use it in GitHub Desktop.
This file contains 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): | |
self.data = data | |
self.children = [] | |
def add(self, node): | |
self.children.append(node) | |
return self | |
def __str__(self): | |
return str(self.data) + "#[" + ",".join([str(c) for c in self.children]) + "]" | |
def __repr__(self): | |
return str(self) | |
def leaves(self): | |
if len(self.children) == 0: | |
yield self | |
else: | |
for c in self.children: | |
yield from c.leaves() | |
def routes_to_leaves(self, acc=[]): | |
if len(self.children) == 0: | |
yield (acc + [self.data])[1:] | |
else: | |
for c in self.children: | |
yield from c.routes_to_leaves(acc + [self.data]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment