4

I am trying to recreate the image shown below.

enter image description here

While the numbers are blurry that is not relevant. The relevant part is making the tree look like a triangle.

Here is how far I have gotten

enter image description here

What is the simplest way to change the way the nodes are placed?

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{trees}
\usetikzlibrary{positioning}
\begin{document}

    \begin{tikzpicture}[thin,
      level 1/.style={sibling distance=40mm},
      level 2/.style={sibling distance=25mm},
      level 3/.style={sibling distance=15mm},
      level 4/.style={sibling distance=15mm},
      level 5/.style={sibling distance=20mm},
      every circle node/.style={minimum size=1.5mm,inner sep=0mm, fill, circle, above}]

      \node[circle,draw,fill,label=above:$1$] (root) {}
      child { node [circle,fill,label=left:$2$] {}
        child { node[circle, fill, label=left:$5$] {}
          child { node [circle, fill, label=left:$9$] {}
            child {node [circle, fill, label=below:$14$] {}
            }
            child {node [circle, fill, label=below:$15$] {}
            }
          }
          child { node[circle, fill, label=right:$10$] {}
            child { node[circle, fill, label = below:$16$] {}}
            child { node[circle, fill, label = below:$17$] {}}
          }
          % edge from parent
          % node[left] {$Z$}
        }
      }
      child { node [circle,fill,label=left:$3$] {}
        child { node [circle,fill,label=left:$6$] {}
        }
      }
      child { node [circle,fill,label=right:$4$] {}
        child { node [circle,fill,label=above:$7$] {}}
        child {  node [circle,fill,label=right:$8$] {}
          child {  node [circle,fill,label=below:$11$] {}
          }
          child {  node [circle,fill,label=below:$12$] {}
          }
          child {  node [circle,fill,label=below:$13$] {}
          }
        }
      };
    \end{tikzpicture}
\end{document}
  • If you do it with Forest, you can specify the way the nodes are placed appropriately. But you'd have to use Forest to do that. [On the other hand, of course, you should use Forest anyway :).] – cfr Apr 12 '18 at 2:35
  • Is node 15 supposed to disappear? – cfr Apr 12 '18 at 2:36
  • @cfr, no the 15 should not dissapear. I am trying to mimick the tree from the image =) – N3buchadnezzar Apr 12 '18 at 9:18
5

Assuming 15 is not meant to disappear (as it doesn't in the target image, as far as I can tell with the blur), you could do something like this in Forest.

\documentclass[border=10pt]{standalone}
\usepackage{forest}
\forestset{%
  tri tree/.style={
    for tree={
      circle, fill, inner sep=1.5pt, 
      parent anchor=center, 
      child anchor=center,
      calign=fixed edge angles,
      fit=rectangle,
    },
    before typesetting nodes={
      tempcounta'=0,
      for tree breadth-first={
        tempcounta'+=1,
        if level=0{label/.register=tempcounta,}{
          if n children=0{temptoksa=below}{
            if={>Ow+P O Ow+n = & {!u.n children}{isodd(##1)} {n} {!u.n children}{(##1+1)/2} }{temptoksa=right}{
              if={> OOw+n< {n}{!u.n children}{(##1+1)/2}}{temptoksa=left}{temptoksa=right},
            },
          },
          label/.process={RRw2{tempcounta}{temptoksa}{##2:##1} }
        },
      },
    },
    before drawing tree={
      where={>Ow+P{n children}{isodd(##1)}}{
        for nodewalk/.process={
          Ow+n w {n children} {(##1+1)/2} {n=##1}{x/.option=!u.x}
        },
      }{},
    },
  },
}
\begin{document}
\begin{forest}
  tri tree
  [ [[[[][]][[][]]]] [[]] [[][[][][]]] ]
\end{forest}
\end{document}

You probably want to tweak l sep, s sep and/or the angles, but the result shows the basic idea applied to your tree:

<code>tri tree</code> with Forest

EDIT

If you want the sides of the tree to always slope, then you might try something like this (probably needs tweaking):

% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode
% arara: pdflatex
\pdfminorversion=7
% ateb: https://tex.stackexchange.com/a/426101/
\documentclass[border=10pt]{standalone}
\usepackage{forest}
\forestset{%
  tri tree 2/.style={
    for tree={
      circle, fill, inner sep=1.5pt, 
      parent anchor=center, 
      child anchor=center,
      calign=fixed edge angles,
      fit=rectangle,
    },
    before typesetting nodes={
      tempcounta'=0,
      for tree breadth-first={
        tempcounta'+=1,
        if level=0{label/.register=tempcounta,}{
          if n children=0{temptoksa=below}{
            if={>Ow+P O Ow+n = & {!u.n children}{isodd(##1)} {n} {!u.n children}{(##1+1)/2} }{temptoksa=right}{
              if={> OOw+n< {n}{!u.n children}{(##1+1)/2}}{temptoksa=left}{temptoksa=right},
            },
          },
          label/.process={RRw2{tempcounta}{temptoksa}{##2:##1} }
        },
      },
   },
    before packing={
      for nodewalk={
        r,
        while nodewalk valid={1}{1}
      }{
        if n children=1{append={[, phantom, typeset node]}}{}
      },
      for nodewalk={
        r,
        while nodewalk valid={l}{l}
      }{
        if n children=1{prepend={[, phantom, typeset node]}}{}
      },
    },
    before drawing tree={
      where={>Ow+P{n children}{isodd(##1)}}{
        for nodewalk/.process={
          Ow+n w {n children} {(##1+1)/2} {n=##1}{
            x/.option=!u.x,
          }%
        },
      }{},
    },
  },
}
\begin{document}
\begin{forest}
  tri tree 2
  [ [[[[][]][[][]]]] [[]] [[][[][][]]] ]
\end{forest}
\end{document}

slopier style

  • This looks great, but why does the node from 2 to 5 go straight down, instead of down left? I think it would be better if the outer shape of the tree looked like ∧, like in the image. Thanks a lot for your help so far! – N3buchadnezzar Apr 12 '18 at 9:17
  • @N3buchadnezzar You need to add a phantom node. Please see edit above. – cfr Apr 14 '18 at 2:59

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.