TreeReplacePart

TreeReplacePart[tree,posnew]

gives a tree in which the subtree of tree at the position specified by pos is replaced with new.

TreeReplacePart[tree,{pos1new1,pos2new2,}]

replaces subtrees at positions specified by posi with newi.

TreeReplacePart[tree,{pos1,pos2,}new]

replaces all subtrees at positions specified by posi with new.

TreeReplacePart[tree,{{pos1,1,pos1,2,}new1,}]

replaces subtrees at positions specified by {posi,1,posi,2,} with newi.

TreeReplacePart[posnew]

represents an operator form of TreeReplacePart that can be applied to a tree.

Details

  • TreeReplacePart allows replacement of subtrees with new subtrees or general expressions as leaves at positions matching any pattern in a Tree object. »
  • TreeReplacePart[tree,{i,j,}new] replaces the subtree at position {i,j,} with new.
  • TreeReplacePart can use the position specifications returned by TreePosition as well as extended forms as used in functions such as TreeInsert and TreeExtract. »
  • An individual position specification pos can be given as {part1,part2,}, where part specifications parti include:
  • ithe i^(th) child
    -ithe i^(th) child from the end
    {i1,i2,}the list of children with part numbers i1, i2,
    patternthe children with part number matching patt
    "key"the child whose key is "key"
    Key[k]the child with an arbitrary key k
  • Patterns can include constructs such as __, representing position specifications of variable lengths. »
  • In TreeReplacePart[tree,{p1,p2,}new], {p1,p2,} is interpreted as a list of individual position specifications if all the pi are lists. Otherwise, {p1,p2,} will be taken to be an individual position specification. »
  • TreeReplacePart works on trees containing an Association of subtrees, using the same specification for keys as in Part. »
  • TreeReplacePart[posnew][tree] is equivalent to TreeReplacePart[tree,posnew].
  • TreeReplacePart has the same options as Tree.

Examples

open allclose all

Basic Examples  (5)

Replace a subtree with a leaf:

Replace a subtree at any position:

Replace subtrees at several positions:

Replace each position with a different subtree:

Replace subtrees whose positions match a pattern:

Use the operator form of TreeReplacePart:

Scope  (5)

Associations  (5)

Replace a subtree of a tree containing an association by key:

If the key is a string, the Key wrapper is optional:

Replace a subtree of a tree containing an association by key:

Replace a subtree of a tree containing an association by position:

Replace several subtrees of a tree containing an association:

Replace a nested subtree of a tree containing an association:

Replace several nested subtrees of a tree containing an association:

Properties & Relations  (11)

Patterns can represent part lists of variable length:

Replacing a subtree with an expression other than a Tree object is equivalent to inserting a leaf:

The expression x is inserted as the leaf

:

Replace a subtree with several subtrees using Splice:

Subtrees can be replaced, as well as inserted or deleted:

Construct a tree from the heads in an expression:

TreeReplacePart replaces a subtree of a tree:

This corresponds to replacing a subexpression of an expression:

ReplacePart can replace subexpressions directly:

If pos is not a list, pos and {pos} are equivalent specifications:

A pattern p that is not explicitly a list matches against the immediate children:

In that case, p is equivalent to {p}:

Replacing position {} replaces the whole tree:

TreeReplacePart uses rules in the order given:

TreeReplacePart can use the lists of positions returned by TreePosition:

These are the subtrees returned by TreeExtract:

TreeReplacePart[tree,{p1,p2,}new] treats {p1,p2,} as a list of individual position specifications if all the pi are lists:

For {{1,2},{3,4}}, the subtrees at positions {1,2} and {3,4} are replaced:

If any pi is not a list, {p1,p2,} is treated as a list of part specifications:

For {{{1,2},{3,4}}}, parts 3 and 4 of parts 1 and 2 are replaced:

Possible Issues  (2)

TreeReplacePart only affects positions that are already present:

TreeReplacePart[tree,{}new] does not replace any subtrees because the list of positions is empty:

TreeReplacePart[tree,{{}}new] replaces position {}, which corresponds to the whole tree:

Wolfram Research (2021), TreeReplacePart, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeReplacePart.html (updated 2023).

Text

Wolfram Research (2021), TreeReplacePart, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeReplacePart.html (updated 2023).

CMS

Wolfram Language. 2021. "TreeReplacePart." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2023. https://reference.wolfram.com/language/ref/TreeReplacePart.html.

APA

Wolfram Language. (2021). TreeReplacePart. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TreeReplacePart.html

BibTeX

@misc{reference.wolfram_2024_treereplacepart, author="Wolfram Research", title="{TreeReplacePart}", year="2023", howpublished="\url{https://reference.wolfram.com/language/ref/TreeReplacePart.html}", note=[Accessed: 25-April-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_treereplacepart, organization={Wolfram Research}, title={TreeReplacePart}, year={2023}, url={https://reference.wolfram.com/language/ref/TreeReplacePart.html}, note=[Accessed: 25-April-2024 ]}