答案1
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[leaf/.style={draw=white, thick, fill=black, circle}, inner/.style={draw=white, thick, fill=gray, circle}]
\node[leaf,label={180:\textbf{\sffamily Chimp}}] (C) at (0,0) {};
\node[leaf,label={180:\textbf{\sffamily Human}}] (H) at (0,-1.5) {};
\node[leaf,label={0:\textbf{\sffamily Seal}},anchor=west] (S) at (4,0) {};
\node[leaf,label={0:\textbf{\sffamily Whale}},anchor=west] (W) at (4,-1.5) {};
\node[inner] (il) at (1.3,-.75) {};
\node[inner] (ir) at (2.8,-.75) {};
\foreach \from/\to/\opt/\lab in
{C/il/above/1,
H/il/below/2,
il/ir/above/3,
ir/S/above/2,
ir/W/below/0
}{
\path[thick,black] (\from) edge [\opt] node {\lab} (\to);
}
\end{tikzpicture}
\end{document}
生产
答案2
使用包绘制树forest
:
\documentclass[margin=3.14159mm]{standalone}
\usepackage{forest}
\tikzset{ELS/.style = {% Edge Label Style
pos=0.5, sloped, node font=\footnotesize, text=blue,
inner sep=2pt},
}
\begin{document}
\begin{forest}
for tree = {
% node style
circle,
draw=gray!50, very thick,
fill=black,
minimum size=4mm,
tikz/every label/.style = {font=\sffamily\bfseries},
% tree style
edge = {semithick},
l sep=6mm,
s sep=7mm,
EL/.style={if n=1{edge label={node[ELS, anchor=south]{$#1$}}}
{edge label={node[ELS, anchor=north]{$#1$}}}}
}
% tree body code
[,coordinate, label={[ELS]3}
[ , fill=gray,
%%%% settings for the right branch
for tree={grow'=east},
before computing xy={l=0, s=+7mm},
%%%%
[,label=right:Seal, EL=2]
[,label=right:Whale, EL=0]
]
[ , fill=gray,
%%%% settings for the left branch
for tree={grow=west},
before computing xy={l=0, s=-7mm},
%%%%
[,label=left:Chimp, EL=1]
[,label=left:Human, EL=2]
]
]
\end{forest}
\end{document}