我一直使用 tikzpicture 以这种方式绘制树木:
\documentclass[12pt, letterpaper, oneside]{article}
\usepackage{tikz}
\begin{document}
\[
\begin{tikzpicture}[nodes={draw, circle, fill=gray!20}, --]
\node{19}
child {node{25}
child[missing]
child{node{28}
child {node{32}}
child {node{93}}
}
}
child [missing]
child[missing]
child {node{20}
child{node{89}
child{node{99}}
child[missing]
}
child[missing]
};
\end{tikzpicture}
\]
\end{document}
答案1
这显示了如何将整个 tikzpicture 放入节点。不确定如何将该节点放入树中。
\documentclass[12pt, letterpaper, oneside]{article}
\usepackage{tikz}
\begin{document}
\begin{center}
\sbox0{\begin{tikzpicture}[nodes={draw, circle, fill=gray!20}]
\node{19}
child {node{25}
child[missing]
child{node{28}
child {node{32}}
child {node{93}}
}
}
child [missing]
child[missing]
child {node{20}
child{node{89}
child{node{99}}
child[missing]
}
child[missing]
};
\end{tikzpicture}}
\begin{tikzpicture}
\node[draw, rectangle, rounded corners] {\usebox0};
\end{tikzpicture}
\end{center}
\end{document}
答案2
在朋友的帮助下,我设法为这棵树找到了一个不太难的解决方案,我希望它能对某些人有所帮助:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}
\flushleft
\begin{tikzpicture}
[square/.style={regular polygon, regular polygon sides=4}]
\node at (0,0) [circle,draw] (a) {4};
\node at (5,-3) [rounded corners, draw] (c) {
\begin{minipage}{0.4\textwidth}
\begin{tikzpicture}
\node at (0,0) [circle,draw] (a1) {8};
\node at (1,-1) [circle,draw] (a2) {16};
\node at (-1,-1) [circle,draw] (a3) {50};
\node at (-2,-2) [circle,draw] (a4) {55};
\draw (a1) -- (a2);
\draw (a1) -> (a3);
\draw (a3) -> (a4);
\end{tikzpicture}
\end{minipage}
\begin{minipage}{0.4\textwidth}
\begin{tikzpicture}
\node at (0,0) [circle,draw] (a1) {19};
\node at (2,-1) [circle,draw] (a2) {20};
\node at (-2,-1) [circle,draw] (a3) {25};
\node at (-1,-2) [circle,draw] (a4) {28};
\node at (-1.5,-3) [circle,draw] (a5) {32};
\node at (-0.5,-3) [circle,draw] (a6) {93};
\node at (1,-2) [circle,draw] (a7) {89};
\node at (0.5,-3) [circle,draw] (a8) {99};
\draw (a1) -> (a2);
\draw (a1) -> (a3);
\draw (a3) -> (a4);
\draw (a4) -> (a5);
\draw (a4) -> (a6);
\draw (a2) -> (a7);
\draw (a7) -> (a8);
\end{tikzpicture}
\end{minipage}
};
\node at (-3,-1) [circle,draw] (b) {9};
\node at (-4,-2) [circle,draw] (b1) {17};
\node at (-5,-3) [circle,draw] (b2) {19};
\node at (-2,-2) [circle,draw] (b3) {26};
\draw (a) -- (c);
\draw (a) -- (b);
\draw (b) -- (b1);
\draw (b1) -- (b2);
\draw (b) -- (b3);
\end{tikzpicture}
\end{document}