How to functionally generate a tree breadth-first. (With Haskell) -


Say I have the following Haskell tree type, where "State" is a simple cover:

  

data tree A = branch (state A) [tree A] | Leaf I -> Tree A "which takes a leaf node, and it is a branch , Or takes a branch and gives it untouched, this tree type represents the N-Ary search-tree.

Deeply searching is a garbage, because the search space is clearly infinite, because I can easily expand the search space with the use of extension of the nodes of the leaves of all the trees, and the mistake The possibility of missing the target state is too big ... the only solution is a fourth-first discovery, which has been implemented very well, which will be found if there is. / P>

To generate the want , however, the tree is detected Up Finding a solution is a problem because I only know that How to do this depth-first, which can only be called "expansion" again on the node for the first time "expansion" function ... as long as the target-state is not found. (This literally will not produce anything really inconvenient.)

Can someone give me an indication of how to do this (or the entire algorithm), or with the decision that it is possible or Not a decent complexity? (Or any source on it, because I did not find anything.)

Have you seen Chris Okasaki ? The data contains a monadic tree builder named unfoldTreeM_BF in the Tree module, which uses a paper-optimized algorithm.

Here's an example that I think is what you are doing:

Suppose I want to find an infinite binary tree of stars where all the left children are the parent string And "A", and the right children are parents and "BB". I can use the unfoldTreeM_BF to search the width of the tree first and return the search tree to the solution:

  import control Currency. Import data data Tree children:: string - & gt; [String] child x = [x ++ "a", x ++ "bb"] xray = explore expose & lieutenant; - If returned, then return (x, []) and do this (first, after that) = brake (== query) $ children x, then tap after return (x, first) and second true return (x Before ++ [after head] SearchBF Query = (evalState $ unfoldTreeM_BF (Expand Query) []) False PrintSearchBF = Dotry SearchBF  

This is not very beautiful, but Does it work. If I search for "Aubb" then I get what I want:

  | + - A | | | + - AA | | | | | + - aaa | | | | | `- Abeb | | | `- ABB | `- BB | + - BBA | `- BBBB  

If this is something you are describing, then your tree should not be suitable for the type.

Update: This is a free version of Expand , if you are in this type of thing:

  extension qx = liftM ( (,) X) $ get> & Gt; = Extension: Children check where (first, []) = before returning child return (first, T: _) = true> & Gt; Returns (before ++ [t]) Children = = [extension] hair = _ = check child $ brake (== q) $ children x  

Thanks to the camccann for the control structure. I hope this version is more acceptable.)


Comments