控制 tikz 树

控制 tikz 树

我有一棵树如图 1 所示,我想修改它以获得如图 2 所示的结果。我尝试过但尚未找到解决方案。非常感谢您的帮助。我需要解决三个任务:任务 1:在水平方向上旋转“是”和“否”标签。任务 2:将一个分支拆分为两个叶节点,如图 2 所示。在图 1 中,我暂时使用圆圈来完成此任务。任务 3:在每个叶节点下方插入标签。

\documentclass[border=10pt]{standalone} 
\usepackage{tikz}
\tikzset{
  treenode/.style = {shape=rectangle, rounded corners,
                     draw, align=center, font=\scriptsize},
  root/.style     = {treenode},
  env/.style      = {treenode},
  leaf/.style     = {shape=circle,draw,align=center,font=\scriptsize},
  every node/.style       = {font=\tiny},
  dummy/.style    = {circle,draw}
}
\begin{document}
\begin{tikzpicture}
  [
    grow                    = down,
    % sibling distance        = 10em,
    level 1/.style          = {sibling distance=3.5cm},
    level 2/.style          = {sibling distance=2cm},
    level 3/.style          = {sibling distance=1.2cm},
    level distance          = 1.2cm,
    edge from parent/.style = {draw, edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}},
    sloped
  ]
  \node [root] {A}
    child { node [env] {B}
        child { node [dummy] {}
            child{ node [leaf] {f}}
            child{ node [leaf] {d}}
            edge from parent node [above] {Yes}
        }
        child{ node [leaf] {d}
            edge from parent node [above] {No}
        }
        edge from parent node [above] {Yes}
      }
      child { node [env] {C}
        child{ node [env] {D}
                child{ node [leaf] {c}
                edge from parent node [above] {Yes}}
                child{ node [leaf] {a}
                edge from parent node [above] {No}}
        edge from parent node [above] {Yes}
        }
        child{ node [env] {E}
                    child { node [dummy] {}
                    child{ node [leaf] {c}}
                    child{ node [leaf] {d}}
                    edge from parent node [above] {Yes}}
                    child{ node [leaf] {f}
                    edge from parent node [above] {No}}
            edge from parent node [above] {No}
        }
        edge from parent node [above] {No}
      };
\end{tikzpicture}
\end{document}

图1

图 2

答案1

至于任务,

  1. 只需将圆圈变成一个点,
  2. 降低sloped
  3. 添加label=below:Leaf <n>

这假设您想随意更改标签。

\documentclass[border=10pt]{standalone} 
\usepackage{tikz}
\tikzset{
  treenode/.style = {shape=rectangle, rounded corners,
                     draw, align=center, font=\scriptsize},
  root/.style     = {treenode},
  env/.style      = {treenode},
  leaf/.style     = {shape=circle,draw,align=center,font=\scriptsize},
  every node/.style       = {font=\tiny},
  dummy/.style    = {coordinate}
}
\begin{document}
\begin{tikzpicture}
  [
    grow                    = down,
    % sibling distance        = 10em,
    level 1/.style          = {sibling distance=3.5cm},
    level 2/.style          = {sibling distance=2cm},
    level 3/.style          = {sibling distance=1.2cm},
    level distance          = 1.2cm,
    edge from parent/.style = {draw, edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}},
    %sloped
  ]
  \node [root] {A}
    child { node [env] {B}
        child { node [dummy] {}
            child{ node [leaf,label=below:Leaf 1] {f}}
            child{ node [leaf,label=below:Leaf 2] {d}}
            edge from parent node [above left] {Yes}
        }
        child{ node [leaf,label=below:Leaf 3] {d}
            edge from parent node [above right] {No}
        }
        edge from parent node [above left] {Yes}
      }
      child { node [env] {C}
        child{ node [env] {D}
                child{ node [leaf,label=below:Leaf 4] {c}
                edge from parent node [above left] {Yes}}
                child{ node [leaf,label=below:Leaf 5] {a}
                edge from parent node [above right] {No}}
        edge from parent node [above left] {Yes}
        }
        child{ node [env] {E}
                    child { node [dummy] {}
                    child{ node [leaf,label=below:Leaf 6] {c}}
                    child{ node [leaf,label=below:Leaf 7] {d}}
                    edge from parent node [above left] {Yes}}
                    child{ node [leaf,label=below:Leaf 8] {f}
                    edge from parent node [above right] {No}}
            edge from parent node [above right] {No}
        }
        edge from parent node [above right] {No}
      };
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

为了锻炼,并寻找另一种可能的解决方案:-)

附带forest包装:

  • 而是在空节点选项中使用小圆圈coordinate
  • 其他愿望也得到了解决@薛定谔的猫回答
\documentclass[border=10pt]{standalone}
\usepackage[edges]{forest}
\tikzset{
 rbox/.style = {rectangle, rounded corners, draw, outer sep=0pt},
every label/.append style = {inner sep=1pt, font=\tiny}
        } % end of tikzset
\forestset{EL/.style 2 args={edge label={%
    node[midway, font=\scriptsize,  anchor=#1]{#2}},
                     },
           }% end of forestset

\begin{document}
    \begin{forest}
for tree={
% nodes
    shape=circle, draw, font=\footnotesize, inner sep=2pt,    
% edges
  calign = fixed edge angles,
   delay = {where content={}{shape=coordinate}{}},% empty nodes are dummy
   where level = {0}{calign angle=60}{calign angle=30}
        }% end of for tree
[A, rbox
    [B,rbox,EL={east}{Yes}
        [,EL={east}{Yes},
            [f, label=below:Leaf 1]
            [d, label=below:Leaf 2]
        ]
        [d, label=below:Leaf 3, EL={west}{No}]
    ]
     [C,rbox,EL={west}{No},calign angle=45
        [D, rbox,EL={east}{Yes}
            [c, label=below:Leaf 4,EL={east}{Yes}] 
            [a, label=below:Leaf 5,EL={west}{No}]
        ]
        [E, rbox, EL={west}{No}
            [, EL={east}{Yes}, 
                [c, label=below:Leaf 6]
                [d, label=below:Leaf 7]
            ]
            [f, EL={west}{No}]
        ]
    ]
]
\end{forest}
\end{document}

在此处输入图片描述

相关内容