8

I'm trying to draw a tree, but I can't get it to skip one level. This is my code.

\documentclass[border=1cm]{standalone}
\usepackage{tikz-qtree}
\begin{document}
\begin{tikzpicture}
    [every tree node/.style={draw,circle},
   level distance=1.25cm,sibling distance=1cm, 
   edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
\Tree [.\node {}; 
    \edge node[auto=right] {1};
    [.\node{};
      \edge node[auto=right] {1};  
      [.\node{};
      \edge node[auto=right] {1};
      [.\node{apple};]
      \edge node[auto=left] {0};
      [.\node {pear}; ]
      ]
      \edge node[auto=left] {0};  
      [.\node{};
      \edge node[auto=right] {1};
      [.\node{bike};]
      \edge node[auto=left] {0};
      [.\node {car}; ]
      ]
    ]
    \edge node[auto=left] {I WANT THIS EDGE TO GO STRAIGHT TO};  
      [
       \edge node[auto=right] {};  
      [.\node{HERE};
      \edge node[auto=right] {1};
      [.\node{walk};]
      \edge node[auto=left] {0};
      [.\node {run}; ]  ]   ]
    ]
\end{tikzpicture}
\end{document}

Here is the image the code produces.

enter image description here

  • 3
    Welcome to TeX.sx! Feel free to consult TeX.SX starter guide to get additional insights into how to get the most out of this site. – Peter Jansson May 9 '13 at 10:43
  • Welcome Johan. I modified your code to add \documentclass etc, so that people can more easily reproduce your problem. – John Wickerson May 9 '13 at 10:44
  • @JohanSjons I have added the image for you. I hope you don't mind. :) Welcome to the site! – hpesoj626 May 9 '13 at 11:47
  • For other options for this kind of tree see What is a more efficient way to draw this tree?. – Alan Munn May 9 '13 at 16:04
5

You can:

  • remove the edges (using the option draw=none),
  • label the two nodes (I called them foo and qux) between which you want your edge, and
  • draw in the edge manually.

Result:

enter image description here

Code:

\documentclass[border=1cm]{standalone}
\usepackage{tikz-qtree}
\begin{document}
\begin{tikzpicture}
    [every tree node/.style={draw,circle},
   level distance=1.25cm,sibling distance=1cm, 
   edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
\Tree [.\node (foo) {}; 
    \edge node[auto=right] {1};
    [.\node{};
      \edge node[auto=right] {1};  
      [.\node{};
      \edge node[auto=right] {1};
      [.\node{apple};]
      \edge node[auto=left] {0};
      [.\node {pear}; ]
      ]
      \edge node[auto=left] {0};  
      [.\node{};
      \edge node[auto=right] {1};
      [.\node{bike};]
      \edge node[auto=left] {0};
      [.\node {car}; ]
      ]
    ]
    \edge[draw=none] node[auto=left] {};  
      [
       \edge[draw=none] node[auto=right] {};  
      [.\node (qux) {};
      \edge node[auto=right] {1};
      [.\node{walk};]
      \edge node[auto=left] {0};
      [.\node {run}; ]  ]   ]
    ];
\draw (qux) -- (foo) node[midway,auto=right] {0};
\end{tikzpicture}
\end{document}
|improve this answer|||||
3

There's no way to draw the branch exactly as you want; tikz-qtree is not designed to draw trees like that. But you can achieve a similar result by using the frontier/.style which allows you to set the terminal nodes a fixed distance from the root. In order to do this, you need to remove the [ ] from the terminal nodes (so that tikz-qtree knows that they are terminal nodes.) I've also added some code to make the circled nodes with text uniform size.

\documentclass{article}
\usepackage{tikz-qtree}
\begin{document}
\begin{tikzpicture}
    [every internal node/.style={draw,circle,inner sep=1.5pt},every leaf node/.style={draw, circle, minimum size=1.cm,inner sep=0pt},
   level distance=1.25cm,sibling distance=1cm,frontier/.style={distance from root=3.5cm},
   edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
\Tree [.\node{}; 
    \edge node[auto=right] {1};
    [.\node{};
      \edge node[auto=right] {1};  
      [.\node{};
      \edge node[auto=right] {1};
      \node{apple};
      \edge node[auto=left] {0};
      \node {pear}; 
      ]
      \edge node[auto=left] {0};  
      [.\node{};
      \edge node[auto=right] {1};
      \node{bike};
      \edge node[auto=left] {0};
      \node {car};
      ]
    ]
    \edge node[auto=left] {0};    
      [.\node{};
      \edge node[auto=right] {1};
      \node{walk};
      \edge node[auto=left] {0};
      \node {run};  ]   ]
    ]
\end{tikzpicture}
\end{document}

output of code

|improve this answer|||||
  • Wow, thank you so much! This is even better than what I had in mind. – Johan Sjons May 9 '13 at 12:20
2

With forest, you can simply use tier=<tier name> to align the nodes you want.

For example:

\documentclass[tikz,border=5pt]{standalone}
\usepackage{forest}

\begin{document}

\begin{forest}
  for tree={
    circle,
    draw,
    inner sep=1pt,
    minimum size=5pt,
    if n=1{
      edge label={node [above left, midway] {1}}
    }{
      if n'=1{
        edge label={node [above right, midway] {0}}
      }{}
    }
  }
  [
    [
      [, tier=align me
        [apple]
        [pear]
      ]
      [
        [bike]
        [car]
      ]
    ]
    [, tier=align me
      [walk]
      [run]
    ]
  ]
\end{forest}

\end{document}

aligned tier

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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