Tikz 绘图:子节点定位

Tikz 绘图:子节点定位

我想要实现在 n 级中有 n 个节点的这种绘图。(忽略圆的不规则性) 在此处输入图片描述

http://www.texample.net/tikz/examples/coin-flipping/,它在第 3 级有 4 个节点。当我在代码中取消一个节点(通过注释该节点)时,子节点直接进入另一个节点下方。 在此处输入图片描述

\documentclass[border=10pt,varwidth]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, shapes, backgrounds}
\usepackage{amsmath, amssymb}
\pagecolor{olive!50!yellow!50!white}
\begin{document}
\tikzset{
  head/.style = {fill = orange!90!blue,
                   label = center:\textsf{\Large H}},
    tail/.style = {fill = blue!70!yellow, text = black,
                   label = center:\textsf{\Large T}}
  }
  \begin{tikzpicture}[
      scale = 1.5, transform shape, thick,
      every node/.style = {draw, circle, minimum size = 10mm},
      grow = down,  % alignment of characters
      level 1/.style = {sibling distance=3cm},
      level 2/.style = {sibling distance=4cm}, 
      level 3/.style = {sibling distance=2cm}, 
      level distance = 1.25cm
    ]
    \node[fill = gray!40, shape = rectangle, rounded corners,
      minimum width = 6cm, font = \sffamily] {Coin flipping} 
    child { node[shape = circle split, draw, line width = 1pt,
            minimum size = 10mm, inner sep = 0mm, font = \sffamily\large,
            rotate=30] (Start)
            { \rotatebox{-30}{H} \nodepart{lower} \rotatebox{-30}{T}}
     child {   node [head] (A) {}
       child { node [head] (B) {}}
       child { node [tail] (C) {}}
     }
     child {   node [tail] (D) {}
      % child { node [head] (E) {}}
       child { node [tail] (F) {}}
     }
    };

    % Filling the root (Start)
    \begin{scope}[on background layer, rotate=30]
      \fill[head] (Start.base) ([xshift = 0mm]Start.east) arc (0:180:5mm)
        -- cycle;
      \fill[tail] (Start.base) ([xshift = 0pt]Start.west) arc (180:360:5mm)
        -- cycle;
    \end{scope}

    % Labels
    \begin{scope}[nodes = {draw = none}]
      \path (Start) -- (A) node [near start, left]  {$0.5$};
      \path (A)     -- (B) node [near start, left]  {$0.5$};
      \path (A)     -- (C) node [near start, right] {$0.5$};
      \path (Start) -- (D) node [near start, right] {$0.5$};
            %\path (D)     -- (E) node [near start, left]  {$0.5$};
      \path (D)     -- (F) node [near start, right] {$0.5$};
      \begin{scope}[nodes = {below = 11pt}]
        \node [name = X] at (B) {$0.25$};
        \node            at (C) {$0.25$};
        %\node [name = Y] at (E) {$0.25$};
        \node            at (F) {$0.25$};
      \end{scope}

    \end{scope}
  \end{tikzpicture}
  \end{document}

我怎样才能使节点的位置像上面的原始绘图那样?

答案1

像这样?

在此处输入图片描述

由于您没有付出努力,我帮助提供给定链接上的代码。我只评论我认为不必要的内容并更改子内容

child {   node [tail] (E) {}

child[missing] { node [head] (E) {}}

完整的 MWE 是:

\begin{tikzpicture}[
    thick, 
every node/.style = {circle, minimum size = 10mm, 
                     font = \sffamily\large, text = black},
      head/.style = {draw, fill = orange!90!blue, label=center:\textsf{\Large H}},
      tail/.style = {draw, fill = blue!60!yellow, label=center:\textsf{\Large T}},
      prob/.style = {draw=none, minimum size=0pt, pos=0.25},
   related/.style = {rectangle, draw=none, minimum size=0pt},
% 
    level 2/.style = {sibling distance=4cm},
    level 3/.style = {sibling distance=2cm},
    level distance = 1.5cm
                    ]
\node[fill=gray!20, shape = rectangle, rounded corners, draw,
        minimum width = 32mm, minimum height = 8mm] {Coin flipping}
    child {node (A) [shape = circle split, draw, 
                 inner sep = 0mm, rotate=45] (Start)
          { \rotatebox{-45}{H} \nodepart{lower} \rotatebox{-45}{T}
          }
    child {node (B) [head] {}
        child {node (C) [head,label=below:0.25] {}
               edge from parent node[prob,left] {0.5}}
        child {node (D) [tail,label=below:0.25] {}
               edge from parent node[prob,right] {0.5}}
        edge from parent node[prob,left] {0.5}
            }
    child {node (E) [tail] {}
        child[missing] { (F) node [head] {}}
        child {node (G) [tail] {}
            child {node (H) [head,label=below:0.25] {} 
                   edge from parent node[prob,left] {0.5}}
            child {node (I) [tail,label=below:0.25] {}
                   edge from parent node[prob,right] {0.5}}
            edge from parent node[prob,right] {1}% changed probability
               }
    edge from parent node[prob,right] {0.5}% changed probability
        }
    };
% Line between nodes C and E
\draw[draw=red,<->,shorten >=1mm,shorten <=1mm]
    (D) -- node[related,above] {related \dots} (G);
% Filling the root (Start)
    \begin{scope}[on background layer, rotate=45]
\fill[head] (Start.base) (Start.east) arc (  0:180:5mm) -- cycle;
\fill[tail] (Start.base) (Start.west) arc (180:360:5mm) -- cycle;
  \end{scope}
\end{tikzpicture}
\end{document}

编辑:好了,现在我明白了,之前我没有像评论中承诺的那样上传我答案的最后一个版本。现在这个问题已经纠正了。

答案2

我不知道您的规律性程度如何,但是,以下内容可以帮助您绘制您所描述的第 n 级的 n 个节点。

\documentclass[border=4pt,tikz]{standalone}
\usetikzlibrary{calc, positioning}
\begin{document} 

\begin{tikzpicture}[ht/.style={circle,draw,minimum size=3em},node distance=4em and 2em,line width=.7pt]    
  \node[ht] (n0) {}; 
  \foreach \x [evaluate=\x as\i using 5-\x] in {1,...,4}{
    \foreach \y [count=\j from 0] in {1,...,\i}{
      \node[ht, below right= of n\j] (n\y) {};
      \draw (n\j)--node[right]{0.5}(n\y);
    }      
    \node[ht,draw=none](n0old) at (n0){};
    \node[ht, below left= of n0] (n0) {};
    \draw (n0old)--node[left]{0.5}(n0);
  }
  \foreach \nf in {0,...,4} \node[below= .5em of n\nf] {0.25};      
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容