TikZ - 哈夫曼树

TikZ - 哈夫曼树

我用的是简单哈夫曼.lua文件,将其放在与我的文件相同的文件夹中tex,然后使用以下代码编译以下代码(pgfmanual 中的“35.8.3 如何在算法内部生成节点”)LuaTeX

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
%\usepackage[lf]{MinionPro}
\usepackage{tikz}
\usepackage[left=0mm,right=0mm,top=0mm,bottom=0mm]{geometry}
\usetikzlibrary{graphs,graphdrawing}

\pgfkeys{
/graph drawing/HuffmanLabel/.style={
/tikz/edge node={node[fill=gray!10,rectangle,font=\footnotesize,inner sep=1pt]{#1}}
},
/graph drawing/HuffmanNode/.style={
/tikz/.cd,circle,inner sep=0pt,outer sep=0pt,fill=black,minimum size=3pt
}
}

\usegdlibrary{SimpleHuffman}

\begin{document}
\tikz[scale=1,every node/.style={fill=yellow,circle,inner sep=1pt,minimum width=4mm},] \graph[simple Huffman layout, grow=left]{
"\textvisiblespace"  [probability=0.173684210526],
e [probability=0.1],
t [probability=0.0868421052632],
o [probability=0.0736842105263],
i [probability=0.0710526315789],
a [probability=0.0605263157895],
s [probability=0.0473684210526],
n [probability=0.0473684210526],
m [probability=0.0447368421053],
r [probability=0.0368421052632],
u [probability=0.0342105263158],
l [probability=0.0342105263158],
h [probability=0.0342105263158],
c [probability=0.0342105263158],
y [probability=0.0289473684211],
f [probability=0.0289473684211],
g [probability=0.0263157894737],
p [probability=0.0210526315789],
w [probability=0.0157894736842],
"." [probability=0.0131578947368],
d [probability=0.0105263157895],
b [probability=0.0105263157895],
x [probability=0.00526315789474],
"," [probability=0.00263157894737],
v [probability=0.00263157894737],
k [probability=0.00263157894737],
};
\end{document}

我遇到的问题是,下图中标记的部分有三个子节点而不是两个,而且标签放错了位置:

简单哈夫曼 TikZ

有人知道如何解决这个问题吗?

编辑: 这是一个展示问题的简单示例。感谢 Andrew Swann

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
%\usepackage[lf]{MinionPro}
\usepackage{tikz}
\usepackage[left=0mm,right=0mm,top=0mm,bottom=0mm]{geometry}
\usetikzlibrary{graphs,graphdrawing}

\pgfkeys{
/graph drawing/HuffmanLabel/.style={
/tikz/edge node={node[fill=gray!10,rectangle,font=\footnotesize,inner sep=1pt]{#1}}
},
/graph drawing/HuffmanNode/.style={
/tikz/.cd,circle,inner sep=0pt,outer sep=0pt,fill=black,minimum size=3pt
}
}

\usegdlibrary{SimpleHuffman}

\begin{document}
\tikz[scale=1,every node/.style={fill=yellow,circle,inner sep=1pt,minimum width=4mm}]
\graph[simple Huffman layout, grow=left]{
A [probability=0.4], B [probability=0.4], C [probability=0.2]
};
\end{document}

答案1

这里有两种方法。第一种方法不能解决您的特定情况,但可以在许多情况下提供帮助。第二种方法展示了如何更改绘图的整体样式以避免重叠。

您可以通过添加参数来调整最终节点的位置nudge,例如

B [probability=0.4,nudge up=2mm],

会将节点放置B在比算法原本使用的节点略高的位置。这足以发现意外重叠。在我提供的最小示例中,这给出了

示例输出

与将above选项添加到edge nodes 相结合时。如果没有,above您将不得不进一步移动此节点以减少重叠。

\documentclass[10pt,a4paper]{article}

\usepackage{tikz}

\usetikzlibrary{graphs,graphdrawing}

\pgfkeys{
/graph drawing/HuffmanLabel/.style={
/tikz/edge node={node[fill=gray!10,rectangle,font=\footnotesize,inner
sep=1pt,above]{#1}}
},
/graph drawing/HuffmanNode/.style={
/tikz/.cd,circle,inner sep=0pt,outer sep=0pt,fill=black,minimum size=3pt
}
}

\usegdlibrary{SimpleHuffman}

\begin{document}
\tikz[scale=1,every node/.style={fill=yellow,circle,inner
sep=1pt,minimum width=4mm}] \graph[simple Huffman
layout,grow=left]{
A [probability=0.4],
B [probability=0.4,nudge up=2mm],
C [probability=0.2],
};
\end{document}

参见第 27.6 节“微调节点位置”pgfmanual寻找其他类似的选项。

不幸的是,对于您的示例,您需要调整不属于输入的节点的垂直位置,因此不能直接应用这样的nudge选项。

pos作为一种替代解决方法,您可以使用参数来移动边的标签,HuffmanLabel/.style并使用装饰使边弯曲而不是直:

示例输出

\documentclass[10pt,a4paper]{article}

\usepackage[english]{babel}

\usepackage[left=0mm,right=0mm,top=0mm,bottom=0mm]{geometry}

\usepackage{tikz}

\usetikzlibrary{graphs,graphdrawing,decorations,decorations.pathmorphing}

\pgfkeys{
/graph drawing/HuffmanLabel/.style={/tikz/edge node={
node[fill=gray!10,rectangle,font=\footnotesize,inner sep=1pt,pos=0.7]{#1}}},
/graph drawing/HuffmanNode/.style={
/tikz/.cd,circle,inner sep=0pt,outer sep=0pt,fill=black,minimum size=3pt}}

\usegdlibrary{SimpleHuffman}

\begin{document}
\tikz[scale=1,
  every node/.style={fill=yellow,circle,inner sep=1pt,minimum width=4mm},
  every edge/.append style={decorate,decoration={bent,amplitude=3mm}}]
  \graph[simple Huffman layout, grow=left]{
"\textvisiblespace"  [probability=0.173684210526],
e [probability=0.1],
t [probability=0.0868421052632],
o [probability=0.0736842105263],
i [probability=0.0710526315789],
a [probability=0.0605263157895],
s [probability=0.0473684210526],
n [probability=0.0473684210526],
m [probability=0.0447368421053],
r [probability=0.0368421052632],
u [probability=0.0342105263158],
l [probability=0.0342105263158],
h [probability=0.0342105263158],
c [probability=0.0342105263158],
y [probability=0.0289473684211],
f [probability=0.0289473684211],
g [probability=0.0263157894737],
p [probability=0.0210526315789],
w [probability=0.0157894736842],
"." [probability=0.0131578947368],
d [probability=0.0105263157895],
b [probability=0.0105263157895],
x [probability=0.00526315789474],
"," [probability=0.00263157894737],
v [probability=0.00263157894737],
k [probability=0.00263157894737],
};

\end{document}

相关内容