子圖中的 Tikz 樹間距

子圖中的 Tikz 樹間距

我一直在嘗試以如下所示的子圖格式建立決策樹,但無論我嘗試哪種間距,在某個點或另一個點總是存在重疊的節點。在下面的範例中,我有同一棵樹的兩個副本作為範例。如何使用 tikz 套件修復此問題?另外,對於使用 tikz 套件繪製稍大的樹,我有什麼建議嗎?這非常令人沮喪,因為我覺得我正處於隨機改變關卡/兄弟距離的階段,以便嘗試讓事情發揮作用...

\documentclass{book}
\usepackage{tikz}
\usepackage{amsmath,amssymb,amstext,amsthm}
\usepackage{subcaption}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}[h]{0.475\textwidth}
\centering
\begin{tikzpicture}[scale=0.6, level distance=25mm,
    level 1/.style={sibling distance=80mm},
    level 2/.style={sibling distance=50mm},
    level 3/.style={sibling distance=30mm}]
\node[circle, draw]{$x\smash{_1}$}
    child{node[circle, draw]{$x\smash{_2}$}
        child{node[circle, draw]{$x\smash{_4}$}
            child{node[rectangle, draw]{$x\smash{_1} \gets 1$}}
            child{node[rectangle, draw]{$x\smash{_2} \gets 1$}} 
        }
        child{node[circle, draw]{$x\smash{_4}$}
            child{node[rectangle, draw]{$x\smash{_1} \gets 1$}}
            child{node[rectangle, draw]{$x\smash{_4} \gets 0$}} 
        }   
    }
    child{node[circle, draw]{$x\smash{_3}$}
        child{node[circle, draw]{$x\smash{_2}$}
            child{node[rectangle, draw]{$\begin{aligned} x\smash{_1} &\gets 0 \\ x\smash{_3} &\gets 1 \end{aligned}$}}
            child{node[rectangle, draw]{$\begin{aligned} x\smash{_1} &\gets 0 \\ x\smash{_2} &\gets 0 \end{aligned}$}}  
        }
        child{node[circle, draw]{$x\smash{_4}$}
            child{node[rectangle, draw]{$x\smash{_4} \gets 1$}}
            child{node[rectangle, draw]{$x\smash{_3} \gets 0$}} 
        }   
    }
;
\end{tikzpicture}
\caption{A Tree}
\end{subfigure}
\begin{subfigure}[h]{0.475\textwidth}
\centering
\begin{tikzpicture}[scale=0.6, level distance=25mm,
    level 1/.style={sibling distance=80mm},
    level 2/.style={sibling distance=50mm},
    level 3/.style={sibling distance=30mm}]
\node[circle, draw]{$x\smash{_1}$}
    child{node[circle, draw]{$x\smash{_2}$}
        child{node[circle, draw]{$x\smash{_4}$}
            child{node[rectangle, draw]{$x\smash{_1} \gets 1$}}
            child{node[rectangle, draw]{$x\smash{_2} \gets 1$}} 
        }
        child{node[circle, draw]{$x\smash{_4}$}
            child{node[rectangle, draw]{$x\smash{_1} \gets 1$}}
            child{node[rectangle, draw]{$x\smash{_4} \gets 0$}} 
        }   
    }
    child{node[circle, draw]{$x\smash{_3}$}
        child{node[circle, draw]{$x\smash{_2}$}
            child{node[rectangle, draw]{$\begin{aligned} x\smash{_1} &\gets 0 \\ x\smash{_3} &\gets 1 \end{aligned}$}}
            child{node[rectangle, draw]{$\begin{aligned} x\smash{_1} &\gets 0 \\ x\smash{_2} &\gets 0 \end{aligned}$}}  
        }
        child{node[circle, draw]{$x\smash{_4}$}
            child{node[rectangle, draw]{$x\smash{_4} \gets 1$}}
            child{node[rectangle, draw]{$x\smash{_3} \gets 0$}} 
        }   
    }
;
\end{tikzpicture}
\caption{B Tree}
\end{subfigure}
\caption{Caption}
\label{datftt}
\end{figure}
\end{document}

答案1

最終,葉(末端)節點內的文字產生的樹的最小寬度。對於這兩棵樹,您可以看到放置所有端到端x_n <- 1結果已經佔用了超過一半的文字寬度。如果您想強制這些樹並排,您將擁有重疊的樹(就像您目前所做的那樣)或重疊的節點。這兩個選項都不優雅......

可以透過為較低層級設定較小的字體大小來稍微調整此最小寬度,以便它們並排放置時佔用更少的空間。這可以透過 來實現level n/.style={font=\footnotesize}

您還提到您必須不斷嘗試和錯誤來調整兄弟距離。原因是Ti中的標準樹建構演算法kZ缺乏遠見。它看到下一層有n節點並根據兄弟距離放置它們,但它不知道這些節點下面是否有更多節點,從而使節點的有效「寬度」更大。

這可以透過使用來解決graph drawing這可以透過使用Ti 的函式庫來kZ.它提供了更多的演算法來自動放置節點,這使得繪圖圖表 簡單得多。我提供了一個範例,展示如何使用圖形繪製庫:

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{
  graphs,
  graphdrawing,
}
\usegdlibrary{trees}
\usepackage{amsmath,amssymb,amstext,amsthm}
\usepackage{subcaption}
\begin{document}
\begin{figure}
  \centering
  \begin{subfigure}[b]{\linewidth}
    \centering
    \tikz \graph [
      tree layout,
      nodes={
        draw,
        circle,
      },
      level 3/.style={
        font=\small,
      },
      level 4/.style={
        nodes={
          rectangle,
          font=\footnotesize,
        }
      }
    ] {
      "\(x_{}\)"
        -> {
          "\(x_{1}\)"
          -> {
            "\(x_{11}\)"
            -> {
              "\(x_{111}\)",
              "\(x_{112}\)"
            },
            "\(x_{12}\)"
            -> {
              "\(x_{121}\)",
              "\(x_{122}\)"
            }
          },
          "\(x_{2}\)"
          -> {
            "\(x_{21}\)"
            -> {
              "\(x_{211}\)",
              "\(x_{212}\)"
            },
            "\(x_{22}\)"
            -> {
              "\(x_{221}\)",
              "\(x_{222}\)"
            }
          }
        }
    };
    \caption{Tree \(x\)}
  \end{subfigure}

  \begin{subfigure}[b]{\linewidth}
    \centering
    \tikz \graph [
      tree layout,
      nodes={
        draw,
        circle,
      },
      level 3/.style={
        font=\small,
      },
      level 4/.style={
        nodes={
          rectangle,
          font=\footnotesize,
        }
      }
    ] {
      "\(y_{}\)"
        -> {
          "\(y_{1}\)"
          -> {
            "\(y_{11}\)"
            -> {
              "\(y_{111}\)",
              "\(y_{112}\)"
            },
            "\(y_{12}\)"
            -> {
              "\(y_{121}\)",
              "\(y_{122}\)"
            }
          },
          "\(y_{2}\)"
          -> {
            "\(y_{21}\)"
            -> {
              "\(y_{211}\)",
              "\(y_{212}\)"
            },
            "\(y_{22}\)"
            -> {
              "\(y_{221}\)",
              "\(y_{222}\)"
            }
          }
        }
    };
    \caption{Tree \(y\)}
  \end{subfigure}
  \caption{My awesome captions for trees!}
  \label{fig:trees}
\end{figure}
\end{document}

和輸出:

輸出

相關內容