tikz 中的树,其中三角形代表子树

tikz 中的树,其中三角形代表子树

我需要在 tikz 中绘制一棵类似于这样的树:

一颗树

我不能做的是绘制虚线三角形叶子(代表未指定的子树)。我想我可以自己绘制图例,并且正在努力。有什么提示或想法吗?

下面是一个 MWE,展示了我目前所做的事情:

\documentclass[a4paper]{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[sibling distance=5cm, level 2/.style={sibling distance =2cm}]
\node[circle,draw,fill=black] {}
    child{ node[circle, fill = black] {}
            child{ node[circle, draw] {}
                        child{ node {Q1} } 
                }
            child{ node[rectangle, fill = black] {}
                    child {
                        node[rectangle, fill = black] {}
                            child{ node {Q2} }
                            child { node[circle,draw] {} 
                                    child { node {Q3} }                         
                                }
                        }
                }
        }
    child{ node[circle, draw] {}
            child{ node {Q4} }
            child{node {Q5} }       
        };
\end{tikzpicture}
\end{document}

答案1

这是使用形状变体的另一种选择isosceles triangle

\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}

\tikzset{
itria/.style={
  draw,dashed,shape border uses incircle,
  isosceles triangle,shape border rotate=90,yshift=-1.45cm},
rtria/.style={
  draw,dashed,shape border uses incircle,
  isosceles triangle,isosceles triangle apex angle=90,
  shape border rotate=-45,yshift=0.2cm,xshift=0.5cm},
ritria/.style={
  draw,dashed,shape border uses incircle,
  isosceles triangle,isosceles triangle apex angle=110,
  shape border rotate=-55,yshift=0.1cm},
letria/.style={
  draw,dashed,shape border uses incircle,
  isosceles triangle,isosceles triangle apex angle=110,
  shape border rotate=235,yshift=0.1cm}
}
\begin{tikzpicture}[sibling distance=5cm, level 2/.style={sibling distance =2cm}]
\node[circle,draw,fill=black] {}
    child{ node[circle, fill = black] {}
            child{ node[circle, draw] {}
                        { node[itria] {Q1} } 
                }
            child{ node[rectangle, fill = black] {}
                    child {
                        node[rectangle, fill = black] {}
                            child{ node[rtria] {Q2} }
                            child { node[circle,draw] {} 
                                     { node[itria] {Q3} }                         
                                }
                        }
                }
        }
    child{ node[circle, draw] {}
            child{ node[ritria] {Q4} }
            child{node[letria] {Q5} }       
        };
\node[draw] at (3,-5) 
{
\begin{tabular}{cl}
\tikz\node[circle,fill] {}; & branching \\
\tikz\node[circle,draw] {}; & jump \\
\tikz\node[rectangle,fill] {}; & unary
\end{tabular}
};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

好的,当您找到将节点名称提交给新定义的形状的方法时,我们就接近您想要的结果了。

\documentclass[a4paper]{article}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\pgfdeclareshape{left triangle}{
    \nodeparts{}
    \anchor{center}{\pgfpoint{0cm}{0cm}}
    \behindbackgroundpath{
    \path [draw,dashed] (0,0) -- (0,-2) -- (-1,-2) -- cycle;
   }
}
\pgfdeclareshape{right triangle}{
    \nodeparts{}
    \anchor{center}{\pgfpoint{0cm}{0cm}}
    \behindbackgroundpath{
    \path [draw,dashed] (0,0) -- (0,-2) -- (1,-2) -- cycle;
   }
}


\begin{document}
\begin{tikzpicture}[
sibling distance=5cm, 
level 2/.style={sibling distance =2cm},
triangle/.style={isosceles triangle,draw,shape border rotate=90, dashed, minimum height=10mm, minimum width=15mm, inner sep=0},
]
\node[circle,draw,fill=black] {}
    child{ node[circle, fill = black] {}
            child{ node[circle, draw] {}
                        child{ node[triangle] {Q1} } 
                }
            child{ node[rectangle, fill = black] {}
                    child {
                        node[rectangle, fill = black] {}
                            child{ node[left triangle] {Q2} }
                            child { node[circle,draw] {} 
                                   child{node[triangle] {Q3}}
                                }
                        }
                }
        }
    child{ node[circle, draw] {}
            child{node[left triangle] {Q4} }
            child{node[right triangle] {Q5} }       
        };
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容