3

Is there a way to give every edge on the left the same label up to a specific level (I don't want labels on my leaf nodes).

I want to build a Huffman-Code, so it would be nice to have every left edge labeled with a 0 and every right edge labeled with a 1. Is there a way to do this with options or automated?

Here is my tree; it's rather big und will only fit onto an A4 landscape page:

\begin{tikzpicture}[every tree node/.style={draw,circle},
    level distance=15mm,
    sibling distance=8mm,
    edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
\tikzset{frontier/.style={distance from root=90mm}}
\Tree
[ .205
    \edge node[auto=right]{0};
    [ .82
        \edge node[auto=right]{0};
        [ .40
            \edge node[auto=right]{0}; 
            [ .20
                \edge node[auto=right]{0}; 
                [ .10
                    \edge node[auto=right]{0};
                    [ .5 <nl> ]
                    \edge node[auto=left]{1};
                    [ .5 I  ]
                ]
                \edge node[auto=left]{1};
                [ .10 l ]
            ]
            \edge node[auto=left]{1};
            [ .20 t ]
        ]
        \edge node[auto=right]{0};
        [ .42
            \edge node[auto=right]{0};
            [ .20
                \edge node[auto=right]{0};
                [ .10
                    \edge node[auto=right]{0};
                    [ .5 b ]
                    \edge node[auto=left]{1};
                    [ .5 d ]
                ]
                \edge node[auto=left]{1};
                [ .10 p ]
            ]
            \edge node[auto=left]{1};
            [ .22
                \edge node[auto=right]{0};
                [ .12
                    \edge node[auto=right]{0};
                    [ .5 k ]
                    \edge node[auto=left]{1};
                    [ .7 u  ]
                ]
                \edge node[auto=left]{1};
                [ .10 s ]
            ]
        ]
    ]
    \edge node[auto=left]{1};
    [ .123
        \edge node[auto=right]{0};
        [ .53
            \edge node[auto=right]{0};
            [ .24
                \edge node[auto=right]{0};
                [ .12 i ]
                \edge node[auto=left]{1};
                [ .12 o  ]
            ]
            \edge node[auto=left]{1};
            [ .29 l ]
        ]
        \edge node[auto=left]{0};
        [ .70
            \edge node[auto=right]{0};
            [ .30
                \edge node[auto=right]{0};
                [ .15 a ]
                \edge node[auto=left]{1};
                [ .15 n ]
            ]
            \edge node[auto=left]{1};
            [ .40 <sp>  ]
        ]
    ]
]
\end{tikzpicture}
3
  • Welcome to TeX.sx! Usually, we don't put a greeting or a “thank you” in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say “thank you” to users who helped you.
    – jub0bs
    Commented Apr 25, 2013 at 10:10
  • Welcome to TeX.SX. It is always better to add a minimal working example (MWE) rather than code snippets. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. Commented Apr 25, 2013 at 10:19
  • Is this what you are looking for: tex.stackexchange.com/questions/68127/… ? You can also try a work-around with graphviz if you feel this is easier. Afterwards you can convert it to TikZ.
    – C.Colden
    Commented Apr 25, 2013 at 11:02

1 Answer 1

7

Here a solution with forest.

It is also possible to let TeX add the values of the children so that one would only need to give the structure of the tree and the values of the second-last. But that needs to be worked out.

A few ideas on what is possible with forest: [1], [2].

Code

\documentclass[tikz]{standalone}
\usepackage{forest}
\tikzset{el style/.style={midway, font=\scriptsize, inner sep=+1pt, auto=right}}
\forestset{angled/.style={
    content/.expanded={\noexpand\textless\forestov{content}\noexpand\textgreater}}}
\begin{document}
\begin{forest}
  for tree={parent anchor=south},
  where n children={0}{tier=word}{
    if={n==1}{% n == 1 means first child
      edge label={node[el style]{0}}
    }{
      edge label={node[el style, swap]{1}}
    }
  }
%
[205 [ 82 [40 [20 [10 [5 [nl, angled] ]
                      [5 [I] ] ]
                  [10 [l ] ] ]
              [20 [t ] ] ]
          [42 [20 [10 [5 [b] ]
                      [5 [d] ] ]
                  [10 [p] ] ]
              [22 [12 [5 [k] ]
                      [7 [u] ] ]
                  [10 [s ] ] ] ] ]
     [123 [53 [24 [12 [i] ]
                  [12 [o] ] ]
              [29 [l ] ] ]
          [70 [30 [15 [a] ]
                  [15 [n] ] ]
              [40 [sp, angled] ] ] ] ]
\end{forest}
\end{document}

Output

enter image description here

You must log in to answer this question.

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