Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
Welcome to TeX.SX! Could you please describe a little more detailed what you want to achieve? Or draw a little sketch. Maybe its my poor English, but I do not really get the question. Thanks. –  LaRiFaRi Jul 28 at 12:03
    
Basically I would like the tree to be a smooth tree with the edges as a continous unbroken line (preferably) following the same angle, just as if inner nodes where indeed present. I would like it to look as the answer by Roel in the linked question does, except with edge labels and circled leaf nodes. –  BMWurm Jul 28 at 12:10

2 Answers 2

up vote 5 down vote accepted

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)

share|improve this answer

Another solution using forest package. The code for nice empty nodes is adapted from example 81 in forest documentation.

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

enter image description here

Gonzalo's version

Next code was provided by Gonzalo Medina. It automates edge labels.

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

enter image description here

share|improve this answer
2  
+1 I was writing something similar, so no point in duplicating answers. You can simplify a little your code, since the labels can be automated: –  Gonzalo Medina Jul 28 at 14:23
    
\documentclass{article} \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} –  Gonzalo Medina Jul 28 at 14:23
    
Sorry about the poor code formatting, but comments are a PITA for code :( –  Gonzalo Medina Jul 28 at 14:24
1  
@GonzaloMedina: I've included your code. Please feel free to edit. And thanks for your comment. I'm just starting to understand and use forest programming power so your examples are always helpful. –  Ignasi Jul 29 at 8:13
    
Brilliant solution. I accepted Tonio's answer mainly because it corrects my original coding - basically it answers the question what was wrong with it. However, if there is the possibility the tree is going to be expanded at some point (or there is more than one in the document), your solution is probably the way to go, especially after moving most of it in the preamble and then being able to easily plant trees everywhere with very little code per tree. I certainly am going to take a look into forest. –  BMWurm Jul 29 at 8:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.