3

I have the following code. If I remove the edge commands, everything is fine. I suppose the problem is that my nodes have the same contents, but I can't avoid this obviously.

Bad code:

\documentclass[tikz,margin=10pt]{standalone}

\usetikzlibrary{positioning,shapes,arrows}

\begin{document}

\tikzset{
  treenode/.style = {align=center, inner sep=0pt, text centered,
    font=\sffamily},
}

\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 5cm/#1, level distance = 1.5cm}] 
 \node (n) {  0  } 
 child { node  (n0)  {  0  } 
 edge from parent node[left] {  0  } 
 child { node  (n00)  {  0  } 
 edge from parent node[left] {  0  } 
 } 
 child { node  (n01)  {  1  } 
 edge from parent node[left] {  1  } 
 } 
 } 
 child { node  (n1)  {  1  } 
 edge from parent node[left] {  1  } 
 child { node  (n10)  {  0  } 
 edge from parent node[left] {  0  } 
 } 
 child { node  (n11)  {  1  } 
 edge from parent node[left] {  1  } 
 } 
 } 
 ; 
 \end{tikzpicture}
\end{document}

\end{document}

Good code (but without labels):

\documentclass[tikz,margin=10pt]{standalone}

\usetikzlibrary{positioning,shapes,arrows}

\begin{document}

\tikzset{
  treenode/.style = {align=center, inner sep=0pt, text centered,
    font=\sffamily},
}

\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 5cm/#1, level distance = 1.5cm}] 
 \node (n) {  0  } 
 child { node  (n0)  {  0  } 
 child { node  (n00)  {  0  } 
 } 
 child { node  (n01)  {  1  } 
 } 
 } 
 child { node  (n1)  {  1  } 
 child { node  (n10)  {  0  } 
 } 
 child { node  (n11)  {  1  } 
 } 
 } 
 ;
\end{tikzpicture}
\end{document}

\end{document}

2 Answers 2

1

Have a look at the following code. You have to position your edges at the right position and it will work. You have to paste the edge after all child nodes. If you insert it before it creates (i assume so) a new node which is taken as root node for all following childs.

If you try to add additional child in your code to your lowest nodes you'll see the issue.

\documentclass[tikz,margin=5mm]{standalone}

\begin{document}
 \begin{tikzpicture}[->, %
                    >=stealth, %
                    level distance=1.5cm, %
                    level 1/.style={sibling distance=2.5cm}, %
                    level 2/.style={sibling distance=1.5cm}]
  \node (n) {0}
   child { node (n0) {0} {
    child { node (n00) {0} edge from parent node [left] {0} }
    child { node (n01) {1} edge from parent node [left] {1} }
    } edge from parent node [left] {0}
   }
   child { node (n1) {1} {
    child { node (n10) {0} edge from parent node [left] {0} }
    child { node (n11) {1} edge from parent node [left] {1} }
    } edge from parent node [left] {1}
   }
  ;
 \end{tikzpicture}
\end{document}

Rendered image:

rendered image

1
  • ah yeah, perfect! I didn't think of this issue.
    – yo'
    Sep 25, 2014 at 9:14
1

From the pgfmanual:

\path . . . edge from parent[<Options>] . . . ; This path operation can only be used inside <child paths> and should be given at the end, possibly followed by <node specifications> like node {a}. If a <child path> does not contain this operation, it will be added at the end of the <child path> automatically. \documentclass[tikz,margin=10pt]{standalone} \usetikzlibrary{arrows}

\begin{document}
\begin{tikzpicture}[
    ->,
    >=stealth',
    level/.style={sibling distance = 5cm/#1, level distance = 1.5cm}
  ] 
  \node (n) {  0  } 
    child { node  (n0)  {  0  } 
      child { node  (n00)  {  0  } 
        edge from parent node[left] {0}
      } 
      child { node  (n01)  {  1  } 
        edge from parent node[right] {1}
      }
      edge from parent node[above left, inner sep=1pt,red] {0}% <-
    } 
    child { node  (n0)  {  1  } 
      child { node  (n00)  {  0  } 
        edge from parent node[left] {0}
      } 
      child { node  (n01)  {  1  } 
        edge from parent node[right] {1}
      }
      edge from parent node[above right, inner sep=1pt,red] {1}% <-
    } 
  ;
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .