TikZtree 具有不對稱兄弟姊妹

TikZtree 具有不對稱兄弟姊妹

有沒有辦法在 Ti 定義(二元)樹kZ 中,兄弟姊妹的兩條線的斜率不同。

我想產生一棵具有以下外觀的樹,而不需要單獨定位每個節點。

具有不對稱兄弟姊妹的樹:

答案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

很容易做到forest

\documentclass{article}
\usepackage[linguistics]{forest}
\begin{document}
\begin{forest}for tree={calign=fixed edge angles,
calign primary angle=-30,calign secondary angle=60,l=1.25cm}
[A [1] [B [2] [C [3] [D [4] [E [5] [F]]]]]]
\end{forest}
\end{document}

程式碼的輸出

答案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}

產生以下結果:

在此輸入影像描述

你可以找到更多例子這裡

相關內容