Created
January 2, 2010 05:01
-
-
Save nazt/267384 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 DFS { | |
def visitedNodeCounter = 0 | |
def visitor = { | |
visitedNodeCounter++ | |
} | |
def visit(node) { | |
node.acceptVisitor visitor | |
node.children.each { visit(it) } | |
} | |
} | |
class Node { | |
def children = [] | |
def addChild(node) { | |
children << node | |
} | |
def acceptVisitor(visitor) { | |
visitor() | |
} | |
} | |
root = new Node() | |
100.times { | |
root.addChild new Node() | |
} | |
root.children.each { child -> | |
50.times { child.addChild new Node() } | |
} | |
dfs = new DFS() | |
dfs.visit(root) | |
result = dfs.visitedNodeCounter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment