在 tikz 中绘制语法树

在 tikz 中绘制语法树

我想绘制一个语法树,类似于下面的语法树。我知道我可以使用 tikz 绘制语法树,但我似乎无法弄清楚如何绘制一条直线到位于父节点正下方的节点。

\begin{tikzpicture}
\node{S}
 child {node {a}}
 child {node {B}};
\end{tikzpicture}

我想要绘制的语法树

答案1

这里有一个起点:

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}

\begin{tikzpicture}[
  tlabel/.style={pos=0.4,right=-1pt,font=\footnotesize\color{red!70!black}},
]
\node{S}
child {node {a}}
child {node {S} 
  child {node {a}}
  child {node {S}
    child {node {$\varepsilon$}
      edge from parent node[tlabel,pos=0.2] {2}
    }
    edge from parent node[tlabel] {1}
  }
  child {node {B}
    child {node {B}
      child {node {b}
        edge from parent node[tlabel,pos=0.2] {5}
     }
      edge from parent node[tlabel] {4}
    }
    child {node {b}}
  }
    edge from parent node[tlabel] {1}
  }
child {node {B}
  child[missing] {}
  child[missing] {}
  child {node {b}
    edge from parent node[tlabel,pos=0.15,right=2pt] {5}
  }
};
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

这是具有一些不同装饰的例子,但可以轻松调整:

平均能量损失

\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
\usepackage{fullpage}
\usepackage{calc}
\usetikzlibrary{positioning,shadows,arrows,trees,shapes,fit}


\begin{document}

\begin{tikzpicture}
[font=\small, edge from parent, 
    every node/.style={top color=white, bottom color=blue!25, 
    rectangle,rounded corners, minimum size=6mm, draw=blue!75,
    very thick, drop shadow, align=center},
    edge from parent/.style={draw=blue!50,thick},
    level 1/.style={sibling distance=3cm},
    level 2/.style={sibling distance=1.2cm}, 
    level 3/.style={sibling distance=1cm}, 
    level distance=2cm,
    ]
    \node (A) {A} 
        child { node (B) {B}
            child { node {B1} 
            edge from parent node[left=.5em,draw=none] {$\chi^2$} }
            child { node {B2}}
            }
        child {node (C) {C}
            child { node {C1}
                child { node {C1a}}
            }
        child { node {C2}}
        child { node {C3}
            child { node {C3a}}
            child { node {C3b} edge from parent node[right=.5em,draw=none] {$\frac{a}{b}$}}
            }
        }
    child { node {D} 
        child { node {D1}}
        child { node {D2}}
};

\end{tikzpicture}
\end{document}

答案3

tikz-qtree包提供了更简单的语法来绘制这些类型的树。边缘的注释以类似的方式进行。

\documentclass{article}
\usepackage{tikz-qtree}
\begin{document}
\begin{tikzpicture}[%
  sibling distance=.5cm,
  empty/.style={draw=none},
  tlabel/.style={font=\footnotesize\color{red!70!black}}]
\Tree  [.S  
         [.a ]
         \edge node[tlabel,auto=left] {1}; 
         [.S  
             [.a ] 
            \edge node[tlabel,auto=left] {1};
            [.S \edge node[tlabel,auto=left] {2}; [.$\epsilon$ ] ] 
            [.B \edge node[tlabel,auto=right] {4};
              [.B \edge node[tlabel,auto=left] {5}; [.b ]]
              [.b ]
            ]
         ] 
         [.B   \edge[empty]; {} \edge node[tlabel,auto=left] {5}; {b}   ]
       ]

\end{tikzpicture}
\end{document}

代码输出

相关内容