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

Alan Munn
  • 218,180

3 Answers3

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

Alan Munn
  • 218,180
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

cfr
  • 198,882