Tikz:如何将节点变成箭头?

Tikz:如何将节点变成箭头?

我有下面的 tikz 图,我正在尝试将白色节点更改为来自父节点的箭头。这是当前的图表

在此处输入图片描述

以下是我正在尝试制作的草图:在此处输入图片描述

我已将目前为止的内容发布在下面。如能提供任何帮助,我将不胜感激

\documentclass[11pt]{report}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}[level/.style={sibling distance=90mm/#1},scale=0.7]
  \node [circle,draw,fill=gray,scale=0.7] (z){$s_0$}
    child {node [circle,draw,scale=0.7](d){$a_1$}
      child {node [circle,draw,fill=gray,scale=0.6] (a) {$s_1'$} 
      child {node {$\vdots$}}edge from parent node[left=4mm] {\small{$r=10$}}edge from parent node[ fill=white] {\tiny{$1$}}
    }
  }
  child {node [circle,draw,scale=0.7] (a) {$a_2$}
    child {node [circle,draw,fill=gray,scale=0.6] (b) {$s_2'$}
      child {node {$\vdots$}}
      child {node {$\vdots$}}edge from parent node[ above=0.2mm, left=3mm] {\small{$r=20$}}edge from parent node[ fill=white] {\tiny{$0.5$}}
    }
    child {node [circle,draw,fill=gray,scale=0.6] (g) {$s'_3$}
      child {node {$\vdots$}}
        child [grow=right] {node (q) {s'} edge from parent[draw=none]
            child [grow=up] {node (r) {a} edge from parent[draw=none]
            child [grow=up] {node (s) {$s$} edge from parent[draw=none]}}
        }edge from parent node[ above=1mm, right=2mm] {\small{$r=75$}}edge from parent node[ fill=white] {\tiny{$0.5$}}
    }
  }

;



\end{tikzpicture}

\end{document}



答案1

您可以添加->,level 2/.style={-}到 tikzpicture 环境的选项中,以绘制指向第一个子级别的箭头。

\documentclass[11pt]{report}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}[
            level/.style={sibling distance=90mm/#1},
            ->,
            level 2/.style={-},
            scale=0.7]
    \node [circle,draw,fill=gray,scale=0.7] (z){$s_0$}
        child {node [circle,draw,scale=0.7](d){$a_1$}
            child {node [circle,draw,fill=gray,scale=0.6] (a) {$s_1'$} 
            child {node {$\vdots$}}edge from parent node[left=4mm] {\small{$r=10$}}edge from parent node[ fill=white] {\tiny{$1$}}
        }
    }
    child {node [circle,draw,scale=0.7] (a) {$a_2$}
        child {node [circle,draw,fill=gray,scale=0.6] (b) {$s_2'$}
            child {node {$\vdots$}}
            child {node {$\vdots$}}edge from parent node[ above=0.2mm, left=3mm] {\small{$r=20$}}edge from parent node[ fill=white] {\tiny{$0.5$}}
        }
        child {node [circle,draw,fill=gray,scale=0.6] (g) {$s'_3$}
            child {node {$\vdots$}}
                child [grow=right] {node (q) {s'} edge from parent[draw=none]
                    child [grow=up] {node (r) {a} edge from parent[draw=none]
                    child [grow=up] {node (s) {$s$} edge from parent[draw=none]}}
                }edge from parent node[ above=1mm, right=2mm] {\small{$r=75$}}edge from parent node[ fill=white] {\tiny{$0.5$}}
        }
    }

;



\end{tikzpicture}

\end{document}

答案2

  • 作为对 @luki 的精彩回答的补充(+1)

  • 目前尚不清楚白色圆圈内的节点应该如何。

    • 只用箭头代替它们,或者
    • 箭头所带的标签分别包含$a_1$和?$a_2$

第二种情况的 MWE:

\documentclass[11pt, border=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\begin{document}
    \begin{tikzpicture}[
  arr/.style = {-{Straight Barb[angle=60:2pt 3]}, semithick}, 
    C/.style = {circle, draw, fill=gray!30,
                minimum size=1.6em, inner sep=0pt},
  LBL/.style = {font=\footnotesize},
  lbl/.style = {fill=white, inner sep=2pt, font=\scriptsize},
level/.style = {sibling distance=66mm/#1},
        arr,
level 2/.style={-},
                        ]
\node [C] (z) {$s_0$}
    child {coordinate (a1) 
        child {node [C]     {$s_1'$}
            child {node {$\vdots$}}
                   edge from parent node[LBL, left=4mm] {$r=10$}
                   edge from parent node[lbl] {1}
              }
        }
    child {coordinate (a2)
        child {node [C]     {$s_2'$}
            child {node {$\vdots$}}
            child {node {$\vdots$}}
                   edge from parent node[LBL, left=4mm] {$r=20$}
                   edge from parent node[lbl] {0.5}
              }
        child {node [C] (g) {$s_3'$}
            child {node {$\vdots$}}
            child {node {}
                   edge from parent[draw=none] 
                   }
                   edge from parent node[LBL,right=4mm] {$r=75$}
                   edge from parent node[lbl] {0.5}
              }
        };
\node[left]  at (a1) {$a_1$};  % <---
\node[right] at (a2) {$a_2$};  % <---
\node (s) [LBL, right=6mm] at (g) {$s'$};
\node     [LBL] at (a2 -| s) {$a$};
\node     [LBL] at (z -| s) {$s$};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

对于第一种情况,您只需删除 %<--- 标记的代码行:

在此处输入图片描述

相关内容