tikz を使用してツリー ダイアグラムのノードにラベルを付ける方法

tikz を使用してツリー ダイアグラムのノードにラベルを付ける方法

現在はノードのみを表示する分岐ツリーがあります。もう少しラベルを付けたいのですが、どうすればよいかわかりません。これまでのコードと、どのような外観にしたいかを示した画像も添付しました。どうすればよいでしょうか?

ここに画像の説明を入力してください

\begin{figure}[h]
\begin{center}
\begin{tikzpicture}
    \tikzstyle{every node}=[circle,inner sep=1.5pt,draw,fill]
\draw node {} child {node {}
    child {
        node {}
        child { node {} child { node {} }child {node {}} child {node {}  }}
    }
    child { node {} }}

;
\end{tikzpicture}
\caption{ Branched tree}
\end{center}
\end{figure}

答え1

ここに画像の説明を入力してください

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}%
   [tn/.style={circle,inner sep=1.5pt,draw,fill}% tree node
   ]
   \draw 
     node[tn] (1) {}
       child { node[tn] (11) {}
         child { node[tn] (111) {}
           child { node[tn] (1111) {}
             child { node[tn] (11111) {} }
             child { node[tn] (11112) {} }
             child { node[tn] (11113) {} }
           }
         }
         child { node[tn] (112) {}}
       };
    \node[left =of 11111] (t4) {$t=4$};
    \node      at (1    -| t4) {$t=0$};
    \node      at (11   -| t4) {$t=1$};
    \node      at (111  -| t4) {$t=2$};
    \node      at (1111 -| t4) {$t=3$};
    \node[right=of 11113] (R4) {$R_4=3$};
    \node      at (1    -| R4) {$R_0=1$};
    \node      at (11   -| R4) {$R_1=1$};
    \node      at (111  -| R4) {$R_2=2$};
    \node      at (1111 -| R4) {$R_3=1$};
\end{tikzpicture}
\end{document}

答え2

誰かがこのようなツリーをたくさん描かなければならない場合に備えて、nodes aloneツリーを自動的にフォーマットしてラベルを付ける Forest スタイル を用意しました。もちろん、ラベルに何を入れるかの詳細は必要に応じてカスタマイズできます。MWE の数字はレベル番号 (t=0, t=1, ...R_0=??, R_1=??, ...) とそのレベルのノード数 ( 1, 1, 2, 1, 3) を表していると想定しています。必要に応じて変更してください。

可能性を示すために、2 番目の大きな例を追加しました。スタイルが定義されると、ツリー自体を非常に簡潔に指定できます。

デモツリー

例えば、元のツリーは次のように生成できます。

\begin{forest}
  nodes alone
  [, baseline
    [
      [
        [
          [][][]
        ]
      ]
      []
    ]
  ]
\end{forest}

そして、より大きなデモンストレーションツリーには

\begin{forest}
  nodes alone
  [, baseline
    [
      [
        [
          [][][]
        ]
      ]
      [[[[[[[[][[[[[]]][]]][[][]]][][][[]]]]][[[][]]]][[]]][[][[][]]]]
    ]
    [
      [[[]][][[[]]]]
    ]
  ]
\end{forest}

完全なコード:

\documentclass[border=10pt]{standalone}
\usepackage{forest}
\forestset{
  nodes alone/.style={
    for tree={
      parent anchor=center,
      child anchor=center,
      anchor=center,
      inner sep=1.5pt,
      circle,
      fill,
      s sep'+=10pt,
    },
    before typesetting nodes={
      tempcounta/.max={>O{level}}{r,tree},
      for nodewalk={
        root,
        tikz+={
          \coordinate (w) at (current bounding box.west);
          \coordinate (e) at (current bounding box.east);
        },
        until={>OR={level}{tempcounta}}{next node},
        Nodewalk={}{current and ancestors}{
          tempcountc/.option=level,
          tempcountd'=0,
          Nodewalk={}{filter={fake=root,tree}{>OR={level}{tempcountc}}}{tempcountd'+=1},
          tikz+/.process={
            ORw2{level}{tempcountd}{
              \node [anchor=east, xshift=-10mm] at (w |- .center) {$t=##1$};
              \node [anchor=west, xshift=10mm] at (e |- .center) {$R_{##1}=##2$};
            }
          },
        }
      }{},
    },
  },
}
\begin{document}
\begin{forest}
  nodes alone
  [, baseline
    [
      [
        [
          [][][]
        ]
      ]
      []
    ]
  ]
\end{forest}
\begin{forest}
  nodes alone
  [, baseline
    [
      [
        [
          [][][]
        ]
      ]
      [[[[[[[[][[[[[]]][]]][[][]]][][][[]]]]][[[][]]]][[]]][[][[][]]]]
    ]
    [
      [[[]][][[[]]]]
    ]
  ]
\end{forest}
\end{document}

関連情報