Skip to content

Instantly share code, notes, and snippets.

@kadirmalak
Last active December 9, 2018 17:28
Show Gist options
  • Save kadirmalak/5225f46e45287139cfe1541fff5df64f to your computer and use it in GitHub Desktop.
Save kadirmalak/5225f46e45287139cfe1541fff5df64f to your computer and use it in GitHub Desktop.
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