TikZ - 每个左/右边缘的标签相同

TikZ - 每个左/右边缘的标签相同

有没有办法给左边的每一条边赋予相同的标签,直到特定的级别(我不希望我的叶节点上有标签)。

我想构建一个 Huffman 编码,因此最好将每个左边缘标记为 0,将每个右边缘标记为 1。有没有办法通过选项或自动化来实现这一点?

这是我的树;它相当大,只能容纳在 A4 横向页面上:

\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}

答案1

这里有一个解决方案forest

也可以让 TeX 添加子元素的值,这样只需要给出树的结构和倒数第二个元素的值。但这需要解决。

关于可能实现的功能的一些想法forest[1][2]

代码

\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}

输出

在此处输入图片描述

相关内容