summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--monkey/graph.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/monkey/graph.py b/monkey/graph.py
index c69d55c..7f02c60 100644
--- a/monkey/graph.py
+++ b/monkey/graph.py
@@ -19,6 +19,21 @@ class Node(object):
nodes += n.preorder()
return nodes
+ # Return a list of subtrees of [self].
+ def subtrees(self):
+ yield self
+ for child in self.eout:
+ yield from child.subtrees()
+
+ # Return the list of leaves' values (left-to-right).
+ def terminals(self):
+ if not self.eout:
+ return [self.data]
+ terminals = []
+ for child in self.eout:
+ terminals.extend(child.terminals())
+ return terminals
+
# Return a one-line string representation of [self].
def __str__(self):
return '(' + str(self.data) + ' ' + \