將箭頭放在用 tikzpicture 繪製的樹中

將箭頭放在用 tikzpicture 繪製的樹中

我開始使用 LaTeX 來完成我在大學學習的圖論課程。我設法構建了這棵樹,我只缺少箭頭(應該在所有邊緣都指向下方)。

\begin{tikzpicture}[level/.style={sibling distance=30mm/#1}]
\node [circle,draw] {1}
  child {node [circle,draw] {2}
    child {node [circle,draw] {5}}
    child {node [circle,draw] {6}}
    child {node [circle,draw] {7}}
  }
  child {node [circle,draw] {3}
  }
  child {node [circle,draw] {4}
    child {node [circle,draw] {8}}
    child {node [circle,draw] {9}}
    child {node [circle,draw] {10}}
};
\end{tikzpicture}

答案1

您只需要定義邊緣的樣式:

\documentclass[tikz, margin=3mm]{standalone}

\begin{document}
    \begin{tikzpicture}[
level/.style={sibling distance=30mm/#1},
edge from parent/.style={->,draw}   % <----
                        ]
\node [circle,draw] {1}
  child {node [circle,draw] {2}
    child {node [circle,draw] {5}}
    child {node [circle,draw] {6}}
    child {node [circle,draw] {7}}
  }
  child {node [circle,draw] {3}
  }
  child {node [circle,draw] {4}
    child {node [circle,draw] {8}}
    child {node [circle,draw] {9}}
    child {node [circle,draw] {10}}
};
\end{tikzpicture}
\end{document}

在此輸入影像描述

forest您可以透過使用專用於繪製樹狀圖的套件來實現類似的結果:

\documentclass[margin=3mm]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}

\begin{document}
    \begin{forest}
for tree = {
    circle, draw, 
    minimum size=1.5em,
    inner sep=2pt,
%
    s sep=3mm,
    l sep=7mm,
    edge={-Straight Barb} % arrows head defined in 'arrows.meta'
            }
[1
    [2
        [5]
        [6]
        [7]
    ]
    [3,fit=band]
    [4
        [8]
        [9]
        [10]
    ]
r
]
    \end{forest}
\end{document}

在此輸入影像描述

相關內容