Skip to content

Instantly share code, notes, and snippets.

@nazt
Created January 2, 2010 05:01
Show Gist options
  • Save nazt/267384 to your computer and use it in GitHub Desktop.
Save nazt/267384 to your computer and use it in GitHub Desktop.
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