我正在尝试绘制类似树的数据结构,tikzpicture
但有些节点被折叠了,我不明白为什么。
你能帮助我理解为什么吗?
\documentclass[letterpaper,12pt]{article}
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}
\begin{center}
\begin{tikzpicture}
\tikzstyle{bplus}=[rectangle split, rectangle split horizontal,rectangle split ignore empty parts,draw]
\tikzstyle{every node}=[bplus]
\tikzstyle{level 1}=[sibling distance=70mm]
\tikzstyle{level 2}=[sibling distance=30mm]
\tikzstyle{level 3}=[sibling distance=30mm]
\tikzstyle{level 4}=[sibling distance=30mm]
\node[rectangle split parts=10] {2 \nodepart{two} 4 \nodepart{three} 3 \nodepart{four} 1 \nodepart{five} 6
\nodepart{six} 7 \nodepart{seven} 8 \nodepart{eight} 9 \nodepart{nine} 1 \nodepart{ten} 7} [->]
child {node[rectangle split parts=5] {2 \nodepart{two} 4 \nodepart{three} 3 \nodepart{four} 1 \nodepart{five} 6 }
child[sibling distance=25mm]{node {2 \nodepart{two} 4}}
child[sibling distance=25mm]{node {3 \nodepart{two} 1 \nodepart{three} 6}}
}
child {node[rectangle split parts=6] {6 \nodepart{two} 7 \nodepart{three} 8 \nodepart{four} 9 \nodepart{five} 1 \nodepart{six} 7}
child[sibling distance=25mm]{node {6 \nodepart{two} 7 \nodepart{three} 8}
child[sibling distance=25mm]{node {6}}
child[sibling distance=25mm]{node {7 \nodepart{two} 8}
child{node {7}}
child{node {8}}
}
}
child[sibling distance=25mm] {node {9 \nodepart{two} 1 \nodepart{three} 7}
child{node {9}}
child{node {1 \nodepart{two} 7}
child{node {1}}
child[sibling distance=25mm]{node {7}}
}
}
}
;\end{tikzpicture}
\end{center}
\end{document}
答案1
您无需forest
关心兄弟姐妹之间的距离:
\documentclass[margin=3mm]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta,
shapes.multipart}
\begin{document}
\begin{forest}
for tree = {
rectangle split,
rectangle split horizontal,
rectangle split parts=10,
rectangle split ignore empty parts,
draw,
%
parent anchor=south,
child anchor=north,
calign=edge midpoint,
edge = {-Stealth, semithick},
l sep=6mm,
where level=0{s sep=11mm}{s sep=3mm},
}
[4\nodepart{two} 2 \nodepart{two} 4 \nodepart{three} 3 \nodepart{four} 1 \nodepart{five} 6
\nodepart{six} 7 \nodepart{seven} 8 \nodepart{eight} 9 \nodepart{nine} 1 \nodepart{ten} 7
[2\nodepart{two} 2 \nodepart{two} 4 \nodepart{three} 3 \nodepart{four} 1 \nodepart{five} 6
[2\nodepart{two} 4]
[3\nodepart{two} 1 \nodepart{three} 6]
]
[6\nodepart{two} 7 \nodepart{three} 8 \nodepart{four} 9 \nodepart{five} 1
\nodepart{six} 7,fit=band
[6\nodepart{two} 7 \nodepart{three} 8
[6]
[7\nodepart{two} 8
[7]
[8]
]
]
[9\nodepart{two} 1 \nodepart{three} 7
[9]
[1\nodepart{two} 7
[1]
[7]
]
]
]
]
\end{forest}
\end{document}
答案2
\documentclass[letterpaper,12pt]{article}
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}
\begin{center}
\begin{tikzpicture}
\tikzstyle{bplus}=[rectangle split, rectangle split horizontal,rectangle split ignore empty parts,draw]
\tikzstyle{every node}=[bplus]
\tikzstyle{level 1}=[sibling distance=70mm]
\tikzstyle{level 2}=[sibling distance=30mm]
\tikzstyle{level 3}=[sibling distance=30mm]
\tikzstyle{level 4}=[sibling distance=30mm]
\node[rectangle split parts=10] {2 \nodepart{two} 4 \nodepart{three} 3 \nodepart{four} 1 \nodepart{five} 6
\nodepart{six} 7 \nodepart{seven} 8 \nodepart{eight} 9 \nodepart{nine} 1 \nodepart{ten} 7} [->]
child {node[rectangle split parts=5] {2 \nodepart{two} 4 \nodepart{three} 3 \nodepart{four} 1 \nodepart{five} 6 }
child[sibling distance=25mm]{node {2 \nodepart{two} 4}}
child[sibling distance=25mm]{node {3 \nodepart{two} 1 \nodepart{three} 6}}
}
child {node[rectangle split parts=6] {6 \nodepart{two} 7 \nodepart{three} 8 \nodepart{four} 9 \nodepart{five} 1 \nodepart{six} 7}
child[sibling distance=25mm]{node {6 \nodepart{two} 7 \nodepart{three} 8}
child[sibling distance=10mm]{node {6}}
child[sibling distance=10mm]{node {7 \nodepart{two} 8}
child[sibling distance=16mm]{node {7}}
child[sibling distance=16mm]{node {8}}
}
}
child[sibling distance=25mm] {node {9 \nodepart{two} 1 \nodepart{three} 7}
child[sibling distance=20mm]{node {9}}
child[sibling distance=20mm]{node {1 \nodepart{two} 7}
child[sibling distance=15mm]{node {1}}
child[sibling distance=15mm]{node {7}}
}
}
}
;\end{tikzpicture}
\end{center}
\end{document}