2

... one upon the time ... :) I draw this tree

enter image description here

by CorelDraw! for presenting convolution coding rules, which I like now to redraw and modernize with help of forest package. I stuck at followings:

  • skip drawing nodes on the end of tree
  • placings edge labels on better/shorter way
  • make tree layout symmetrical at all tree levels

enter image description here

I kindly ask help me in the following to resolve above listed problems as well to add names to nodes in tree for later use, for example draw input line with condition for branching, for append some legend and auxiliary images or for callout nodes (balloons) for some explanations.

So far I manage to write the following code:

\documentclass[tikz,
               margin=3mm,
               preview]{standalone}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{forest}
%---------------------------------------------------------------%
    \begin{document}
   \begin{forest}
    for tree={% style of tree nodes
      circle, font=\small\sffamily,
             fill = blue!60!black,
     minimum size = 4mm, 
        inner sep = 0.5pt,
             text = white,
              % style of tree (edges, distances, direction)
             edge = {draw, semithick},
  edge path={
    \noexpand\path[\forestoption{edge}]
      (!u.parent anchor) |-
      (.child anchor) -- % intermediate point
      (.child anchor)\forestoption{edge label};
  },
             grow = east,
            s sep = 2mm,    % sibling distance
            l sep = 12mm,   % level distance
               }
[a
    [b,edge label={node[xshift=-9mm,fill=white]{11}}
        [d,edge label={node[xshift=-9mm,fill=white]{01}}
            [d,edge label={node[xshift=-9mm,fill=white]{10}}
                [,edge label={node[xshift=-9mm,fill=white]{10}}]
                [,edge label={node[xshift=-9mm,fill=white]{01}}]
            ]
            [c,edge label={node[xshift=-9mm,fill=white]{01}}
                [,edge label={node[xshift=-9mm,fill=white]{00}}]
                [,edge label={node[xshift=-9mm,fill=white]{11}}]
            ]
        ]
        [c,edge label={node[xshift=-9mm,fill=white]{10}}
            [b,edge label={node[xshift=-9mm,fill=white]{00}}
                [,edge label={node[xshift=-9mm,fill=white]{10}}]
                [,edge label={node[xshift=-9mm,fill=white]{01}}]
            ]
            [a,edge label={node[xshift=-9mm,fill=white]{11}}
                [,edge label={node[xshift=-9mm,fill=white]{11}}]
                [,edge label={node[xshift=-9mm,fill=white]{00}}]
            ]
        ]
    ]
    [a,edge label={node[xshift=-9mm,fill=white]{00}}
        [b,edge label={node[xshift=-9mm,fill=white]{11}}
            [d,edge label={node[xshift=-9mm,fill=white]{01}}
                [,edge label={node[xshift=-9mm,fill=white]{10}}]
                [,edge label={node[xshift=-9mm,fill=white]{01}}]
            ]
            [c,edge label={node[xshift=-9mm,fill=white]{10}}
                [,edge label={node[xshift=-9mm,fill=white]{00}}]
                [,edge label={node[xshift=-9mm,fill=white]{11}}]
            ]
        ]
        [a,edge label={node[xshift=-9mm,fill=white]{00}}
            [b,edge label={node[xshift=-9mm,fill=white]{11}}
                [,edge label={node[xshift=-9mm,fill=white]{01}}]
                [,edge label={node[xshift=-9mm,fill=white]{10}}]
            ]
            [a,edge label={node[xshift=-9mm,fill=white]{00}}
                [,edge label={node[xshift=-9mm,fill=white]{11}}]
                [,edge label={node[xshift=-9mm,fill=white]{00}}]
            ]
        ]
    ]
];
    \end{forest}
\end{document}
  • Is there a pattern to the labels? – cfr Mar 18 '17 at 0:09
  • Your partial labelling doesn't match the target image, which doesn't help. – cfr Mar 18 '17 at 0:14
  • @cfr, yes, labels has pattern (maybe not so obvious). I edit my question and add all labels to edges and also try to make my question more clear. I check labels an as far as I can see, they are now equal to labels on target image. – Zarko Mar 18 '17 at 5:59
  • Well, what is the pattern? Is there a rule? – cfr Mar 18 '17 at 22:34
2

Hard to say about the labelling without knowing what it should look like. You can define a variable to hold it and use that, define a style or use one of various automated-labelling options, if there's a pattern.

The rest is fairly straightforward, I think.

  • Distinguish terminal nodes from the others by testing to see if they have any children.

  • Use name to name a node for later reference.

  • Use invisible terminal nodes for spacing and symmetry.

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage{forest}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\forestset{
  declare toks={way label}{},
}
\begin{document}
\begin{forest}
  for tree={% style of tree nodes
    semithick,
    font=\small\sffamily,
    edge = {draw, semithick},
    grow = 0,
    s sep = 2mm,    % minimum sibling distance
    l sep = 12mm,   % minimum level distance
    edge path'={
      (!u) |- (.child anchor)\forestoption{edge label}
    },
    circle,
    minimum size=4mm,
    inner sep=.5pt,
    anchor=center,
  },
  where n children=0{}{
    draw,
    fill = blue!60!black,
    text = white,
  },
  before typesetting nodes={
    where way label={}{}{
      edge label={
        node [pos=.75, fill=white, font=\sffamily] {\forestoption{way label}}
      },
    },
  }
  [a
    [b, way label=11
      [d
        [d
          []
          []
        ]
        [c
          []
          []
        ]
      ]
      [c
        [b
          []
          []
        ]
        [a
          []
          []
        ]
      ]
    ]
    [a
      [b
        [d, name=zarko
          []
          []
        ]
        [c
          []
          []
        ]
      ]
      [a
        [b
          []
          []
        ]
        [a, name=bob
          [, way label=11
          ]
          [, for current and ancestors={way label=00}
          ]
        ]
      ]
    ]
  ]
  \draw [<->, shorten >=20pt, shorten <=20pt] (zarko) -- (current bounding box.east |- zarko) -- ++(5pt,0) |- (bob);
\end{forest}
\end{document}

adjusted tree

  • almost done! Now I need first to understand all your changes/improvements to my MWE, This will take me a while :) – Zarko Mar 18 '17 at 6:02
  • I have problem to understand syntax of where n children=0{}{...}, all others make me less troubles :). Only thing which is left over is how to consider common \tikzset{...} (defined in document preamble) in images , which I like to add to forest. Thank you for nice answer. – Zarko Mar 18 '17 at 10:03
  • @Zarko It just tests if the node has any children. If not, do nothing. Otherwise, draw, colour and fill. It is just excluding the terminal nodes and do something to all the rest. – cfr Mar 18 '17 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.