Monday 15 June 2015

recursion - Build a tree from a list of node, parent elements -


I have a list of nodes, with each parent and I want to create one of these trees .

  (def elems' [{node A: parent nil} {: node b: guardian a} {: node c: parent a} {: node d: guardian c}]) Build-Tree Ames) = & gt; (A (B) (C (D))   

I currently have this code:

  (def-root-node [elems] ( : Node (first: remove: guardian hand)) (Suspending children [Ames route] (Map: node (filter # (= root (: original%)) AIIMS)) - Make sub-tree [Ams root -Node] Definition of build-tree [AIIMS] (make-sub-tree AIIMS (root- node) (conj (map #)% (hands-node to children), root-node) Node ames)))   

In this solution, recycling is used, but loop It is not bad with syntax syntax, which is bad because the code can not be optimized and A. It seems that I can use it twice only if I have a recursive in each step. In the case of a tree, I am the child of a recurring one node for each.

I am looking for an adjusted solution that will not run in this problem.
If you have this problem If I have a complete solution, I would love to see it.
I read something about zipper, perhaps it is a better way of building a tree.

This is the solution I would go with. It is still susceptible to a StackOverflowError, but only for "tall" trees. (Map: F-AC): [ACC [node parent]]] (Update-AC [ Original] VC-cone node reduction (AIIMS) construction-tree (FN cut-tree [node] (Bars node) tree (adjusted map node)) tree (tree-building tree)] (Emphasis (= (tree tree) 2) "should only be a root node") (second tree))

We can remove the StackOverflowError problem, but to do so There is some pain for everyone. Rather than processing the cards immediately with the construct-tree , we can indicate that more work has been done to do this (such as the zero RGR function), then take action on each of them There is a continuous processing until another step of processing, unless a work is left to work, it will be possible to do this consistently in stack space, but unless you actually get tall trees Id are not, it is probably unnecessary (even clojure.walk / prewalk and Postvok pile on a tall tree).

No comments:

Post a Comment