You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
355 B
Ruby
23 lines
355 B
Ruby
def bfs_tree(node)
|
|
queue = [node]
|
|
|
|
while queue.size != 0
|
|
n = queue.shift
|
|
puts n.value
|
|
n.children.each do |child|
|
|
queue.push(child)
|
|
end
|
|
end
|
|
end
|
|
|
|
def bfs_binary_tree(node)
|
|
queue = [node]
|
|
|
|
while queue.size != 0
|
|
n = queue.shift
|
|
puts n.value
|
|
queue.push(n.left) if n.left
|
|
queue.push(n.right) if n.right
|
|
end
|
|
end
|