I have a tree represented by
type LazyTree<'T> =
| LazyBranch of 'T * ('T LazyTree list Lazy)
I want to assign a number n
to each node, counting left to right.
More formally
Edited:
For a node a
, h(a)=0
if a
has no children
For a node a
with parent p
, h(a)=h(p+1)
For a node a
, without parent, L(a)
is an empty set.
For a node a
, which has a parent, L(a)
is a set of all nodes where for each node i
, path from root to it doesn't contain a
For a node a
, S(a)
is all elements of L(a)
, where for each element i
holds h(i)<=h(a)
I need to replace value of each node a
with size of S(a)
I cannot come up with a solution for my problem which does not involve side-effects.
My function should return a tree.
Its node is dependent on node of left sibling. So, the function should get a 'T LazyTree Option
as a parameter.
But its leftmost child is dependent on our rightmost sibling which is dependent on us. It seems, we got an infinite cycle.
I'm not asking how to solve it on finite tree.
I'm not asking about an abstract concept. Here is how can I return one infinite tree from another:
type LazyTree<'T> =
| LazyBranch of 'T * ('T LazyTree list Lazy)
with
member this.Item = match this with LazyBranch(item,_) -> item
member this.Children = match this with LazyBranch(_,children) -> children
member this.Map func =
let children =
lazy
(
this.Children.Force()
|> List.map (fun child -> child.Map func)
)
LazyBranch(func this.Item, children)