Skip to content

walk_tree_bfs doesn not implement BFS #72

@qheath

Description

@qheath

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions