A version of bottomup
that doesn't traverse bridges.
The basic relations between a node and its children.
The basic relations between a node and its children. All of the
other relations are derived from child
. If this tree's shape
argument is CheckTree
then a check will be performed that the
structure is actually a tree. A runtime error will be thrown
if it's not a tree.
A version of everywherebu
that doesn't traverse bridges.
A relation that relates a node to its first child.
Return the first index of t
in the children of t's
parent node.
Return the first index of t
in the children of t's
parent node.
Counts from zero. Is zero for root.
Return the last index of t
in the children of t's
parent node.
Return the last index of t
in the children of t's
parent node.
Counts from zero. Is zero for root.
Return whether or not t
is a first child.
Return whether or not t
is a first child. True for root.
Return whether or not t
is a last child.
Return whether or not t
is a last child. True for root.
Return whether or not t
is the root of this tree.
A relation that relates a node to its last child.
Map the function f
over the images of this tree's child relation and
use the resulting graph to make a new tree relation.
A relation that relates a node to its next sibling.
A relation that relates a node to its next sibling. Inverse of
the prev
relation.
The nodes that occur in this tree.
The nodes that occur in this tree. Mostly useful if you want to iterate to look at every node.
The parent relation for this tree (inverse of child relation).
A relation that relates a node to its previous sibling.
A relation that relates a node to its previous sibling. Inverse of the
next
relation.
The root node of the tree.
The root node of the tree. If this tree's shape
argument is EnsureTree
the root node will be different from the original root if any nodes in
the original tree are shared, since they will be cloned as necessary
to yield a proper tree structure. If there is no sharing or EnsureTree
is not specified then root
will be same as originalRoot
.
Bridges to other structures will not be traversed. This behaviour means that you can have references to other structures while still processing this structure as a tree in its own right.
A relation that relates a node to its siblings, including itself.
If the tree contains node u
return v
, otherwise throw a
NodeNotInTreeException
.
If the tree contains node u
return v
, otherwise throw a
NodeNotInTreeException
. v
is only evaluated if necessary.
Relational representations of trees built out of hierarchical
Product
instances. Typically, the nodes are instances of case classes.The type
T
is the common base type of the tree nodes. The typeR
is a sub-type ofT
that is the type of the root node.The
originalRoot
value should be the root of a finite, acyclic structure that contains onlyProduct
instances of typeT
(unless they are skipped, see below).The
shape
argument governs how the structure referred to byoriginalRoot
is treated. Ifshape
isLeaveAlone
(the default) then the structure is used without change. Ifshape
isCheckTree
then a check is run before the structure is used to make sure that it is a tree (i.e., no node sharing nor cycles). A runtime error occurs if this is not the case. Ifshape
isEnsureTree
then shared parts of the structure are cloned to ensure that it is a tree before it is used.If you are confident that your structure is a tree (e.g., it comes from a parser) then the default shape should be fine. If you prefer a bit more safety, then use
CheckTree
so that a non-tree structure cannot be missed. Finally, useEnsureTree
if you know that your structure may contain sharing (e.g., it is produced by a term rewriting process).The
child
relation of a tree is defined to skip certain nodes. Specifically, if a node of typeT
is wrapped in aSome
of an option, aLeft
orRight
of anEither
, a tuple up to size four, or aTraversableOnce
, then those wrappers are ignored. E.g., ift1
isSome (t2)
where botht1
andt2
are of typeT
, thent1
will bet2
's parent, not theSome
value.Thanks to Len Hamey for the idea to use lazy cloning to restore the tree structure instead of requiring that the input trees contain no sharing.