2

I have a branched tree that currently just shows nodes. I want to label it a bit more, but I’m not sure how to do that. I’ve included the code I have so far, and also a picture of what I want it to look like. How should I do this?

enter image description here

\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
    Please always post a complete example we can compile. You probably don't want to use center inside a figure environment as it will add too much space. Generally, inside a figure or table, it is better to use \centering. – cfr Jan 26 '17 at 01:34

2 Answers2

2

In case somebody must draw many of these, here's a Forest style, nodes alone, which formats and labels the tree automatically. Obviously, the details of what goes in the labels can be customised as required. I'm assuming that the numbers in the MWE represent the level number (t=0, t=1, ... and R_0=??, R_1=??, ...) and the number of nodes at that level (1, 1, 2, 1, 3). Modify as desired.

I've included a second, larger example just to illustrate the potential: once the style is defined, the trees themselves can be specified very concisely.

demo trees

For instance, the original tree can be produced with

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

and the larger demonstration tree with

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

Complete code:

\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}
cfr
  • 198,882
1

enter image description here

\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}
gernot
  • 49,614