zl程序教程

您现在的位置是:首页 >  后端

当前栏目

3.1 数据结构(Graphviz)

数据结构 3.1 graphviz
2023-09-11 14:20:29 时间

文章目录


  LaTex绘图使用tikz包。Tikz这个包非常强大,而tikz又可以集成graphviz。但是如果没有特殊需要,比如图中插入数学公式,可以单独使用graphviz。下面我以一棵红黑树为例子,介绍单独使用graphviz画图。

配置

  在选项里找到设置:
在这里插入图片描述
  在pdfLaTex命令里加上–shell-escape,如下图,图片虽然不清晰,但是位置可以看出来。
在这里插入图片描述

代码

  此外,还需要机器上安装了graphviz并且dot命令配置到了环境变量中。然后就可以写代码了。以下是我写的代码:

\documentclass[UTF8, a4paper,12pt]{article}
\usepackage[pdf]{graphviz}
\begin{document}
	\digraph g {
		graph[layout=fdp]
		node[shape=circle;height=1]
		"5"[pos="2,4!";fontcolor=white;style=filled;fillcolor=black]
		"3"[pos="1,3!";fontcolor=white;style=filled;fillcolor=red]
		"7"[pos="3,3!";fontcolor=white;style=filled;fillcolor=red]
		"0"[pos="0,2!";fontcolor=white;style=filled;fillcolor=black]
		"4"[pos="1.5,2!";fontcolor=white;style=filled;fillcolor=black]
		"6"[pos="2.5,2!";fontcolor=white;style=filled;fillcolor=black]
		"8"[pos="3.5,2!";fontcolor=white;style=filled;fillcolor=black]
		"2"[pos="0.5,1!";fontcolor=white;style=filled;fillcolor=red]
		"9"[pos="4,1!";fontcolor=white;style=filled;fillcolor=red]
		"5" -> "3"
		"5" -> "7"
		"3" -> "0"
		"3" -> "4"
		"7" -> "6"
		"7" -> "8"
		"0" -> "2"
		"8" -> "9"
	}
\end{document}

效果

  编译后,红黑树就画完了,如图:
在这里插入图片描述