2

I have an tree and I need to make it so it feels like it's infinite. I need to draw an ellipsis at the end of each branch. I would like the ellipsis to be vertical oriented if possible (not a big deal)

\documentclass[paper=a4, fontsize=10pt]{article} % A4 paper and 11pt font size

\usepackage{forest}

\begin{document}

        \begin{center}
            \begin{forest}
                for tree={
                    if n=1{edge label={node [midway, left, anchor=south] {0} } }{edge label={node [midway, right, anchor=south] {1} } },
                    draw,
                    circle,
                    if level=0{}{!u.s sep/.wrap pgfmath arg={#1}{35mm/(level())}},
                    anchor=mid,
                }
                [6
                    [2
                        [2, edge label={node [midway, left] {1} } 
                            [1
                                [1, edge label={node [midway, left] {1} } 
                                    [1, edge label={node [midway, left] {1} }
                                        [1, edge label={node [midway, left] {1} } ]
                                    ]
                                ]
                            ]
                            [1
                                [1, edge label={node [midway, left] {1} } 
                                    [1, edge label={node [midway, left] {0} } 
                                        [1, edge label={node [midway, left] {1} } 
                                            [..., draw=none]
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            \end{forest}
        \end{center}    
\end{document}

I can get rid of the circle of the node by not the edge and the 0 that appears.

Previous question Create a tree where all left edges are 0 and right edges are label 1 and node are circle draw

2
  • What are you trying to do with the labels right now? I get a very odd result.
    – cfr
    Commented Apr 13, 2016 at 22:59
  • 1
    @cfr I added the link to the previous question. Commented Apr 13, 2016 at 23:40

1 Answer 1

2

We can just make a final dotted edge to an empty node for all terminal nodes:

\documentclass[border=10pt,tikz,multi]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  for tree={
    if n=1{edge label={node [midway, left, anchor=south] {0} } }{edge label={node [midway, right, anchor=south] {1} } },
    draw,
    circle,
    if level=0{}{!u.s sep/.wrap pgfmath arg={#1}{35mm/(level())}},
    anchor=mid,
  },
  before typesetting nodes={
    where n children=0{%
      append={[, edge={dotted, shorten <=5pt} ]}
    }{}
  }
  [6
      [2
          [2, edge label={node [midway, left] {1} }
              [1
                  [1, edge label={node [midway, left] {1} }
                      [1, edge label={node [midway, left] {1} }
                          [1, edge label={node [midway, left] {1} } ]
                      ]
                  ]
              ]
              [1
                  [1, edge label={node [midway, left] {1} }
                      [1, edge label={node [midway, left] {0} }
                          [1, edge label={node [midway, left] {1} }
                          ]
                      ]
                  ]
              ]
          ]
      ]
  ]
\end{forest}
\end{document}

dotted edges

However, the current edge label code doesn't make any sense. Since I'm not sure what it is supposed to do at this point, I can't recommend how to correct it, however.

EDIT

I realise that the above was the answer you wanted, but the edge label stuff is still messy. It is hard to guess what might do the job as I don't know what the job is. (This is fair enough - you didn't ask anybody how to do the job, so there's no reason for you to have explained it.)

Here's one possibility to satisfy my own curiosity. The idea is to separate the location of the labels from their content.

By default, the labels mostly get the content 1 as most of them are 1 in the target image. This can be overridden as shown with my label=<something else>. If the node has a sibling, the default content of the left child is 0 rather than 1. (This is something like something you asked for in an earlier question which you've linked above, I believe.)

The labels are mostly placed to the left. In the case of a branch, the right child's label goes to the right and so do the labels of its children, so long as the branch remains 'straight' i.e. no branches.

The result is something like this

separate location & content of label code

which could obviously be modified according to your needs.

\documentclass[border=10pt,tikz,multi]{standalone}
\usepackage{forest}
\begin{document}
\forestset{%
  declare boolean={leftwards}{1},
  declare toks={my label}{1},
}
\begin{forest}
  for tree={
    draw,
    circle,
      if={n_children()>1}{
        for n=1{
          my label=0,
          for tree={leftwards},
          for descendants={my label=1}
        },
        for n'=1{
          for tree={not leftwards, my label=1}
        },
      }{},
    if level=0{}{%
      !u.s sep/.wrap pgfmath arg={#1}{35mm/(level())}
    },
    anchor=mid,
  },
  before typesetting nodes={
    where n children=0{%
      delay={%
        append={[, edge={dotted, shorten <=5pt} ]}
      }
    }{},
    where level=0{}{% this places the labels as specified
      if={n_children("!u")==1}{%
        if leftwards={%
          edge label/.wrap pgfmath arg={%
            node [midway, left, anchor=east] {#1}
          }{my_label()},
        }{%
           edge label/.wrap pgfmath arg={%
            node [midway, right, anchor=west] {#1}
          }{my_label()},
       }
      }{%
        if n=1{%
          edge label/.wrap pgfmath arg={%
            node [midway, left, anchor=south east] {#1}
          }{my_label()},
        }{%
          edge label/.wrap pgfmath arg={%
            node [midway, right, anchor=south west] {#1}
          }{my_label()},
        },
      },
    }
  }
  [6
    [2
      [2,
        [1
          [1,
            [1,
              [1,
              ]
            ]
          ]
        ]
        [1
          [1,
            [1, my label=0
              [1,
              ]
            ]
          ]
        ]
      ]
    ]
  ]
\end{forest}
\end{document}
6
  • This is exactly what I need Commented Apr 13, 2016 at 23:33
  • Great! You may or may not find my edit above of interest.
    – cfr
    Commented Apr 14, 2016 at 0:10
  • Looks like there is a bug, if the root has 2 child both are getting 1 Commented Apr 14, 2016 at 0:31
  • Oops. Should work now. At least, it shouldn't fail in the same way.
    – cfr
    Commented Apr 14, 2016 at 0:46
  • It's working, what was the modification? Commented Apr 14, 2016 at 0:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .