TreeInsert[tree,child,pos]
inserts child at the position specified by pos in tree.
TreeInsert[tree,child,{pos1,pos2,…}]
inserts child at several positions.
TreeInsert[child,pos]
represents an operator form of TreeInsert that can be applied to a tree.
TreeInsert
TreeInsert[tree,child,pos]
inserts child at the position specified by pos in tree.
TreeInsert[tree,child,{pos1,pos2,…}]
inserts child at several positions.
TreeInsert[child,pos]
represents an operator form of TreeInsert that can be applied to a tree.
Details and Options
- TreeInsert allows insertion of subtrees or general expressions as leaves at any position in a Tree object. »
- TreeInsert[tree,child,{i,j,…}] inserts the subtree child at position {i,j,…}.
- TreeInsert can use the position specifications returned by TreePosition as well as extended forms as used in functions such as TreeExtract and TreeReplacePart. »
- 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, … "key" the child whose key is "key" Key[k] the child with an arbitrary key k - In TreeInsert[tree,child,{p1,p2,…}], {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. »
- TreeInsert works on trees containing an Association of subtrees, using the same specification for keys as in Part. »
- If tree contains an Association of subtrees, TreeInsert[tree,keyvalue,pos] inserts a subtree in the association.
- If the specified position is of the form "key" or Key[k], then TreeInsert inserts a new subtree before the one corresponding to this key, or overwrites the value if this key is already present.
- If the key of the subtree to be inserted is already present, then the old subtree with the same key is deleted.
- TreeInsert[child,pos][tree] is equivalent to TreeInsert[tree,child,pos].
- TreeInsert 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 (3)
Insert a leaf with the given data into a tree:
TreeInsert[[image], xx, {2}]Insert a subtree at any position:
TreeInsert[[image], [image], {2, 1}]TreeInsert[%, xx, {{1}, {2}, {3}}]Use the operator form of TreeInsert:
TreeInsert[xx, -1][[image]]Scope (7)
Associations (7)
TreeInsert[[image], c -> 3, Key[a]]TreeInsert[[image], {c -> 3, d -> 4}, Key[a]]Inserting subtrees with repeating keys overrides earlier values:
TreeInsert[[image], a -> 3, Key[a]]Insert a subtree into a tree containing an association by key:
TreeInsert[[image], c -> 3, {Key[expr]}]If the key is a string, the Key wrapper is optional:
TreeInsert[[image], c -> 3, {"str"}]Insert a subtree into a tree containing an association by key:
TreeInsert[[image], c -> 3, {Key[1]}]Insert a subtree into a tree containing an association by position:
TreeInsert[[image], c -> 3, {1}]Insert a subtree at a nested position in a tree containing an association:
TreeInsert[[image], [image], {Key[3], 1}]Insert a subtree at several nested positions in a tree containing an association:
TreeInsert[[image], [image], {{Key[3], 1}, {3, 1}}]Properties & Relations (9)
Inserting an expression other than a Tree object is equivalent to inserting a leaf:
TreeInsert[[image], x, -3]The expression x is inserted as the leaf
:
TreeInsert[[image], [image], -3]Insert several subtrees using Splice:
TreeInsert[[image], Splice[{1, 2, 3, 4}], 2]Subtrees can be inserted, as well as replaced or deleted:
TreeInsert[[image], [image], 2]TreeReplacePart[[image], 2 -> [image]]TreeDelete[[image], 2]Construct a tree from the heads in an expression:
ExpressionTree[1[2[3, 4], 5[6, 7]], "Heads"]TreeInsert inserts a subtree into a tree:
TreeInsert[%, x, {1}]This corresponds to inserting a subexpression into an expression:
TreeExpression[%, "Heads"]Insert can insert subexpressions directly:
Insert[1[2[3, 4], 5[6, 7]], x, {1}]For a tree with n children, there are n+1 available positions to insert a new child:
TreeInsert[[image], [image], {{1}, {2}, {3}, {4}, {5}}]If pos is not a list, pos and {pos} are equivalent specifications:
tree = [image];TreeInsert[tree, x, 3] === TreeInsert[tree, x, {3}]TreeInsert inserts the new subtree repeatedly if a position is mentioned repeatedly:
TreeInsert[[image], [image], {{1}, {3}, {1}}]TreeInsert can use the lists of positions returned by TreePosition:
tree = NestTree[Range[3# - 1, 3# + 1]&, 1, 2]pos = TreePosition[tree, _ ? PrimeQ]TreeInsert[tree, [image], pos]These are the subtrees returned by TreeExtract:
TreeExtract[tree, pos]TreeInsert[tree,new,{p1,p2,…}] treats {p1,p2,…} as a list of individual position specifications if all the pi are lists:
tree = [image];For {{1,2},{3,4}}, the new subtree is inserted at positions {1,2} and {3,4}:
TreeInsert[tree, [image], {{1, 2}, {3, 4}}]If any pi is not a list, {p1,p2,…} is treated as a list of part specifications:
TreeInsert[tree, [image], {{1, 2}, 3}]TreeInsert[tree, [image], {1, {3, 4}}]For {{{1,2},{3,4}}}, the new subtree is inserted at parts 3 and 4 of parts 1 and 2:
TreeInsert[tree, [image], {{{1, 2}, {3, 4}}}]Possible Issues (3)
TreeInsert[tree,new,{}] does not insert any subtrees because the list of positions is empty:
TreeInsert[[image], x, {}]Subtrees cannot be inserted at the root:
TreeInsert[[image], x, {{}}]Subtrees cannot be inserted into a leaf:
TreeInsert[[image], [image], {1}]For an inner node with no children, a child can be inserted at position 1:
TreeInsert[[image], [image], {1}]Related Guides
Text
Wolfram Research (2021), TreeInsert, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeInsert.html (updated 2024).
CMS
Wolfram Language. 2021. "TreeInsert." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2024. https://reference.wolfram.com/language/ref/TreeInsert.html.
APA
Wolfram Language. (2021). TreeInsert. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TreeInsert.html
BibTeX
@misc{reference.wolfram_2026_treeinsert, author="Wolfram Research", title="{TreeInsert}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/TreeInsert.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_treeinsert, organization={Wolfram Research}, title={TreeInsert}, year={2024}, url={https://reference.wolfram.com/language/ref/TreeInsert.html}, note=[Accessed: 13-June-2026]}