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.
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 and Options
- 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:
-
i the i
child-i the i
child from the end{i1,i2,…} the list of children with part numbers i1, i2, … pattern the 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.
-
AlignmentPoint Center the default point in the graphic to align with AspectRatio Automatic ratio of height to width Axes False whether to draw axes AxesLabel None axes labels AxesOrigin Automatic where axes should cross AxesStyle {} style specifications for the axes Background None background color for the plot BaselinePosition Automatic how to align with a surrounding text baseline BaseStyle {} base style specifications for the graphic ContentSelectable Automatic whether to allow contents to be selected CoordinatesToolOptions Automatic detailed behavior of the coordinates tool Epilog {} primitives rendered after the main plot FormatType TraditionalForm the default format type for text Frame False whether to put a frame around the plot FrameLabel None frame labels FrameStyle {} style specifications for the frame FrameTicks Automatic frame ticks FrameTicksStyle {} style specifications for frame ticks GridLines None grid lines to draw GridLinesStyle {} style specifications for grid lines ImageMargins 0. the margins to leave around the graphic ImagePadding All what extra padding to allow for labels etc. ImageSize Automatic the absolute size at which to render the graphic LabelStyle {} style specifications for labels Method Automatic details of graphics methods to use PlotLabel None an overall label for the plot PlotRange All range of values to include PlotRangeClipping False whether to clip at the plot range PlotRangePadding Automatic how much to pad the range of values PlotRegion Automatic the final display region to be filled PreserveImageOptions Automatic whether to preserve image options when displaying new versions of the same graphic Prolog {} primitives rendered before the main plot RotateLabel True whether to rotate y labels on the frame Ticks Automatic axes ticks TicksStyle {} style specifications for axes ticks
List of all options
Examples
open all close allBasic Examples (5)
Replace a subtree with a leaf:
TreeReplacePart[[image], {2} -> xx]Replace a subtree at any position:
TreeReplacePart[[image], {2, 1} -> [image]]Replace subtrees at several positions:
TreeReplacePart[[image], {{1}, {3}, {5}} -> xx]Replace each position with a different subtree:
TreeReplacePart[[image], {{1} -> xx, {3} -> yy, {5} -> zz}]Replace subtrees whose positions match a pattern:
TreeReplacePart[[image], {i_, i_} :> i]Use the operator form of TreeReplacePart:
TreeReplacePart[-1 -> xx][[image]]Scope (5)
Associations (5)
Replace a subtree of a tree containing an association by key:
```TreeReplacePart[[image], {Key[expr]} -> 3]If the key is a string, the Key wrapper is optional:
TreeReplacePart[[image], {"str"} -> 3]Replace a subtree of a tree containing an association by key:
TreeReplacePart[[image], {Key[1]} -> 3]Replace a subtree of a tree containing an association by position:
TreeReplacePart[[image], {1} -> 3]Replace several subtrees of a tree containing an association:
TreeReplacePart[[image], {{1}, {Key[1]}} -> 3]Replace a nested subtree of a tree containing an association:
TreeReplacePart[[image], {Key[3], 1} -> 3]Replace several nested subtrees of a tree containing an association:
TreeReplacePart[[image], {{Key[3], 1}, {3, 1}} -> 3]Properties & Relations (11)
Patterns can represent part lists of variable length:
TreeReplacePart[[image], {___, 2, ___} -> x]Replacing a subtree with an expression other than a Tree object is equivalent to inserting a leaf:
TreeReplacePart[[image], -3 -> x]The expression x is inserted as the leaf
:TreeReplacePart[[image], -3 -> [image]]Replace a subtree with several subtrees using Splice:
TreeReplacePart[[image], 2 -> Splice[{1, 2, 3, 4}]]Subtrees can be replaced, as well as inserted or deleted:
TreeReplacePart[[image], 2 -> [image]]TreeInsert[[image], [image], 2]TreeDelete[[image], 2]Construct a tree from the heads in an expression:
ExpressionTree[1[2[3, 4], 5[6, 7]], "Heads"]TreeReplacePart replaces a subtree of a tree:
TreeReplacePart[%, {1} -> x]This corresponds to replacing a subexpression of an expression:
TreeExpression[%, "Heads"]ReplacePart can replace subexpressions directly:
ReplacePart[1[2[3, 4], 5[6, 7]], {1} -> x]If pos is not a list, pos and {pos} are equivalent specifications:
tree = [image];TreeReplacePart[tree, 3 -> x] === TreeReplacePart[tree, {3} -> x]A pattern p that is not explicitly a list matches against the immediate children:
TreeReplacePart[[image], _ ? EvenQ -> [image]]In that case, p is equivalent to {p}:
TreeReplacePart[[image], {_ ? EvenQ} -> [image]]Replacing position {} replaces the whole tree:
TreeReplacePart[[image], {{}} -> x]TreeReplacePart uses rules in the order given:
TreeReplacePart[[image], {1 -> x, 1 -> y}]TreeReplacePart can use the lists of positions returned by TreePosition:
tree = NestTree[Range[3# - 1, 3# + 1]&, 1, 2]pos = TreePosition[tree, _ ? PrimeQ]TreeReplacePart[tree, pos -> [image]]These are the subtrees returned by TreeExtract:
TreeExtract[tree, pos]TreeReplacePart[tree,{p1,p2,…}new] treats {p1,p2,…} as a list of individual position specifications if all the pi are lists:
tree = [image];For {{1,2},{3,4}}, the subtrees at positions {1,2} and {3,4} are replaced:
TreeReplacePart[tree, {{1, 2}, {3, 4}} -> [image]]If any pi is not a list, {p1,p2,…} is treated as a list of part specifications:
TreeReplacePart[tree, {{1, 2}, 3} -> [image]]TreeReplacePart[tree, {1, {3, 4}} -> [image]]For {{{1,2},{3,4}}}, parts 3 and 4 of parts 1 and 2 are replaced:
TreeReplacePart[tree, {{{1, 2}, {3, 4}}} -> [image]]Possible Issues (2)
TreeReplacePart only affects positions that are already present:
TreeReplacePart[[image], 5 -> x]TreeReplacePart[tree,{}new] does not replace any subtrees because the list of positions is empty:
TreeReplacePart[[image], {} -> x]TreeReplacePart[tree,{{}}new] replaces position {}, which corresponds to the whole tree:
TreeReplacePart[[image], {{}} -> x]Related Guides
Text
Wolfram Research (2021), TreeReplacePart, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeReplacePart.html (updated 2024).
CMS
Wolfram Language. 2021. "TreeReplacePart." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2024. 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_2026_treereplacepart, author="Wolfram Research", title="{TreeReplacePart}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/TreeReplacePart.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_treereplacepart, organization={Wolfram Research}, title={TreeReplacePart}, year={2024}, url={https://reference.wolfram.com/language/ref/TreeReplacePart.html}, note=[Accessed: 13-June-2026]}