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}
jub0bs
  • 58,916

1 Answers1

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

Qrrbrbirlbel
  • 119,821