TreeGraph
更多信息和选项
- TreeGraph 生成 Graph 对象.
- TreeGraph 支持与 Graph 相同的顶点、边、封装和选项.
- 树是没有圈的简单连通图.
- 树图是没有圈的连通图.
- TreeGraph 只能表示树.
所有选项的列表
范例
打开所有单元 关闭所有单元基本范例 (1)
范围 (25)
连通性 (6)
TreeGraph[{12, 13, 14}]TreeGraph[{12, 13, 14}]TreeGraph[{1 -> 2, 1 -> 3, 1 -> 4}]TreeGraph[{1 -> 2, 1 -> 3, 1 -> 4}, DirectedEdges -> False]使用 VertexList 和 EdgeList 以获取顶点和边:
{g1, g2} = {TreeGraph[{12, 13, 14}], TreeGraph[{13, 12, 14}]}EdgeList /@ {g1, g2}VertexList /@ {g1, g2}利用显式顶点列表,控制 VertexList 所用的排序:
{g1, g2} = {TreeGraph[{1, 2, 3, 4}, {13, 12, 14}], TreeGraph[{3, 2, 1, 4}, {13, 12, 14}]}VertexList /@ {g1, g2}{TreeGraph[{ab, ac}], TreeGraph[{"foo""bar", "foo""gnu"}],
TreeGraph[{[image][image], [image][image]}]}VertexList /@ %封装 (5)
{TreeGraph[{Style[1, Red], 2, 3, 4}, {12, 13, 14}], TreeGraph[{1, 2, 3, 4}, {Style[12, Red], 13, 14}]}{TreeGraph[{Labeled[Style[1, Red], "hello"], 2, 3, 4}, {12, 13, 14}], TreeGraph[{1, 2, 3, 4}, {Style[Labeled[12, "hello"], Red], 13, 14}]}通过封装如 Tooltip,添加交互式行为:
TreeGraph[{12, 13, Tooltip[Style[14, Red], "hello"]}]TreeGraph[{12, 13, Tooltip[Style[14, Red], Plot[Sin[x], {x, 0, 2Pi}]]}]利用 Button 当点击一条边或者一个顶点时触发动作:
{TreeGraph[{12, Button[Style[13, Red], Speak["Edge from 1 to 3"]]}], TreeGraph[{1, 2, Button[Style[3, Red], Speak["Vertex 3"]]}, {12, 23}, VertexSize -> Medium]}利用 PopupWindow 提供进一步的详细信息:
TreeGraph[{12, 13, PopupWindow[Style[14, Red], DateListPlot[FinancialData["IBM", "Jan. 1, 2004"]]]}]样式化 (8)
{TreeGraph[{12, 13, 14}, VertexStyle -> Green], TreeGraph[{12, 13, 14}, EdgeStyle -> Green]}{TreeGraph[{12, 13, 14}, VertexStyle -> {1 -> Green, Orange}], TreeGraph[{12, 13, 14}, EdgeStyle -> {12 -> Green, Orange}]}{TreeGraph[{Style[1, Green], 2, 3}, {12, 13, 14}, VertexStyle -> Orange], TreeGraph[{Style[12, Green], 13, 14}, EdgeStyle -> Orange]}Table[TreeGraph[{12, 13, 14}, VertexSize -> s, PlotLabel -> s], {s, {Tiny, Small, Medium, Large}}]Table[TreeGraph[{12, 13, 14}, VertexSize -> s, PlotLabel -> s], {s, {0.1, 0.2, 0.5, 1}}]使用 VertexShapeFunction 的内置设置:
Table[TreeGraph[{12, 13, 14}, VertexShapeFunction -> s, VertexSize -> 0.2, PlotLabel -> s], {s, {"Triangle", "Square", "Rectangle", "Pentagon", "Hexagon", "Octagon"}}]Table[TreeGraph[{12, 13, 14}, VertexShapeFunction -> s, VertexSize -> 0.2, PlotLabel -> s], {s, ResourceData["VertexShapeFunction", "Rounded"]}]Table[TreeGraph[{12, 13, 14}, VertexShapeFunction -> s, VertexSize -> 0.2, PlotLabel -> s], {s, ResourceData["VertexShapeFunction", "Concave"]}]TreeGraph[{12, 13, 14}, VertexShapeFunction -> { 1 -> "Square"}, VertexSize -> 0.2]TreeGraph[{12, 13, 14}, VertexShapeFunction -> { 1 -> "Square", "Triangle"}, VertexSize -> 0.2]将任意 Graphics、Image 或者 Graphics3D 作为顶点形状使用:
Table[TreeGraph[{12, 13, 14}, VertexShape -> s, VertexSize -> Medium], {s, {[image], [image], [image]}}]使用 EdgeShapeFunction 的内置设置:
Table[TreeGraph[{12, 13, 14}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, {"BoxLine", "DiamondLine", "DotLine"}}]Table[TreeGraph[{12, 13, 14}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, ResourceData["EdgeShapeFunction", "FilledArrow"]}]Table[TreeGraph[{12, 13, 14}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, ResourceData["EdgeShapeFunction", "UnfilledArrow"]}]Table[TreeGraph[{12, 13, 14}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, ResourceData["EdgeShapeFunction", "CarvedArrow"]}]TreeGraph[{12, 13, 14}, EdgeShapeFunction -> {12 -> "UnfilledArrow"}]TreeGraph[{12, 13, 14}, EdgeShapeFunction -> {12 -> "FilledArrow", "CarvedArrow"}]标签 (6)
{TreeGraph[{Labeled[12, "hello"], 13, 14}], TreeGraph[{Labeled[1, "hello"], 2, 3}, {12, 13, 14}]}Table[TreeGraph[{1, 2, Labeled[3, l]}, {12, 13, 14}], {l, {Sin[x], [image], [image]}}]Table[TreeGraph[{12, 13, Labeled[14, l]}], {l, {Sin[x], [image], [image]}}]使用 Placed 控制顶点标签的位置,包括位置内的符号:
Table[TreeGraph[{1, 2, Labeled[3, Placed["■■", p]]}, {12, 23}, PlotLabel -> p, VertexSize -> 0.25, VertexShapeFunction -> "Square"], {p, {Left, Right, Top, Bottom}}]Table[TreeGraph[{1, 2, Labeled[3, Placed["■■", p]]}, {12, 23}, PlotLabel -> p, VertexSize -> Small, VertexShapeFunction -> "Square"], {p, {Before, After, Below, Above}}]Table[TreeGraph[{1, 2, Labeled[3, Placed["■", p]]}, {12, 13}, VertexSize -> 0.25, VertexShapeFunction -> "Square", PlotLabel -> p, BaselinePosition -> Bottom], {p, {{0, 0}, {1 / 2, 1 / 2}, {1, 1}}}]使用 Placed 在封装中放置多个标签:
TreeGraph[{1, 2, 3, Labeled[4, Placed[{"lbl1", "lbl2"}, {Above, Below}]]}, {12, 13, 14}]TreeGraph[{1, 2, 3, Labeled[4, Placed[{"lbl1", "lbl2", "lbl3", "lbl4"}, {Above, After, Below, Before}]]}, {12, 13, 14}]使用 VertexLabels 放置多个标签:
TreeGraph[{12, 13, 14}, VertexLabels -> {4 -> Placed[{"lbl1", "lbl2"}, {Above, Below}]}]使用具有符号位置的 Placed 控制边上的标签位置:
Table[TreeGraph[{Labeled[12, Placed["■■", p]], 13}, PlotLabel -> p, VertexSize -> Small], {p, {"Start", "Middle", "End"}}]Table[TreeGraph[{Labeled[12, Placed["■■", p]], 13}, PlotLabel -> p, VertexSize -> Small, BaselinePosition -> Bottom], {p, {0, 1 / 4, 1 / 3}}]使用 Placed 在一个封装内放置多个标签:
TreeGraph[{12, 13, Labeled[14, Placed[{"lbl1", "lbl2"}, {"Start", "End"}]]}]TreeGraph[{12, 13, Labeled[14, Placed[{"lbl1", "lbl2", "lbl3"}, {"Start", "Middle", "End"}]]}]使用 EdgeLabels 放置多个标签:
TreeGraph[{12, 13, 14}, EdgeLabels -> {14 -> Placed[{"lbl1", "lbl2"}, {"Start", "End"}]}]选项 (82)
AnnotationRules (3)
TreeGraph[{12, 13, 14}, AnnotationRules -> {1 -> {VertexLabels -> "hello"}}]TreeGraph[{12, 13, 14}, AnnotationRules -> {12 -> {EdgeLabels -> "hello"}}]TreeGraph[{12, 13, 14}, AnnotationRules -> {"GraphProperties" -> {"Message" -> "hello"}}]AnnotationValue[%, "Message"]DirectedEdges (2)
TreeGraph[{1 -> 2, 1 -> 3, 1 -> 4}, EdgeStyle -> Arrowheads[Medium], EdgeShapeFunction -> "Arrow"]使用 DirectedEdges->False 将规则解释为无向边:
TreeGraph[{1 -> 2, 1 -> 3, 1 -> 4}, DirectedEdges -> False]使用 DirectedEdge 或 UndirectedEdge 来直接指定图是有向还是无向:
{TreeGraph[{12, 13, 14}, EdgeStyle -> Arrowheads[Medium], EdgeShapeFunction -> "Arrow"], TreeGraph[{12, 13, 14}]}EdgeLabels (7)
TreeGraph[{12, 13, 14}, EdgeLabels -> {12 -> "Hello"}]el = {12, 13, 14};TreeGraph[el, EdgeLabels -> Table[el[[i]] -> Subscript["e", i], {i, Length[el]}]]TreeGraph[{12, 13, 14}, EdgeLabels -> {12 -> [image], 13 -> [image], 14 -> [image]}]使用具有符号位置信息的 Placed 来控制边上标签的位置:
Table[TreeGraph[{12, 13, 14}, EdgeLabels -> {12 -> Placed["■■■", p]}, PlotLabel -> p], {p, {"Start", "Middle", "End"}}]Table[TreeGraph[{12, 13, 14}, EdgeLabels -> {12 -> Placed["■■■", p]}, PlotLabel -> p, BaselinePosition -> Bottom], {p, {0, 1 / 3, 1 / 4}}]Table[TreeGraph[{12, 13, 14}, EdgeLabels -> {12 -> Placed["■■■", {1 / 2, p}]}, PlotLabel -> p, BaselinePosition -> Bottom], {p, {{0, 0}, {1 / 2, 1 / 2}, {1, 1}}}]将 Placed 用于封装内以放置多个标签:
TreeGraph[{12, 13, Labeled[14, Placed[{"lbl1", "lbl2"}, {"Start", "End"}]]}]TreeGraph[{12, 13, Labeled[14, Placed[{"lbl1", "lbl2", "lbl3"}, {"Start", "Middle", "End"}]]}]使用 EdgeLabels 放置多个标签:
TreeGraph[{12, 13, 14}, EdgeLabels -> {14 -> Placed[{"lbl1", "lbl2"}, {"Start", "End"}]}]通过 Tooltip 和 StatusArea 的值来使用自动标签:
TreeGraph[{12, 13, 14}, EdgeLabels -> Placed["Name", Tooltip]]TreeGraph[{12, 13, 14}, EdgeLabels -> Placed["Name", StatusArea]]EdgeShapeFunction (6)
得到 EdgeShapeFunction 的内置设置列表:
ResourceData["EdgeShapeFunction"]TreeGraph[{12, 13, 14}, EdgeShapeFunction -> "Line"]Table[TreeGraph[{12, 13, 14}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, {"BoxLine", "DiamondLine", "DotLine"}}]Table[TreeGraph[{12, 13, 14}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, ResourceData["EdgeShapeFunction", "FilledArrow"]}]Table[TreeGraph[{12, 13, 14}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, ResourceData["EdgeShapeFunction", "UnfilledArrow"]}]Table[TreeGraph[{12, 13, 14}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, ResourceData["EdgeShapeFunction", "CarvedArrow"]}]TreeGraph[{12, 13, 14}, EdgeShapeFunction -> {12 -> "FilledArcArrow"}]TreeGraph[{12, 13, 14}, EdgeShapeFunction -> {12 -> "FilledArcArrow", "CarvedArrow"}]ef[pts_List, e_] :=
Block[{s = 0.015, g = [image]}, {Arrowheads[{{s, 0.33, g}, {s, 0.67, g}}], Arrow[pts]}]TreeGraph[{12, 13, 14}, EdgeShapeFunction -> ef]EdgeShapeFunction 可以与 EdgeStyle 组合使用:
TreeGraph[{12, 13, 14}, EdgeStyle -> Blue, EdgeShapeFunction -> (Line[#1]&)]EdgeShapeFunction 具有比 EdgeStyle 更高的优先级:
TreeGraph[{12, 13, 14}, EdgeStyle -> Blue, EdgeShapeFunction -> ({Red, Line[#1]}&)]EdgeStyle (2)
EdgeWeight (2)
TreeGraph[{12, 13, 14}, EdgeWeight -> RandomInteger[5, 3]]WeightedAdjacencyMatrix[%]//MatrixFormTreeGraph[{12, 13, 14}, EdgeWeight -> {a, b, c}]WeightedAdjacencyMatrix[%]//MatrixFormGraphHighlight (3)
TreeGraph[{12, 13, 14}, VertexSize -> Medium, GraphHighlight -> {1}]TreeGraph[{12, 13, 14}, VertexSize -> Tiny, GraphHighlight -> {13}]TreeGraph[{12, 13, 14}, VertexSize -> Tiny, GraphHighlight -> {1, 2, 12, 13}]GraphHighlightStyle (2)
获取 GraphHighlightStyle 的内置设置列表:
ResourceData["GraphHighlightStyle"]使用 GraphHighlightStyle 的内置设置:
TreeGraph[{12, 13, 14}, GraphHighlight -> {1, 13}, VertexSize -> Small, GraphHighlightStyle -> #, PlotLabel -> #]& /@ Select[ResourceData["GraphHighlightStyle"], # =!= Automatic&]GraphLayout (5)
TreeGraph[{12, 13, 14}, GraphLayout -> Automatic]Table[TreeGraph[Table[ii + 1, {i, 20}], GraphLayout -> l, PlotLabel -> l], {l, {"CircularEmbedding", "SpiralEmbedding"}}]el = EdgeList@CompleteKaryTree[4, 5];Table[TreeGraph[el, GraphLayout -> l, PlotLabel -> l], {l, {"SpringEmbedding", "SpringElectricalEmbedding", "HighDimensionalEmbedding"}}]VertexCoordinates 覆盖 GraphLayout 坐标:
{TreeGraph[{12, 13, 14}, GraphLayout -> "SpringElectricalEmbedding"],
TreeGraph[{12, 13, 14}, GraphLayout -> "SpringElectricalEmbedding", VertexCoordinates -> Table[{i, i}, {i, 0, 3}]]}利用 AbsoluteOptions 来提取使用一个布局算法计算得到的 VertexCoordinates:
TreeGraph[{12, 13, 14}]AbsoluteOptions[%, VertexCoordinates]PlotTheme (4)
基本主题 (2)
VertexCoordinates (3)
TreeGraph[{12, 13, 14}]使用 AbsoluteOptions 提取所得的顶点坐标:
AbsoluteOptions[%, VertexCoordinates]ellipseLayout[n_, {a_, b_}] := Table[{a Cos[2Pi / n u], b Sin[2Pi / n u]}, {u, 1, n}]Graphics[Point[ellipseLayout[20, {2, 1}]]]TreeGraph[Table[1i, {i, 2, 20}], VertexCoordinates -> ellipseLayout[20, {2, 1}]]VertexCoordinates 具有比 GraphLayout 更高的优先级:
TreeGraph[{12, 13, 14}, VertexCoordinates -> Table[{i, i}, {i, 4}], GraphLayout -> "CircularEmbedding"]VertexLabels (13)
TreeGraph[{12, 13, 14}, VertexLabels -> "Name"]TreeGraph[{12, 13, 14}, VertexLabels -> {2 -> "one"}]TreeGraph[{12, 13, 14}, VertexLabels -> Table[i -> Subscript[v, i], {i, 4}]]TreeGraph[{12, 13}, VertexLabels -> {1 -> [image], 2 -> [image], 3 -> [image]}, ImagePadding -> 30]使用具有符号位置信息的 Placed 来控制标签位置,包括外部位置:
Table[TreeGraph[{12, 13, 14}, VertexSize -> 0.1, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed["■■■", p], {i, 4}], PlotLabel -> p], {p, {Before, After, Below, Above}}]pl = {{Before, Below}, {After, Below}, {Before, Above}, {After, Above}};Table[TreeGraph[{12, 13, 14}, VertexSize -> 0.1, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed["■■■", p], {i, 4}], PlotLabel -> p], {p, pl}]Table[TreeGraph[{12, 13, 14}, VertexSize -> 0.35, VertexLabels -> Table[i -> Placed["■■■", p], {i, 4}], VertexShapeFunction -> "Square", PlotLabel -> p], {p, {Left, Top, Right, Bottom}}]pl = {{Left, Bottom}, {Right, Bottom}, {Left, Top}, {Right, Top}};Table[TreeGraph[{12, 13, 14}, VertexSize -> 0.35, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed["■■■", p], {i, 4}], PlotLabel -> p], {p, pl}]Table[TreeGraph[{12, 13, 14}, VertexSize -> 0.25, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed[[image], p], {i, 4}], PlotLabel -> p, BaselinePosition -> Bottom], {p, {{0, 0}, {1 / 2, 1 / 2}, {1, 1}}}]Table[TreeGraph[{12, 13, 14}, VertexSize -> 0.35, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed[[image], {{1, 1}, p}], {i, 4}], PlotLabel -> p, BaselinePosition -> Bottom], {p, {{0, 0}, {1 / 2, 1 / 2}, {1, 1}}}]将 Placed 用于封装内以放置多个标签:
TreeGraph[{1, 2, 3, Labeled[4, Placed[{"lbl1", "lbl2"}, {Above, Below}]]}, {12, 13, 14}]TreeGraph[{1, 2, 3, Labeled[4, Placed[{"lbl1", "lbl2", "lbl3", "lbl4"}, {Above, After, Below, Before}]]}, {12, 13, 14}]使用 VertexLabels 放置多个标签:
TreeGraph[{12, 13, 14}, VertexLabels -> {4 -> Placed[{"lbl1", "lbl2"}, {Above, Below}]}]使用 Placed 的变量来控制格式化,包括 Tooltip:
TreeGraph[{12, 13, 14}, VertexLabels -> Placed["Name", Tooltip]]或者 StatusArea:
TreeGraph[{12, 13, 14}, VertexLabels -> Placed["Name", StatusArea]]rotateLabel[lab_] := Rotate[lab, 45Degree]TreeGraph[{12, 13, 14}, VertexLabels -> Table[i -> Placed["xxx", Below, rotateLabel], {i, 4}]]panelLabel[lab_] := Panel[lab, FrameMargins -> 0, Background -> Lighter[Yellow, 0.7]]TreeGraph[{12, 13, 14}, VertexLabels -> Table[i -> Placed["xxx", Center, panelLabel], {i, 4}]]hyperlinkLabel[lab_] := Hyperlink[lab, "http://www.wolfram.com"]TreeGraph[{12, 13, 14}, VertexLabels -> Table[i -> Placed["xxx", Center, hyperlinkLabel], {i, 4}]]VertexShape (5)
使用任意的 Graphics、Image 或 Graphics3D 作为顶点形状:
Table[TreeGraph[{12, 13, 14}, VertexShape -> s, VertexSize -> 0.3], {s, {[image], [image], [image]}}]TreeGraph[{12, 13, 14}, VertexShape -> {2 -> [image]}, VertexSize -> 0.3]VertexShape 可与 VertexSize 组合使用:
Table[TreeGraph[{12, 13, 14}, VertexSize -> s, VertexShape -> [image], PlotLabel -> s], {s, {Medium, Large}}]VertexShape 不受 VertexStyle 的影响:
TreeGraph[{12, 13, 14}, VertexSize -> 0.3, VertexShape -> [image], VertexStyle -> Blue]VertexShapeFunction 具有比 VertexShape 更高的优先级:
TreeGraph[{12, 13, 14}, VertexSize -> 0.2, VertexShapeFunction -> "Square", VertexShape -> [image]]VertexShapeFunction (10)
获取 VertexShapeFunction 一组内置设置的列表:
ResourceData["VertexShapeFunction"]使用 "Basic" 系列中 VertexShapeFunction 的内置设置:
ResourceData["VertexShapeFunction", "Basic"]Table[TreeGraph[{12, 13, 14}, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, {"Triangle", "Square", "Rectangle", "Pentagon", "Hexagon", "Octagon"}}]Table[TreeGraph[{12, 13, 14}, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, {"DownTrapezoid", "UpTrapezoid", "Parallelogram", "FiveDown", "Circle", "Diamond", "Star", "Capsule"}}]使用 "Rounded" 系列中 VertexShapeFunction 的内置设置:
ResourceData["VertexShapeFunction", "Rounded"]Table[TreeGraph[{12, 13, 14}, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, ResourceData["VertexShapeFunction", "Rounded"]}]使用 "Concave" 系列中 VertexShapeFunction 的内置设置:
ResourceData["VertexShapeFunction", "Concave"]Table[TreeGraph[{12, 13, 14}, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, ResourceData["VertexShapeFunction", "Concave"]}]TreeGraph[{12, 13, 14}, VertexShapeFunction -> { 1 -> "Square"}, VertexSize -> 0.2]TreeGraph[{12, 13, 14}, VertexShapeFunction -> { 1 -> "Square", "Triangle"}, VertexSize -> 0.2]TreeGraph[{12, 13, 14}, VertexShapeFunction -> (Inset[[image], #]&)]vf[{xc_, yc_}, name_, {w_, h_}] :=
Block[{xmin = xc - w, xmax = xc + w, ymin = yc - h, ymax = yc + h},
Polygon[{{xmin, ymin}, {xmax, ymax}, {xmin, ymax}, {xmax, ymin}}]
];TreeGraph[{12, 13, 14}, VertexShapeFunction -> vf, VertexSize -> 0.2]VertexShapeFunction 可以与 VertexStyle 组合使用:
vf1[{xc_, yc_}, name_, {w_, h_}] := Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]TreeGraph[{12, 13, 14}, VertexSize -> 0.2, VertexStyle -> Blue, VertexShapeFunction -> vf1]VertexShapeFunction 具有比 VertexStyle 更高的优先级:
vf2[{xc_, yc_}, name_, {w_, h_}] := {Red, Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]}TreeGraph[{12, 13, 14}, VertexSize -> 0.2, VertexStyle -> Blue, VertexShapeFunction -> vf2]VertexShapeFunction 可以与 VertexSize 组合使用:
TreeGraph[{12, 13, 14}, VertexShapeFunction -> "Star", VertexSize -> {1 -> Small, Medium}]VertexShapeFunction 具有比 VertexShape 更高的优先级:
TreeGraph[{12, 13, 14}, VertexSize -> 0.3, VertexShapeFunction -> "Star", VertexShape -> [image]]VertexSize (8)
TreeGraph[{12, 13, 14}, VertexSize -> Automatic]Table[TreeGraph[{12, 13, 14}, VertexSize -> s, PlotLabel -> s], {s, {Tiny, Small, Medium, Large}}]Table[TreeGraph[{12, 13, 14}, VertexSize -> s, PlotLabel -> s], {s, 0.1, 1, 0.3}]Table[TreeGraph[{12, 13, 14}, VertexSize -> {"Scaled", s}, PlotLabel -> {"Scaled", s}], {s, 0.1, 1, 0.3}]Table[TreeGraph[{12, 13, 14}, VertexSize -> s, PlotLabel -> s], {s, {{0.1, 0.2}, {0.2, 0.1}}}]TreeGraph[{12, 13, 14}, VertexSize -> {1 -> 0.2, 2 -> 0.3}]VertexSize 可以与 VertexShapeFunction 组合使用:
Table[TreeGraph[{12, 13, 14}, VertexSize -> s, VertexShapeFunction -> "Square", PlotLabel -> s], {s, {0.05, 0.1, 0.2}}]VertexSize 可以与 VertexShape 组合使用:
Table[TreeGraph[{12, 13, 14}, VertexSize -> s, VertexShape -> [image], PlotLabel -> s], {s, {0.1, 0.2, 0.4}}]VertexStyle (5)
Table[TreeGraph[{12, 13, 14}, VertexStyle -> style, VertexSize -> 0.3, PlotLabel -> style], {style, {Yellow, EdgeForm[Dashed]}}]TreeGraph[{12, 13, 14}, VertexStyle -> {1 -> Blue, 2 -> Red}, VertexSize -> 0.2]VertexShapeFunction 可以与 VertexStyle 组合使用:
vf1[{xc_, yc_}, name_, {w_, h_}] := Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]TreeGraph[{12, 13, 14}, VertexSize -> 0.2, VertexStyle -> Blue, VertexShapeFunction -> vf1]VertexShapeFunction 具有比 VertexStyle 更高的优先级:
vf2[{xc_, yc_}, name_, {w_, h_}] := {Red, Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]}TreeGraph[{12, 13, 14}, VertexSize -> 0.2, VertexStyle -> Blue, VertexShapeFunction -> vf2]VertexStyle 可以与 BaseStyle 组合使用:
TreeGraph[{12, 13, 14}, VertexStyle -> LightBlue, BaseStyle -> EdgeForm[Dotted], VertexSize -> 0.2]VertexStyle 具有比 BaseStyle 更高的优先级:
TreeGraph[{12, 13, 14}, VertexStyle -> LightBlue, BaseStyle -> Gray, VertexSize -> 0.2]VertexShape 不受 VertexStyle 的影响:
TreeGraph[{12, 13, 14}, VertexSize -> 0.3, VertexShape -> [image], VertexStyle -> Blue]VertexWeight (2)
TreeGraph[{12, 13, 14}, VertexWeight -> {2, 3, 4, 5}]AnnotationValue[{%, 1}, VertexWeight]TreeGraph[{12, 13, 14}, VertexWeight -> {a, b, c, d}]AnnotationValue[{%, 1}, VertexWeight]应用 (4)
g = TreeGraph[RandomInteger[#] # + 1 & /@ Range[0, 30], VertexSize -> 0.4]突出显示 GraphCenter 和 GraphPeriphery:
{HighlightGraph[g, GraphCenter[g]], HighlightGraph[g, GraphPeriphery[g]]}g = TreeGraph[RandomInteger[#] # + 1 & /@ Range[0, 30], VertexSize -> 0.4];VertexEccentricity[g, #]& /@ Range[3]FindVertexEccentricityPath[g_ ? UndirectedGraphQ, u_] /; MemberQ[VertexList[g], u] := Module[{d = GraphDistanceMatrix[g], posu, posv, vl = VertexList[g]}, posu = VertexIndex[g, u];
posv = First@First@Position[d[[posu]], Max[d[[posu]]]];
PathGraph[FindShortestPath[g, u, vl[[posv]]]]]Table[HighlightGraph[g, FindVertexEccentricityPath[g, u]], {u, Range[3]}]g = TreeGraph[RandomInteger[#] # + 1 & /@ Range[0, 30], VertexSize -> 0.4];GraphRadius[g]FindRadiusPath[g_ ? UndirectedGraphQ] := Module[{c = First@GraphCenter[g], d, v, pos}, d = Table[GraphDistance[g, c, u], {u, VertexList[g]}];
pos = First@Position[d, Max[d]];
v = First@Part[VertexList[g], pos];
PathGraph[FindShortestPath[g, c, v]]]HighlightGraph[g, FindRadiusPath[g]]g = TreeGraph[RandomInteger[#] # + 1 & /@ Range[0, 30], VertexSize -> 0.4];GraphDiameter[g]FindDiameterPath[g_ ? UndirectedGraphQ] := Module[{d = GraphDistanceMatrix[g], u, v, pos}, pos = First@Position[d, Max[d]];
{u, v} = Part[VertexList[g], pos];
PathGraph[FindShortestPath[g, u, v]]]HighlightGraph[g, FindDiameterPath[g]]属性和关系 (9)
使用 VertexCount 和 EdgeCount 计算顶点数和边数:
g = CompleteKaryTree[3, 3]{VertexCount[g], EdgeCount[g]}使用 VertexList 和 EdgeList 以标准顺序列举顶点和边:
g = TreeGraph[{12, 13, 34}];{VertexList[g], EdgeList[g]}g1 = TreeGraph[{34, 12, 13}];{VertexList[g1], EdgeList[g1]}从一个图计算 AdjacencyMatrix:
g = TreeGraph[{12, 13, 1 -> 4}, EdgeStyle -> Arrowheads[Medium], EdgeShapeFunction -> "Arrow"](a = AdjacencyMatrix[g]//Normal)//MatrixFormCompleteKaryTree[3, 3]{ConnectedGraphQ[%], TreeGraphQ[%]}TreeGraph[Range[8], {1, 1, 2, 1, 4, 2, 3, 1}]{LoopFreeGraphQ[%], AcyclicGraphQ[%], TreeGraphQ[%]}TreeGraph[{1 -> 2, 1 -> 3, 2 -> 4, 2 -> 5, 3 -> 6}]VertexCount[%] - EdgeCount[%]CompleteKaryTree[3, 3]{ TreeGraphQ[%], BipartiteGraphQ[%]}具有
(
) 个顶点树图具有至少2个、至多
个度数为1的顶点:
TreeGraph[{1 -> 2, 1 -> 3}]VertexDegree[%]CompleteKaryTree[2, 6]VertexDegree[%]StarGraph[10]TreeGraphQ[%]文本
Wolfram Research (2010),TreeGraph,Wolfram 语言函数,https://reference.wolfram.com/language/ref/TreeGraph.html (更新于 2015 年).
CMS
Wolfram 语言. 2010. "TreeGraph." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2015. https://reference.wolfram.com/language/ref/TreeGraph.html.
APA
Wolfram 语言. (2010). TreeGraph. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/TreeGraph.html 年
BibTeX
@misc{reference.wolfram_2026_treegraph, author="Wolfram Research", title="{TreeGraph}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/TreeGraph.html}", note=[Accessed: 03-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_treegraph, organization={Wolfram Research}, title={TreeGraph}, year={2015}, url={https://reference.wolfram.com/language/ref/TreeGraph.html}, note=[Accessed: 03-November-2026]}