Created
March 19, 2015 02:15
-
-
Save charlesetc/a9ab6a6115d1c609af87 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# node.jl | |
# A Node Type | |
# with a name | |
# and a list of children | |
type Node | |
name :: Char | |
children :: Array{Node} | |
Node(n) = new(n, []) | |
Node(a, b) = new(a, b) | |
end | |
# Add one node to the other's list of children. | |
function push(base :: Node, child :: Node) | |
push!(base.children, child) | |
end | |
a = Node('a') | |
b = Node('b') | |
c = Node('c') | |
d = Node('d') | |
# a | |
push(a, b) # / \ | |
push(b, c) # b d | |
push(a, d) # / | |
# c | |
function depth_search(node :: Node, name :: Char) | |
# First check if this is the node we're looking for. | |
if node.name == name | |
return node | |
else | |
# Then search through all the children | |
for child in node.children | |
result = depth_search(child, name) | |
# This is needed because objects can't be treated | |
# as booleans. | |
if result != false | |
# If there is a child found, return that child. | |
return result | |
end | |
end | |
# A value other than a child | |
# There's no nil/null, so... | |
return false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment