二叉树的帮助

二叉树的帮助

我想使用 Tikz 创建一个二叉树,如下所示: 到目前为止我已经尝试过了,但是节点的大小发生了变化,我一点也不喜欢它。

\begin{figure}[h]
\centering
\begin{tikzpicture}[level distance=1.5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=2cm},
leaf/.style={isosceles triangle,draw,shape border rotate=90,isosceles triangle stretches=true, minimum height=15mm,minimum width=12mm,inner sep=0,yshift={-0.95cm}}]

\node (Root) [double, circle, draw] {$\tilde{p}_1$}
    child {
        node [leaf] {$\mathcal{A}_1$}
        [child anchor = north]
        edge from parent [->]
    }
    child {
        node [double, circle, draw] {$\tilde{p}_2$}
        child {
            node [leaf] {$\mathcal{A}_{2}$}
            [child anchor = north]
            edge from parent
        }
        child {
            node [double, circle, draw] {$\tilde{p}_{n-1}$}
            child {
                node [leaf] {$\mathcal{A}_{n-1}$}
                [child anchor = north]
                edge from parent [->]
            }
            child {
                node [leaf] {$\mathcal{A}_{n}$}
                [child anchor = north]
                edge from parent [->]
            }
            edge from parent [draw=none] node[sloped] {$\ldots$}
        }
    };

\end{tikzpicture}
\end{figure}

答案1

您可以使用forest

\documentclass{article}
\usepackage{forest}
\usetikzlibrary{shapes.geometric}
\tikzset{leaf/.style={isosceles triangle,draw,shape border rotate=90,isosceles
triangle stretches=true, minimum height=15mm,minimum width=14mm,inner
sep=0,text width=2em,align=center},
cc/.style={circle,draw,double,minimum width=3em}}
\begin{document}
\begin{forest}
for tree={where n children=0{leaf}{cc},math content,s sep+=1em}
[P_1
 [\mathcal{A}_1]
 [P_2,alias=P2
  [\mathcal{A}_2]
  [,phantom
   [,phantom]
   [P_{n-1},alias=Pn-1
    [{\makebox[0pt]{$\mathcal{A}_{n-1}$}}]
    [\mathcal{A}_n]
   ]
  ]
 ]
]
\draw[dashed] (P2) -- (Pn-1);
\end{forest}
\end{document}

在此处输入图片描述

相关内容