TikZ 中只有叶子的树

TikZ 中只有叶子的树

按照 Roel 的回答如何用 TikZ 编写没有(可见)根的树?我尝试将“田纳西州”的霍夫曼编码形象化,但结果并不像我预期的那样:在此处输入图片描述

我的 MWE:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{trees,arrows}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
    \begin{tikzpicture}[-,>=stealth',level/.style={sibling distance = 3cm, level distance = 1.5cm}] 
    \coordinate
            child {node [circle,draw] {E} edge from parent node[left] {\textcolor{red}{0}}} 
        child {edge from parent node[right]{\textcolor{red}{1}} 
            child {node [circle,draw] {S} edge from parent node[left] {\textcolor{red}{0}}}
            child {edge from parent node[right]{\textcolor{red}{1}}
                    child {node [circle,draw]{N} edge from parent node[left] {\textcolor{red}{0}}}
                    child {node [circle,draw]{T} edge from parent node[right] {\textcolor{red}{1}}}
                }
        };
\end{tikzpicture}
\end{document}

显然边缘上的编号是不均匀的,更重要的是,边缘是重叠的。

我尝试过删除样式,但这只会让树变窄,但不是解决缺失边缘对齐的问题。

答案1

您应该将edge from parent操作放在相应路径声明的末尾child,如下所示:

\begin{tikzpicture}[-,>=stealth',level/.style={sibling distance = 3cm, level distance = 1.5cm}, leaf/.style={circle, draw}, label/.style={red},edge from parent path={(\tikzparentnode.south) -- (\tikzchildnode)}] 
    \coordinate[child anchor=south]
        child {node[leaf] {E} edge from parent node[left, label] {0}} 
        child {
            child {node [leaf] {S} edge from parent node[left, label] {0}}
            child {
                child {node [leaf]{N} edge from parent node[left, label] {0}}
                child {node [leaf]{T} edge from parent node[right, label] {1}}
                edge from parent node[right, label]{1}
            }
            edge from parent node[right, label]{1}
        };
\end{tikzpicture}

我添加了样式以获得更清晰的代码,并重新定义edge from parent path以对齐节点(重新定义child anchor应该parent anchor足够了,但我的 Tikz 版本中似乎有一个错误)

答案2

另一种解决方案是使用forest包。的代码nice empty nodes改编自forest文档中的示例 81。

\documentclass[tikz, border=3mm]{standalone}
\usepackage{forest}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\forestset{
    nice empty nodes/.style={
    for tree={calign=fixed edge angles},
    delay={where content={}{shape=coordinate, for parent={for children={anchor=north}}}{circle, draw}}
},
    zero/.style={edge label={node[midway, left] {0}}},
    one/.style={edge label={node[midway, right] {1}}},
}

    \begin{forest}
    [ , nice empty nodes,  
        [E, zero] 
        [, one
            [S, zero] 
            [,one
                [N, zero]
                [T, one]
            ]
        ]
    ]
\end{forest}
\end{document}

在此处输入图片描述

贡萨洛的版本

下一个代码由贡萨洛·梅迪纳. 它可实现边缘标签的自动化。

\documentclass[border=3mm]{standalone} 
\usepackage{forest}  
\begin{document}  
\begin{forest} 
    for tree={%   
        calign=fixed edge angles,   
        delay={where content={}{shape=coordinate}{}},   
        parent anchor=south,   
        where n children=0{circle,draw}{},   
        s sep=1.5cm,   
        if n=1 { 
            edge label={%     
                node[midway,auto,swap,font=\color{red}\scriptsize]{0}}     
        }{%
            if n'=1 {
                edge label={%      
                    node[midway,auto,font=\color{red}\scriptsize]{1}}      
            }{}    
        } 
    }  
    [ [E] [ [S] [ [N] [T] ] ] ] 
\end{forest}  
\end{document}

在此处输入图片描述

相关内容