vlambda博客
学习文章列表

【LaTeX应用】树形结构,二叉树



二叉树的形式很多,借着递归的二叉树形式,画个递归过程

图形比较大,标注的信息有限,示意图比较简单




下面是实现过程的主要代码部分,每条代码的具体语法这里不一一解释了,通过规范的排版,基本上已经将每条代码的含义解释清楚了,请耐心点阅读。这些代码可以直接运行,实现如图所示效果,可以当做模板应用。

\begin{tikzpicture}[->,>=stealth,level/.style={sibling distance = 7cm/#1,level distance = 2cm}, edge from parent path = {}, arrow1/.style = { draw = black, dashed, thick, -{Latex[length = 2mm, width = 1.5mm]}, }, arrow2/.style = { draw = black, thick, -{Latex[length = 2mm, width = 1.5mm]}, }, treenode/.style = {align=center, inner sep=0pt, text centered,font=\sffamily}, arnx/.style = {treenode, rectangle, minimum width=2em, minimum height=1em}]   \node (n0) [arnx] {$ f(6,5,6) $} child{ node (n01) [arnx] {$ f(5,5,6) $}  child{ node (n11) [arnx] {$ f(4,5,6) $}  child{ node (n12) [arnx] {$ f(3,5,6) $} child{ node (n21) [arnx] {$ f(2,.) $}} child{ node (n22) [arnx] {$ f(1,.) $}} }  child{ node (n13) [arnx] {$ f(2,.) $} } } child{ node (n23) [arnx] {$ f(3,5,6) $} child{ node (n31) [arnx] {$ f(2,.) $}} child{ node (n32) [arnx] {$ f(1,.) $}} }  } child{ node (n02) [arnx] {$ f(4,5,6) $}  child{ node (n41) [arnx] {$ f(3,5,6) $} child{ node (n42) [arnx] {$ f(2,.) $}} child{ node (n43) [arnx] {$ f(1,.) $}} }  child{ node (n51) [arnx] {$ f(2,.) $} } };   \path[arrow1] ([xshift=-2ex] n01.north) -- node[left]{0}([xshift=-2ex] n0.south); \path[arrow2] ([xshift=2ex] n0.south) -- ([xshift=2ex] n01.north);   \path[arrow1] ([xshift=-2ex] n02.north) -- node[left]{5}([xshift=-2ex] n0.south); \path[arrow2] ([xshift=2ex] n0.south) -- ([xshift=2ex] n02.north);  \path[arrow1] ([xshift=-2ex] n11.north) -- node[left]{5}([xshift=-2ex] n01.south); \path[arrow2] ([xshift=2ex] n01.south) -- ([xshift=2ex] n11.north);   \path[arrow1] ([xshift=-2ex] n12.north) -- node[left]{4}([xshift=-2ex] n11.south); \path[arrow2] ([xshift=2ex] n11.south) -- ([xshift=2ex] n12.north);  \path[arrow1] ([xshift=-2ex] n21.north) -- node[left]{1}([xshift=-2ex] n12.south); \path[arrow2] ([xshift=2ex] n12.south) -- ([xshift=2ex] n21.north);  \path[arrow1] ([xshift=-2ex] n13.north) -- node[left]{1}([xshift=-2ex] n11.south); \path[arrow2] ([xshift=2ex] n11.south) -- ([xshift=2ex] n13.north);  \path[arrow1] ([xshift=-2ex] n23.north) -- node[left]{4}([xshift=-2ex] n01.south); \path[arrow2] ([xshift=2ex] n01.south) -- ([xshift=2ex] n23.north);  \path[arrow1] ([xshift=-2ex] n31.north) -- node[left]{1}([xshift=-2ex] n23.south); \path[arrow2] ([xshift=2ex] n23.south) -- ([xshift=2ex] n31.north);  \path[arrow1] ([xshift=-2ex] n32.north) -- node[left]{1}([xshift=-2ex] n23.south); \path[arrow2] ([xshift=2ex] n23.south) -- ([xshift=2ex] n32.north);  \path[arrow1] ([xshift=-2ex] n22.north) -- node[left]{1}([xshift=-2ex] n12.south); \path[arrow2] ([xshift=2ex] n12.south) -- ([xshift=2ex] n22.north);  \path[arrow1] ([xshift=-2ex] n41.north) -- node[left]{4}([xshift=-2ex] n02.south); \path[arrow2] ([xshift=2ex] n02.south) -- ([xshift=2ex] n41.north);  \path[arrow1] ([xshift=-2ex] n51.north) -- node[left]{1}([xshift=-2ex] n02.south); \path[arrow2] ([xshift=2ex] n02.south) -- ([xshift=2ex] n51.north);  \path[arrow1] ([xshift=-2ex] n42.north) -- node[left]{1}([xshift=-2ex] n41.south); \path[arrow2] ([xshift=2ex] n41.south) -- ([xshift=2ex] n42.north);  \path[arrow1] ([xshift=-2ex] n43.north) -- node[left]{1}([xshift=-2ex] n41.south); \path[arrow2] ([xshift=2ex] n41.south) -- ([xshift=2ex] n43.north); \end{tikzpicture}

在导言区加几行代码

\usepackage{tikz}\usetikzlibrary{chains,scopes,positioning,backgrounds,shapes,fit,shadows,calc,arrows.meta}

去掉了默认的二叉树线条,重新用path画出路径