如何在树形图中居中放置一个值

如何在树形图中居中放置一个值

我正在尝试将一个值置于概率图树的中心,但做不到。有人能帮帮我吗?这是我正在运行的代码,这是输出。我试图将值 56.444 置于中心,但没有结果。

输出

\begin{tikzpicture}[grow=right, sloped]
\node[bag] {40}

child {
    node[bag] {33.484}        
        child {
            node[end, label=right:
                {28.029}] {}
            edge from parent
            node[above] {}
            node[below]  {}
        }
        child {
            node[end, label=right:
                {39.775}] {}
            edge from parent
            node[above] {}
            node[below]  {}
        }
        edge from parent 
        node[above] {}
        node[below]  {}
}
child {
    node[bag] {47.516}        
    child {
            node[end, label=right:
                {39.775}] {}                
        }
        child {
            node[bag, label=-45:
                {56.444}] {}
             child {
            node[end, label=right:
                {56.444}] {}
            edge from parent
            node[above] {}
            node[below]  {}
        }     child {
            node[end, label=right:
                {56.444}] {}
            edge from parent
            node[above] {}
            node[below]  {}
        }   } 
            };
\end{tikzpicture}

答案1

另外,您可以考虑使用forest,这可以使代码更简单:

在此处输入图片描述

\documentclass{article}

\usepackage{forest}
\usetikzlibrary{arrows.meta}

\begin{document}

\begin{forest}
for tree={grow'=east,
    if n children=0{edge=-{Circle[length=2pt]}, child anchor=west}{},
    l sep=1.2cm, s sep=5mm
}
[40
  [47.516
    [56.444
      [56.444][56.444]
    ]
    [39.775]
  ]
  [33.484
    [39.775][28.029]
  ]
]
\end{forest}

\end{document}

相关内容