![使用 tikz 绘制数组树](https://linux22.com/image/449788/%E4%BD%BF%E7%94%A8%20tikz%20%E7%BB%98%E5%88%B6%E6%95%B0%E7%BB%84%E6%A0%91.png)
答案1
这是一个使用的解决方案forest
:
基本思想是,图表是一棵向北生长的树(grow'=north
),箭头指向后方(latex-
)。每个节点(顶部的叶子除外)都是一个多部分节点,使用\tikzset
和定义rectangle split
。
\documentclass{article}
\usepackage{forest}
\usetikzlibrary {shapes.multipart}
\tikzset{multinode/.style={rectangle split, rectangle split horizontal, rectangle split parts=#1, anchor=center}}
\colorlet{c1}{yellow!30}
\colorlet{c2}{green!30}
\colorlet{c3}{red!30}
\colorlet{c4}{cyan!30}
\begin{document}
\begin{forest}
for tree={grow'=north, l sep=7mm, edge={gray!70,very thick, latex-, shorten <=2pt, shorten >=2pt}, draw, anchor=center}
[\nodepart{one}1\nodepart{two}1\nodepart{three}2\nodepart{four}2\nodepart{five}5\nodepart{six}7\nodepart{seven}7\nodepart{eight}8, multinode=8, {rectangle split part fill={c2,c4,c1,c3,c3,c1,c4,c2}}, for children={multinode=4}
[\nodepart{one}1\nodepart{two}2\nodepart{three}7\nodepart{four}8, {rectangle split part fill={c2,c1,c1,c2}}, for children={multinode=2}
[\nodepart{one}2\nodepart{two}7, fill=c1, for descendants={fill=c1}[7][2]]
[\nodepart{one}1\nodepart{two}8, fill=c2, for descendants={fill=c2}[8][1]]
]
[\nodepart{one}1\nodepart{two}2\nodepart{three}5\nodepart{four}7, {rectangle split part fill={c4,c3,c3,c4}}, for children={multinode=2}
[\nodepart{one}2\nodepart{two}5, fill=c3, for descendants={fill=c3}[2][5]]
[\nodepart{one}1\nodepart{two}7, fill=c4, for descendants={fill=c4}[7][1]]
]
]
\end{forest}
\end{document}