将箭头放在用 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}

在此处输入图片描述

相关内容