Tree traversal: Difference between revisions

Content added Content deleted
Line 2,512: Line 2,512:


data=:3 :'data[''data parent''=. y'
data=:3 :'data[''data parent''=. y'
parent=:3 :'parent[''data parent''=. y'</lang>
parent=:3 :'parent[''data parent''=. y'

childinds=: [: <:@(2&{.@-.&> #\) (</. #\)`(]~.)`(a:"0)}~</lang>


Here, <code>data</code> extracts the list of data items from the tree and <code>parent</code> extracts the structure from the tree.
Here, <code>data</code> extracts the list of data items from the tree and <code>parent</code> extracts the structure from the tree.
Line 2,519: Line 2,521:


<code>reorder</code> is like indexing, except that it returns an equivalent tree (with the structural elements updated to maintain the original tree structure). The left argument for reorder should select the entire tree. Selecting partial trees is a more complex problem which needs specifications about how to deal with issues such as dangling roots and multiple roots. (Our abstraction here has no problem ''representing'' trees with multiple roots, but they are not relevant to this task.)
<code>reorder</code> is like indexing, except that it returns an equivalent tree (with the structural elements updated to maintain the original tree structure). The left argument for reorder should select the entire tree. Selecting partial trees is a more complex problem which needs specifications about how to deal with issues such as dangling roots and multiple roots. (Our abstraction here has no problem ''representing'' trees with multiple roots, but they are not relevant to this task.)

<code>childinds</code> extracts the child pointers which some of these results assume.


Next, we define our "traversal" routines (actually, we are going a bit overboard here - we really only need to extract the data for this tasks's concept of traversal):
Next, we define our "traversal" routines (actually, we are going a bit overboard here - we really only need to extract the data for this tasks's concept of traversal):