编辑

编辑

我正在尝试重新创建下面显示的图像。

在此处输入图片描述

虽然数字很模糊,但这并不重要。重要的部分是让树看起来像一个三角形。

以下是我取得的进展

在此处输入图片描述

改变节点放置方式的最简单的方法是什么?

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

答案1

假设 15 不会消失(因为据我所知,它不会在目标图像中消失),您可以在 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}

您可能想要调整l seps sep/或角度,但结果显示了应用于您的树的基本思想:

<code>三叉树</code> 和 Forest

编辑

如果您希望树的侧面始终倾斜,那么您可以尝试这样的方法(可能需要进行调整):

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

更随意的风格

相关内容