答案1
我發布這個答案是因為之前的答案(西娜·艾哈邁迪)不滿足一個重要條件:兄弟姊妹的兩條線的斜率不同。
可自訂程式碼:
我們定義一個新指令\binary
。其語法如下:
\binary[slope left=<num>,slope right=<num>,height=<num>] (<node>) to (<node>) node {<text>} and (<node>) node {<text>};
它會給我們
請注意,height
是 fromA.north
而不是 from A.south
。需要改進。
設定為slope left=50,slope right=35,height=0.75
給我們
\documentclass[tikz,margin=3mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\def\binary[slope left=#1,slope right=#2,height=#3] (#4) to (#5) node #6 and (#7) node #8; {
\node (#5) at ($(#4)+({180+#1}:{#3/cos(90-(#1))})$) {#6};
\node (#7) at ($(#4)+({-#2}:{#3/cos(90-(#2))})$) {#8};
\draw (#5.north)--(#4.south)--(#7.north);
}
\node (A) at (0,0) {A};
\binary[slope left=50,slope right=35,height=0.75] (A) to (1) node {1} and (B) node {B};
\binary[slope left=50,slope right=35,height=0.75] (B) to (2) node {2} and (C) node {C};
\binary[slope left=50,slope right=35,height=0.75] (C) to (3) node {3} and (D) node {D};
\binary[slope left=50,slope right=35,height=0.75] (D) to (4) node {4} and (E) node {E};
\binary[slope left=50,slope right=35,height=0.75] (E) to (5) node {5} and (F) node {F};
\end{tikzpicture}
\end{document}
較少可自訂的代碼:
稍微改變\binary
了一點,現在所有節點的標籤和節點名稱都是一樣的。
\documentclass[tikz,margin=3mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\def\binary[slope left=#1,slope right=#2,height=#3] (#4) to (#5) and (#6); {
\node (#5) at ($(#4)+({180+#1}:{#3/cos(90-(#1))})$) {#5};
\node (#6) at ($(#4)+({-#2}:{#3/cos(90-(#2))})$) {#6};
\draw (#5.north)--(#4.south)--(#6.north);
}
\node (A) at (0,0) {A};
\binary[slope left=50,slope right=35,height=0.75] (A) to (1) and (B);
\binary[slope left=50,slope right=35,height=0.75] (B) to (2) and (C);
\binary[slope left=50,slope right=35,height=0.75] (C) to (3) and (D);
\binary[slope left=50,slope right=35,height=0.75] (D) to (4) and (E);
\binary[slope left=50,slope right=35,height=0.75] (E) to (5) and (F);
\end{tikzpicture}
\end{document}
答案2
答案3
您可以使用forest
包。這是一個最小的工作範例:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{forest}
\begin{document}
\begin{forest}
[A
[1][B
[2][C
[3][D
[4][E]
]
]
]
]
]
\end{forest}
\end{document}
產生以下結果:
你可以找到更多例子這裡。