the current implementation of walk_tree_bfs actually implements DFS, as evidenced by the fact that it is almost identical to walk_tree_dfs
in other words, walk_tree_bfs node {...} is identical to walk_tree_dfs node { |node,level| node.children.each { |child| ... }}, modulo some corner-cases
I think the tests used in test/acts_as_tree_test.rb are not deep enough to show the difference, but if you take
1
-2
--3
---4
-5
--6
---7
then the current implementation generate
1 2 5 3 4 6 7
while I believe true BFS would generate
1 2 5 3 6 4 7
(4 and 6 are switched)
a correct implementation should use a FIFO to keep tracks of the next generation; I will suggest one later, unless someone beats me to it or explains that I'm mistaken
the current implementation of
walk_tree_bfsactually implements DFS, as evidenced by the fact that it is almost identical towalk_tree_dfsin other words,
walk_tree_bfs node {...}is identical towalk_tree_dfs node { |node,level| node.children.each { |child| ... }}, modulo some corner-casesI think the tests used in
test/acts_as_tree_test.rbare not deep enough to show the difference, but if you takethen the current implementation generate
while I believe true BFS would generate
(
4and6are switched)a correct implementation should use a FIFO to keep tracks of the next generation; I will suggest one later, unless someone beats me to it or explains that I'm mistaken