[Tex/LaTex] Trees with only leaves in TikZ

foresttikz-pgftikz-treestrees

Following Roel's answer to How to write a tree with no (visible) root with TikZ? I attempted to visualise my Huffman-Code for "Tennessee", but it didn't turn out as I expected:enter image description here

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

Obviously the numbering on the edges is not being level and more importantly, the edges are overlapping.

I experimented with removing the style, but that just leaves the tree narrower yet does not resolve the issue of the missing edge alignment.

Best Answer

You are supposed to put the edge from parent operation at the end of the corresponding child path declaration, like in the following :

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

I added styles to get a cleaner code, and redefined edge from parent path to align the nodes (redefining child anchor and parent anchor should be enough, but there seems to be a bug with it in my version of Tikz)

Related Question