![二进制节点在第 5 级重叠](https://linux22.com/image/438669/%E4%BA%8C%E8%BF%9B%E5%88%B6%E8%8A%82%E7%82%B9%E5%9C%A8%E7%AC%AC%205%20%E7%BA%A7%E9%87%8D%E5%8F%A0.png)
如何防止二叉树中的节点重叠?
\documentclass[border=5pt, tikz]{standalone}
\begin{document}
\begin{tikzpicture}[
every node/.style = {minimum width = 1em, draw, circle},
level/.style = {sibling distance = 80mm/#1}
]
\node {7}
child {node {6}
child {node {5}
child {node {4}
child {node {3}
child {node {2}}
child {node {1}}
}
child {node {2}}
}
child {node {3}
child {node {2}}
child {node {1}}
}
}
child {node {4}
child {node {3}
child {node {2}}
child {node {1}}
}
child {node {2}}
}
}
child {node {5}
child {node {4}
child {node {3}
child {node {2}}
child {node {1}}
}
child {node {2}}
}
child {node {3}
child {node {2}}
child {node {1}}
}
};
\end{tikzpicture}
\end{document}
答案1
您可以手动管理每个级别的兄弟距离:
\documentclass[border=5pt, tikz]{standalone}
\begin{document}
\begin{tikzpicture}[
every node/.style = {minimum width = 1em, draw, circle},
level/.style = {sibling distance = 80mm/#1},
level 3/.style = {sibling distance = 20mm},
level 4/.style = {sibling distance = 10mm}
]
\node {7}
child {node {6}
child {node {5}
child {node {4}
child {node {3}
child {node {2}}
child {node {1}}
}
child {node {2}}
}
child {node {3}
child {node {2}}
child {node {1}}
}
}
child {node {4}
child {node {3}
child {node {2}}
child {node {1}}
}
child {node {2}}
}
}
child {node {5}
child {node {4}
child {node {3}
child {node {2}}
child {node {1}}
}
child {node {2}}
}
child {node {3}
child {node {2}}
child {node {1}}
}
};
\end{tikzpicture}
\end{document}