如何使树中的节点看起来分布更均匀并平衡边长?

如何使树中的节点看起来分布更均匀并平衡边长?

我如何让树中的边具有相同的长度并具有良好的对称性?它看起来有点丑。

它需要看起来更加对称。有没有简单的方法可以实现这个目标?

\begin{figure}[h]
    \begin{tikzpicture}[
    > = stealth, 
    shorten > = 1pt,
    auto,
    node distance = 2cm, 
    semithick % line style
    ]

    \tikzstyle{state}=[
    draw = black,
    thick,
    fill = white,
    minimum size = 4mm
    ]

    \node[state][circle, draw](hub) {$hub$};        
    \node[state][circle, draw] (a) [below left of=hub] {$a$};
    \node[state][circle, draw] (b) [below right of=hub] {$b$};

    \node[state][circle, draw] (c) [below left of=a] {$c$};        
    \node[state][circle, draw] (d) [right of=c] {$d$};

    \node[state][circle, draw] (e) [below left of=c] {$e$};        
    \node[state][circle, draw] (f) [right of=e] {$f$};

    \node[state][circle, draw] (g) [right of=d] {$g$};        
    \node[state][circle, draw] (h) [right of=g] {$h$};


    \path[->] (a) edge node {100} (hub);
    \path[->] (b) edge node {85} (hub);       

    \path[->] (c) edge node {35} (a);
    \path[->] (d) edge node {55} (a);       
    \path[->] (e) edge node {10} (c);
    \path[->] (f) edge node {15} (c);   
    \path[->] (g) edge node {40} (b);
    \path[->] (h) edge node {25} (b);           


    \node[state][circle, draw] (u) [above left of=hub] {$u$}; 
    \node[state][circle, draw] (v) [above right of=hub] {$v$};

    \node[state][circle, draw] (w) [above left of=u] {$w$}; 
    \node[state][circle, draw] (x) [right of=w] {$x$}; 
    \node[state][circle, draw] (y) [right of=x] {$y$};
    \node[state][circle, draw] (z) [right of=y] {$z$};


    \path[->] (hub) edge node {125} (u); 
    \path[->] (hub) edge node {25} (v); 
    \path[->] (u) edge node {50} (w);
    \path[->] (u) edge node {60} (x);
    \path[->] (v) edge node {10} (y);
    \path[->] (v) edge node {12} (z);



    \draw (0,-6) node[above,xshift=0.7cm]{$ \textsc{Pos and Neg heap represented together as a single structure}$};
    \end{tikzpicture}
\end{figure}

答案1

我可以为此宣传森林包吗?您的对称性问题只需一个键即可解决:calign=fixed edge angles。而且,输入树并对其进行自定义要容易得多。

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
for tree={calign=fixed edge angles,
edge+={stealth-},grow=south,circle,draw,thick}
[$hub$
 [$a$,edge label={node[midway,left]{100}}
  [$c$,edge label={node[midway,left]{35}}
   [$e$,edge label={node[midway,left]{10}}]
   [$f$,edge label={node[midway,right]{15}}]
   ]
  [$d$,edge label={node[midway,right]{55}}]
  ]
 [$b$,edge label={node[midway,right]{85}}
  [$g$,edge label={node[midway,left]{40}}]
  [$h$,edge label={node[midway,right]{25}}]
 ]
]
\end{forest}
\end{document}

在此处输入图片描述

答案2

借助 tikz 库arrows.metapositioning并且quotes

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta, 
                positioning, 
                quotes}

\begin{document}
\begin{tikzpicture}[
                  node distance = 6mm and 4mm,
       every edge/.append style = {draw, semithick, shorten >=1pt, -Stealth},
every edge quotes/.append style = {font=\footnotesize, auto, inner sep=1pt},
            state/.append style = {circle, draw, minimum size = 5mm, 
                                   inner sep=1pt, outer sep=0pt} 
                    ]
%%%% below
\node[state] (hub) {\textit{hub}};
\node[state] (a) [below left =12mm and 8mm of hub]  {$a$};
\node[state] (c) [below left = of a]    {$c$};
\node[state] (e) [below left = of c]    {$e$};
    \node[state] (d) [below right = of a] {$d$};
    \node[state] (f) [below right = of c] {$f$};
%
\node[state] (b) [below right =12mm and 8mm of hub]     {$b$};
\node[state] (h) [below right = of b]       {$h$};
    \node[state] (g) [below left = of b]    {$g$};
%%%% above
\node[state] (u) [above left =12mm and 8mm of hub]  {$u$};
\node[state] (w) [above left = of u]    {$w$};
    \node[state] (x) [above right = of u]   {$x$};
%
\node[state] (v) [above right =12mm and 8mm of hub] {$v$};
\node[state] (z) [above right = of v]   {$z$};
    \node[state] (y) [above left = of v]    {$y$};
%%%% edges
\path   (a) edge  ["100"] (hub)
    (b) edge  ["85"] (hub)

    (c) edge  ["35"]    (a)
    (d) edge  ["55" ']  (a)
    (e) edge  ["10"]    (c)
    (f) edge  ["15" ']  (c)
    (g) edge  ["40"]    (b)
    (h) edge  ["25" ,]  (b)

    (hub) edge  ["125"] (u)
    (hub) edge  ["25" '](v)
    (u) edge  ["50"]    (w)
    (u) edge  ["60" ']  (x)
    (v) edge  ["10"]    (y)
    (v) edge  ["12" ']  (z);
\node[below=of hub |- e]
    {\textsc{Pos and Neg heap represented together as a single structure}};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容