
Linked Trees in a functional language - giuliohome
I&#x27;m reading the first paragraph of https:&#x2F;&#x2F;johnazariah.github.io&#x2F;&#x2F;blog&#x2F;2017&#x2F;12-23-fun-with-trees&#x2F;index.html
But I believe that parent linked trees are really needed in real world e.g. to get the path of a node. I think this use case is missing. There must be a simple sort of link to the parent tags.
I&#x27;d suggest<p><pre><code>  type Tags&lt;&#x27;a&gt; = {
    Tag : &#x27;a
    ParentTag : Tags&lt;&#x27;a&gt; option 
  }
  type Tree&lt;&#x27;a&gt; = {
    Tags : Tags&lt;&#x27;a&gt; 
    Children : Tree&lt;&#x27;a&gt; list
  }
</code></pre>
With the path helper<p><pre><code>  let rec getPath (node:Tags&lt;&#x27;a&gt;) : &#x27;a list =
    match node.ParentTag with
    | None -&gt; [node.Tag]
    | Some parent -&gt; 
        List.append (getPath parent) [node.Tag] 

</code></pre>
What&#x27;s your opinion? Thanks
======
giuliohome
Notice that you can add all the content, with whatever non-unique attribute
inside the Tags type;

plus the link to access the parent's parent content and so on... by
reproducing the whole tree structure backwards

Finally consider that getPath is not doing a lookup by id but it is really
_directly_ accessing the parent node content and up to the root, immediately.

What you can't do this way is that you can't go through the other children of
the parent (and all the rest of the tree structure that is not straight up in
the hierarchy), but that's another story: it would need another use case to be
introduced and discussed - in the first place

------
dozzie
> But I believe that parent linked trees are really needed in real world e.g.
> to get the path of a node.

Yeah, it would be about that: _belief_. You can remember the path to the root
when descending, so the parent node is not really necessary.

~~~
giuliohome
For example I develop software for trading applications. Let's say I have a
trade with its portfolio (an attribute). I want to get the full path of such
portfolio (see it like a floder/filename tree structure). I'm not descending
anything, I just need backward propagation...

And also I don't want to "remember" the parent path (let's say a long string)
by duplicating it in memory for each of its children...

