如何防止二叉树中的节点重叠?
\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}