3

I'd like to align all leaves of a tree at the bottom, so what I'd like to achieve should look like:

    p
   / \
  /  /\
 c  d  e

What I've tried so far (using tikz-qtree) results in:

    p
   / \
  c  /\
    d  e

I'm currently using tikz-qtree, but if there's an alternative better suited to the task, feel free to suggest one ;) Sidenote: I'm actually not labeling nodes, but edges.

Code:

\documentclass[12pt]{article}
\usepackage[active,pdftex,tightpage]{preview}
\usepackage{tikz-qtree}
\PreviewEnvironment[]{tikzpicture}
\begin{document}
\begin{tikzpicture}[every tree node/.style={draw,circle},sibling distance=10pt,
edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
\tikzstyle{every node}=[font=\tiny, sloped,anchor=south,auto=false,inner sep=1pt]
\Tree
[.{}
    \edge node[]{C};
    [.{}
    ]
    \edge node[]{A};
    [.{}
        \edge node[]{D};
        [.{}
        ]
        \edge node[]{E};
        [.{}
        ]
    ]
]
\end{tikzpicture}
\end{document}
1

Here's a forest solution which allows the tree to be specified quite concisely. Once the preamble of the tree is configured (which could also be saved as a style, for example), we can write

  [
    [c]
    [a
      [d]
      [e]
    ]
  ]

to produce

auto-labelling

The alignment of the final nodes in the tree is accomplished with

    if n children=0{tier=terminal}{},

Complete code:

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  for tree={
    draw,
    circle,
    delay={
      if level=0{}{
          edge label/.wrap pgfmath arg={node [font=\tiny, sloped, midway, anchor=south, auto=false, inner sep=1pt] {#1} }{content()}
      },
    },
    if n children=0{tier=terminal}{},
    s sep=10pt,
  },
  before typesetting nodes={
    for tree={
      content=,
    }
  }
  [
    [c]
    [a
      [d]
      [e]
    ]
  ]
\end{forest}
\end{document}
4

This code generates what you need:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}
    \begin{figure}
        \begin{center}
        \tikzstyle{level 1}=[level distance=1.5cm, sibling distance=2.5cm]
        \tikzstyle{level 2}=[level distance=1.5cm, sibling distance=2.5cm]
        \tikzstyle{level 3}=[level distance=1.5cm, sibling distance=1cm]
        \tikzstyle{level 4}=[level distance=1.5cm, sibling distance=2cm]
            \begin{tikzpicture}
            \node {p}
                child{
                    child{
                        node(a){c}
                    } child{edge from parent[draw=none] }
                }
                child{
                    node{}
                    child{
                        node(b){d}
                    }
                    child{
                        node(c){e}
                    }
                };
            \end{tikzpicture}
        \end{center}
    \end{figure}
\end{document}

enter image description here

Credits to this page.

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.