给出含有 n 个层的完全二叉树.
CompleteKaryTree[n,k]
给出含有 n 个层的完全 k 叉树.
CompleteKaryTree
给出含有 n 个层的完全二叉树.
CompleteKaryTree[n,k]
给出含有 n 个层的完全 k 叉树.
更多信息和选项
- CompleteKaryTree 也称为完整 k 叉树.
- 含有 n 个层的完全 k 叉树是一个含有根结点的树,在每个结点上有 k 个分叉并且深度为 n.
- 含有 n 个层的完全 k 叉树有
个顶点. - CompleteKaryTree[…,DirectedEdges->True] 给出一个有向完全 k 叉树.
- CompleteKaryTree 采用和 Graph 相同的选项.
所有选项的列表
范例
打开所有单元 关闭所有单元基本范例 (3)
选项 (79)
AnnotationRules (2)
DirectedEdges (1)
CompleteKaryTree[2, 3]使用 DirectedEdges->True 来生成一个有向图:
CompleteKaryTree[2, 3, DirectedEdges -> True]EdgeLabels (6)
CompleteKaryTree[2, 3, EdgeLabels -> {12 -> "Hello"}]el = EdgeList[CompleteKaryTree[2, 3]]CompleteKaryTree[2, 3, EdgeLabels -> Table[el[[i]] -> Subscript["e", i], {i, Length[el]}]]CompleteKaryTree[2, 3, EdgeLabels -> {12 -> [image], 13 -> [image], 14 -> [image]}]Table[CompleteKaryTree[2, 3, EdgeLabels -> {12 -> Placed["■■■", p]}, PlotLabel -> p, BaselinePosition -> Bottom], {p, {0, 1 / 4, 1 / 3}}]Table[CompleteKaryTree[2, 3, EdgeLabels -> {12 -> Placed["■■■", {1 / 2, p}]}, PlotLabel -> p, BaselinePosition -> Bottom], {p, {{0, 0}, {1 / 2, 1 / 2}, {1, 1}}}]CompleteKaryTree[2, 3, EdgeLabels -> {31 -> Placed[{"lbl1", "lbl2"}, {"Start", "End"}]}]CompleteKaryTree[2, 3, EdgeLabels -> {31 -> Placed[{"lbl1", "lbl2", "lbl3"}, {"Start", "Middle", "End"}]}]通过 Tooltip 和 StatusArea 的值使用自动标签:
CompleteKaryTree[2, 3, EdgeLabels -> Placed["Name", Tooltip]]CompleteKaryTree[2, 3, EdgeLabels -> Placed["Name", StatusArea]]EdgeShapeFunction (6)
获取 EdgeShapeFunction 的内置设置列表:
ResourceData["EdgeShapeFunction"]CompleteKaryTree[2, 3, EdgeShapeFunction -> "Line"]Table[CompleteKaryTree[2, 3, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, {"BoxLine", "DiamondLine", "DotLine"}}]Table[CompleteKaryTree[2, 3, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, ResourceData["EdgeShapeFunction", "FilledArrow"]}]Table[CompleteKaryTree[2, 3, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, ResourceData["EdgeShapeFunction", "UnfilledArrow"]}]Table[CompleteKaryTree[2, 3, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, ResourceData["EdgeShapeFunction", "CarvedArrow"]}]CompleteKaryTree[2, 3, EdgeShapeFunction -> {12 -> "DotLine"}]CompleteKaryTree[2, 3, EdgeShapeFunction -> {12 -> "BoxLine", "DotLine"}]ef[pts_List, e_] :=
Block[{s = 0.015, g = [image]}, {Arrowheads[{{s, 0.33, g}, {s, 0.67, g}}], Arrow[pts]}]CompleteKaryTree[2, 3, EdgeShapeFunction -> ef]EdgeShapeFunction 可以与 EdgeStyle 合起来使用:
CompleteKaryTree[2, 3, EdgeStyle -> Blue, EdgeShapeFunction -> (Line[#1]&)]EdgeShapeFunction 具有比 EdgeStyle 更高的优先级:
CompleteKaryTree[2, 3, EdgeStyle -> Blue, EdgeShapeFunction -> ({Red, Line[#1]}&)]EdgeStyle (2)
EdgeWeight (2)
CompleteKaryTree[2, 3, EdgeWeight -> RandomInteger[5, 3]]WeightedAdjacencyMatrix[%]//MatrixFormCompleteKaryTree[2, 3, EdgeWeight -> {a, b, c}]WeightedAdjacencyMatrix[%]//MatrixFormGraphLayout (5)
CompleteKaryTree[2, 3, GraphLayout -> Automatic]Table[CompleteKaryTree[5, 3, GraphLayout -> l, PlotLabel -> l], {l, {"CircularEmbedding", "SpiralEmbedding"}}]Table[CompleteKaryTree[5, 3, GraphLayout -> l, PlotLabel -> l], {l, {"SpringEmbedding", "SpringElectricalEmbedding", "HighDimensionalEmbedding"}}]VertexCoordinates 覆盖 GraphLayout 坐标:
{CompleteKaryTree[2, 3, GraphLayout -> "SpringElectricalEmbedding"],
CompleteKaryTree[2, 3, GraphLayout -> "SpringElectricalEmbedding", VertexCoordinates -> Table[{i, i}, {i, 0, 3}]]}通过一个布局算法,利用 AbsoluteOptions 提取 VertexCoordinates:
CompleteKaryTree[2, 3]AbsoluteOptions[%, VertexCoordinates]GraphHighlight (3)
CompleteKaryTree[2, 3, VertexSize -> Tiny, GraphHighlight -> {1}]CompleteKaryTree[2, 3, VertexSize -> Tiny, GraphHighlight -> {13}]CompleteKaryTree[2, 3, VertexSize -> Tiny, GraphHighlight -> {1, 2, 13, 14}]GraphHighlightStyle (2)
获取 GraphHighlightStyle 的默认设置列表:
ResourceData["GraphHighlightStyle"]使用 GraphHighlightStyle 的内置设置:
CompleteKaryTree[2, 3, GraphHighlight -> {1, 12}, VertexSize -> Small, GraphHighlightStyle -> #, PlotLabel -> #]& /@ Select[ResourceData["GraphHighlightStyle"], # =!= Automatic&]PlotTheme (4)
基本主题 (2)
VertexCoordinates (3)
CompleteKaryTree[2, 3]使用 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}]]]CompleteKaryTree[3, 3, VertexCoordinates -> ellipseLayout[13, {2, 1}]]VertexCoordinates 具有比 GraphLayout 更高的优先级:
CompleteKaryTree[2, 3, VertexCoordinates -> Table[{i, i}, {i, 4}], GraphLayout -> "CircularEmbedding"]VertexLabels (13)
CompleteKaryTree[2, 3, VertexLabels -> "Name"]CompleteKaryTree[2, 3, VertexLabels -> {2 -> "one"}]CompleteKaryTree[2, 3, VertexLabels -> Table[i -> Subscript[v, i], {i, 4}]]CompleteKaryTree[2, 3, VertexLabels -> {1 -> [image], 2 -> [image], 3 -> [image]}]使用具有符号位置信息的 Placed 来控制标签位置,包括外部位置:
Table[CompleteKaryTree[2, 3, VertexSize -> 0.1, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed["■■■", p], {i, 4}], PlotLabel -> p, ImagePadding -> 20], {p, {Before, After, Below, Above}}]pl = {{Before, Below}, {After, Below}, {Before, Above}, {After, Above}};Table[CompleteKaryTree[2, 3, VertexSize -> 0.1, VertexShapeFunction -> "Square", ImagePadding -> 20, VertexLabels -> Table[i -> Placed["■■■", p], {i, 4}], PlotLabel -> p], {p, pl}]Table[CompleteKaryTree[2, 3, VertexSize -> 0.45, 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[CompleteKaryTree[2, 3, VertexSize -> 0.45, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed["■■■", p], {i, 4}], PlotLabel -> p], {p, pl}]Table[CompleteKaryTree[2, 3, VertexSize -> 0.45, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed[[image], p], {i, 4}], PlotLabel -> p, BaselinePosition -> Bottom], {p, {{0, 0}, {1 / 2, 1 / 2}, {1, 1}}}]Table[CompleteKaryTree[2, 3, VertexSize -> 0.45, 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}}}]CompleteKaryTree[2, 3, VertexLabels -> {1 -> Placed[{"lbl1", "lbl2"}, {Above, Below}]}]CompleteKaryTree[2, 3, VertexLabels -> {1 -> Placed[{"lbl1", "lbl2", "lbl3", "lbl4"}, {Above, After, Below, Before}]}]使用 Placed 的参数来控制格式,包括t Tooltip:
CompleteKaryTree[2, 3, VertexLabels -> Placed["Name", Tooltip]]或者 StatusArea:
CompleteKaryTree[2, 3, VertexLabels -> Placed["Name", StatusArea]]rotateLabel[lab_] := Rotate[lab, 45Degree]CompleteKaryTree[2, 3, VertexLabels -> Table[i -> Placed["xxx", Below, rotateLabel], {i, 4}]]panelLabel[lab_] := Panel[lab, FrameMargins -> 0, Background -> Lighter[Yellow, 0.7]]CompleteKaryTree[2, 3, VertexLabels -> Table[i -> Placed["xxx", Center, panelLabel], {i, 4}]]hyperlinkLabel[lab_] := Hyperlink[lab, "http://www.wolfram.com"]CompleteKaryTree[2, 3, VertexLabels -> Table[i -> Placed["xxx", Center, hyperlinkLabel], {i, 4}]]VertexShape (5)
将任意 Graphics、Image 或者 Graphics3D 作为顶点形状使用:
Table[CompleteKaryTree[2, 3, VertexShape -> s, VertexSize -> Medium], {s, {[image], [image], [image]}}]CompleteKaryTree[2, 3, VertexShape -> {2 -> [image]}, VertexSize -> Large]VertexShape 可以与 VertexSize 组合使用:
Table[CompleteKaryTree[2, 3, VertexSize -> s, VertexShape -> [image], PlotLabel -> s], {s, {Small, Large}}]VertexShape 不受 VertexStyle 影响:
CompleteKaryTree[2, 3, VertexSize -> 0.2, VertexShape -> [image], VertexStyle -> Blue]VertexShapeFunction 具有比 VertexShape 更高的优先级:
CompleteKaryTree[2, 3, VertexSize -> 0.2, VertexShapeFunction -> "Square", VertexShape -> [image]]VertexShapeFunction (10)
获取 VertexShapeFunction 的内置设置列表:
ResourceData["VertexShapeFunction"]利用 VertexShapeFunction 在 "Basic" 集合中的内置设置:
ResourceData["VertexShapeFunction", "Basic"]Table[CompleteKaryTree[2, 3, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, {"Triangle", "Square", "Rectangle", "Pentagon", "Hexagon", "Octagon"}}]Table[CompleteKaryTree[2, 3, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, {"DownTrapezoid", "UpTrapezoid", "Parallelogram", "FiveDown", "Circle", "Diamond", "Star", "Capsule"}}]使用 VertexShapeFunction 在 "Rounded" 集合中的内置设置:
ResourceData["VertexShapeFunction", "Rounded"]Table[CompleteKaryTree[2, 3, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, ResourceData["VertexShapeFunction", "Rounded"]}]使用 VertexShapeFunction 在 "Concave" 集合中的内置设置:
ResourceData["VertexShapeFunction", "Concave"]Table[CompleteKaryTree[2, 3, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, ResourceData["VertexShapeFunction", "Concave"]}]CompleteKaryTree[2, 3, VertexShapeFunction -> { 1 -> "Square"}, VertexSize -> 0.2]CompleteKaryTree[2, 3, VertexShapeFunction -> { 1 -> "Square", "Triangle"}, VertexSize -> 0.2]CompleteKaryTree[2, 3, 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}}]
];CompleteKaryTree[2, 3, VertexShapeFunction -> vf, VertexSize -> 0.2]VertexShapeFunction 可以与 VertexStyle 组合使用:
vf1[{xc_, yc_}, name_, {w_, h_}] := Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]CompleteKaryTree[2, 3, 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}]}CompleteKaryTree[2, 3, VertexSize -> 0.2, VertexStyle -> Blue, VertexShapeFunction -> vf2]VertexShapeFunction 可以与 VertexSize 组合使用:
CompleteKaryTree[2, 3, VertexShapeFunction -> "Star", VertexSize -> {1 -> Small, Medium}]VertexShapeFunction 具有比 VertexShape 更高的优先级:
CompleteKaryTree[2, 3, VertexSize -> 0.3, VertexShapeFunction -> "Star", VertexShape -> [image]]VertexSize (8)
CompleteKaryTree[2, 3, VertexSize -> Automatic]Table[CompleteKaryTree[2, 3, VertexSize -> s, PlotLabel -> s], {s, {Tiny, Small, Medium, Large}}]Table[CompleteKaryTree[2, 3, VertexSize -> s, PlotLabel -> s], {s, 0.1, 1, 0.3}]Table[CompleteKaryTree[2, 3, VertexSize -> {"Scaled", s}, PlotLabel -> {"Scaled", s}], {s, 0.1, 1, 0.3}]Table[CompleteKaryTree[2, 3, VertexSize -> s, PlotLabel -> s], {s, {{0.1, 0.2}, {0.2, 0.1}}}]CompleteKaryTree[2, 3, VertexSize -> {1 -> 0.2, 2 -> 0.3}]VertexSize 可以与 VertexShapeFunction 组合使用:
Table[CompleteKaryTree[2, 3, VertexSize -> s, VertexShapeFunction -> "Square", PlotLabel -> s], {s, {0.05, 0.1, 0.2}}]VertexSize 可以与 VertexShape 组合使用:
Table[CompleteKaryTree[2, 3, VertexSize -> s, VertexShape -> [image], PlotLabel -> s], {s, {0.1, 0.2, 0.4}}]VertexStyle (5)
Table[CompleteKaryTree[2, 3, VertexStyle -> style, VertexSize -> 0.3, PlotLabel -> style], {style, {Yellow, EdgeForm[Dashed]}}]CompleteKaryTree[2, 3, 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}]CompleteKaryTree[2, 3, 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}]}CompleteKaryTree[2, 3, VertexSize -> 0.2, VertexStyle -> Blue, VertexShapeFunction -> vf2]VertexStyle 可以与 BaseStyle 组合使用:
CompleteKaryTree[2, 3, VertexStyle -> LightBlue, BaseStyle -> EdgeForm[Dotted], VertexSize -> 0.2]VertexStyle 具有比 BaseStyle 更高的优先级:
CompleteKaryTree[2, 3, VertexStyle -> LightBlue, BaseStyle -> Gray, VertexSize -> 0.2]VertexShape 不受 VertexStyle 的影响:
CompleteKaryTree[2, 3, VertexSize -> 0.2, VertexShape -> [image], VertexStyle -> Blue]VertexWeight (2)
CompleteKaryTree[2, 3, VertexWeight -> {2, 3, 4, 5}]AnnotationValue[{%, 1}, VertexWeight]CompleteKaryTree[2, 3, VertexWeight -> {a, b, c, d}]AnnotationValue[{%, 1}, VertexWeight]应用 (7)
一个完全 k 叉树的 GraphCenter:
Table[HighlightGraph[#, GraphCenter[#]]&[CompleteKaryTree[i, 2, VertexSize -> Medium]], {i, 2, 4}]Table[HighlightGraph[#, GraphPeriphery[#]]&[CompleteKaryTree[i, 2, VertexSize -> Medium]], {i, 2, 4}]VertexEccentricity[CompleteKaryTree[3, 2], #]& /@ VertexList[CompleteKaryTree[3, 2]]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 = CompleteKaryTree[3, 2], FindVertexEccentricityPath[g, u]], {u, Range[3]}]Table[GraphRadius[CompleteKaryTree[i, 2]], {i, 2, 4}]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]]]Table[HighlightGraph[#, FindRadiusPath[#]]&[CompleteKaryTree[i, 2, VertexSize -> Tiny]], {i, 2, 4}]Table[GraphDiameter[CompleteKaryTree[i, 2]], {i, 2, 4}]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]]]Table[HighlightGraph[#, FindDiameterPath[#]]&[CompleteKaryTree[i, 2, VertexSize -> Tiny]], {i, 2, 4}]突出显示 CompleteKaryTree 的顶点度:
HighlightCentrality[g_, cc_] := HighlightGraph[g, Table[Style[VertexList[g][[i]], ColorData["TemperatureMap"][cc[[i]] / Max[cc]]], {i, VertexCount[g]}]];g = CompleteKaryTree[3, 2, VertexSize -> Large];HighlightCentrality[g, VertexDegree[g]]HighlightCentrality[g, ClosenessCentrality[g]]HighlightCentrality[g, EigenvectorCentrality[g]]g = CompleteKaryTree[3, 2, VertexLabels -> "Name"]Table[HighlightGraph[g, PathGraph[FindShortestPath[g, 2, k]]], {k, Complement[Range[7], {2}]}]属性和关系 (8)
CompleteKaryTree[n,k] 具有
个顶点:
VertexCount[CompleteKaryTree[n, k]]CompleteKaryTree[n,k] 具有
条边:
EdgeCount[CompleteKaryTree[n, k]]CompleteKaryTree[3, 3]VertexCount[%] - EdgeCount[%]CompleteKaryTree[3, 4]TreeGraphQ[%]BipartiteGraphQ[CompleteKaryTree[3, 3]]AcyclicGraphQ[CompleteKaryTree[3, 3]]LoopFreeGraphQ[CompleteKaryTree[3, 3]]Table[CompleteKaryTree[3, i], {i, {2, 3, 4}}]Table[KaryTree[(i^3 - 1) / (i - 1), i], {i, {2, 3, 4}}]文本
Wolfram Research (2010),CompleteKaryTree,Wolfram 语言函数,https://reference.wolfram.com/language/ref/CompleteKaryTree.html.
CMS
Wolfram 语言. 2010. "CompleteKaryTree." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/CompleteKaryTree.html.
APA
Wolfram 语言. (2010). CompleteKaryTree. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/CompleteKaryTree.html 年
BibTeX
@misc{reference.wolfram_2026_completekarytree, author="Wolfram Research", title="{CompleteKaryTree}", year="2010", howpublished="\url{https://reference.wolfram.com/language/ref/CompleteKaryTree.html}", note=[Accessed: 07-September-2026]}
BibLaTeX
@online{reference.wolfram_2026_completekarytree, organization={Wolfram Research}, title={CompleteKaryTree}, year={2010}, url={https://reference.wolfram.com/language/ref/CompleteKaryTree.html}, note=[Accessed: 07-September-2026]}