如何画一棵树
使用 LaTeX 以及如何为顶点和边添加标签?
答案1
\documentclass[pstricks]{standalone}
\usepackage{pst-tree}
\begin{document}
\small\psset{radius=3pt,treesep=6mm}
\begin{pspicture}(-2,-2.75)(13,3)
\psline(12,0)
\psforeach{\iA}{0,1,3,4,5,6,7,9,10,11,12}{\uput[90](\iA,0){\iA}}
\psforeach{\iA}{2,8}{\uput[-90](\iA,0){\iA}}
\rput[t](0,3pt){%
\pstree[radius=3pt,tnpos=b,tnsep=0pt]{\Tc*{3pt}}{%
\TC*~{30}~[tnpos=a,tnsep=-15pt]{15}
\TC*~{42}~[tnpos=a,tnsep=-15pt]{21}
\TC*~{56}~[tnpos=a,tnsep=-15pt]{28}
\TC*~{72}~[tnpos=a,tnsep=-15pt]{36}}}
\rput[b](2,-3pt){%
\pstree[treemode=U,tnpos=a,tnsep=0pt]{\Tc*{3pt}}{%
\TC*~{88}~[tnpos=b,tnsep=-15pt]{45}
\TC*~{108}~[tnpos=b,tnsep=-15pt]{55}
\TC*~{130}~[tnpos=b,tnsep=-15pt]{66}
\TC*~{154}~[tnpos=b,tnsep=-15pt]{78}}}
\rput[t](4,3pt){%
\pstree[radius=3pt,tnpos=b,tnsep=0pt]{\Tc*{3pt}}{%
\TC*~{178}~[tnpos=a,tnsep=-15pt]{91}
\TC*~{206}~[tnpos=a,tnsep=-15pt]{105}
\TC*~{236}~[tnpos=a,tnsep=-15pt]{120}
\TC*~{268}~[tnpos=a,tnsep=-15pt]{136}}}
% ...
\end{pspicture}
\end{document}
答案2
使用 TikZ,为了在一条路径中完成它的乐趣(仅仅是一个开始):
\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{trees}
\tikzset{level 1/.style={
level distance=2cm, sibling distance=1cm
},
point/.style={circle,
fill,
inner sep=1.4pt
},
every label/.style={font=\scriptsize}
}
\begin{document}
\begin{tikzpicture}[]
\draw (0,0)
node[point,label={north:0}]{}
child{edge from parent node[label={west:15}]{}; \node[point,label={south:30}]{}}
child{edge from parent node[label={west:21}]{}; \node[point,label={south:42}]{}}
child{edge from parent node[label={east:28}]{}; \node[point,label={south:56}]{}}
child{edge from parent node[label={east:36}]{}; \node[point,label={south:72}]{}}
--node[above,midway,label={above:1}]{}(3,0)
node[point,label={south:2}] {}[grow'=up]
child{edge from parent node[label={west:45}]{}; \node[point,label={north:88}]{}}
child{edge from parent node[label={west:55}]{}; \node[point,label={north:108}]{}}
child{edge from parent node[label={east:66}]{}; \node[point,label={north:130}]{}}
child{edge from parent node[label={east:78}]{}; \node[point,label={north:154}]{}}
--node[above,midway,label={above:3}]{}(6,0)
node[point,label={north:4}] {} [grow'=down]
child{edge from parent node[label={west:..}]{}; \node[point,label={south:..}]{}}
child{edge from parent node[label={west:..}]{}; \node[point,label={south:..}]{}}
child{edge from parent node[label={east:..}]{}; \node[point,label={south:..}]{}}
child{edge from parent node[label={east:..}]{}; \node[point,label={south:..}]{}}
;
\end{tikzpicture}
\end{document}
结果: