我正在尝试使用 tikz 绘制一棵 k-ary 树。
不幸的是,我无法避免节点重叠。我也尝试过设置level/.style={sibling distance=30mm/#1}, growth parent anchor=south
并试验这些设置,但到目前为止毫无效果。如果顶部节点位置没有空间(就像原始树一样),那就太好了。
这是我使用的代码:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{figure}
\begin{tikzpicture}[]
\node{}
child {node {1}
child {node {1}
child {node {1}
child {node {0}}
child {node {1}}
child {node {0}}
child {node {0}}
}
child {node {1}
child {node {0}}
child {node {0}}
child {node {1}}
child {node {1}}
}
child {node {0}}
child {node {0}}
}
child {node {1}
child {node {1}
child {node {0}}
child {node {0}}
child {node {1}}
child {node {0}}
}
child {node {0}}
child {node {0}}
child {node {0}}
}
child {node {0}}
child {node {1}
child {node {0}}
child {node {0}}
child {node {0}}
child {node {1}
child {node {0}}
child {node {0}}
child {node {1}}
child {node {0}}
}
}
}
child {node {0}}
child {node {1}
child {node {0}}
child {node {1}
child {node {0}}
child {node {1}
child {node {1}}
child {node {0}}
child {node {1}}
child {node {0}}
}
child {node {0}}
child {node {1}
child {node {1}}
child {node {0}}
child {node {0}}
child {node {0}}
}
}
child {node {0}}
child {node {0}}
}
child {node {1}
child {node {1}
child {node {1}
child {node {0}}
child {node {1}}
child {node {1}}
child {node {0}}
}
child {node {1}
child {node {0}}
child {node {1}}
child {node {0}}
child {node {0}}
}
child {node {1}
child {node {0}}
child {node {1}}
child {node {0}}
child {node {0}}
}
child {node {0}}
}
child {node {0}}
child {node {0}}
child {node {0}}
};
\end{tikzpicture}
\end{figure}
\end{document}
答案1
使用forest
。它会自动间隔节点以使树尽可能紧凑,并且语法更简单。根据您的需要,您还可以通过编程生成树。
\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage[linguistics]{forest}
\begin{document}
\begin{forest}for tree={inner sep=0pt,outer sep=0pt}
[1
[1
[1
[0]
[1]
[0]
[0]
]
[1
[0]
[0]
[1]
[1]
]
[0]
[0]
]
[1
[1
[0]
[0]
[1]
[0]
]
[0]
[0]
[0]
]
[0]
[1
[0]
[0]
[0]
[1
[0]
[0]
[1]
[0]
]
]
[0]
[1
[0]
[1
[0]
[1
[1]
[0]
[1]
[0]
]
[0]
[1
[1]
[0]
[0]
[0]
]
]
[0]
[0]
]
[1
[1
[1
[0]
[1]
[1]
[0]
]
[1
[0]
[1]
[0]
[0]
]
[1
[0]
[1]
[0]
[0]
]
[0]
]
[0]
[0]
[0]
]
]
\end{forest}
\end{document}
答案2
看来这forest
确实是适合这项工作的工具。显然,要使用 TikZ 按照你的意愿做到这一点,你需要使用库的算法graphdrawing
,这需要 LuaLaTeX(有关 GraphDrawing 引擎,请参阅第 26 节,有关可自定义的树键,请参阅第 21 节)TikZ 手册):
\documentclass[border=5mm,tikz]{standalone}
\usetikzlibrary{graphdrawing}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}[tree layout, level 4/.style={sibling distance=0.01em}]
\node{}
child {node {1}
child {node {1}
child {node {1}
child {node {0}}
child {node {1}}
child {node {0}}
child {node {0}}
}
child {node {1}
child {node {0}}
child {node {0}}
child {node {1}}
child {node {1}}
}
child {node {0}}
child {node {0}}
}
child {node {1}
child {node {1}
child {node {0}}
child {node {0}}
child {node {1}}
child {node {0}}
}
child {node {0}}
child {node {0}}
child {node {0}}
}
child {node {0}}
child {node {1}
child {node {0}}
child {node {0}}
child {node {0}}
child {node {1}
child {node {0}}
child {node {0}}
child {node {1}}
child {node {0}}
}
}
}
child {node {0}}
child {node {1}
child {node {0}}
child {node {1}
child {node {0}}
child {node {1}
child {node {1}}
child {node {0}}
child {node {1}}
child {node {0}}
}
child {node {0}}
child {node {1}
child {node {1}}
child {node {0}}
child {node {0}}
child {node {0}}
}
}
child {node {0}}
child {node {0}}
}
child {node {1}
child {node {1}
child {node {1}
child {node {0}}
child {node {1}}
child {node {1}}
child {node {0}}
}
child {node {1}
child {node {0}}
child {node {1}}
child {node {0}}
child {node {0}}
}
child {node {1}
child {node {0}}
child {node {1}}
child {node {0}}
child {node {0}}
}
child {node {0}}
}
child {node {0}}
child {node {0}}
child {node {0}}
};
\end{tikzpicture}
\end{document}
请注意第二级的第二个数字(0),它不在它应该在的位置,我不知道为什么,并尝试了很多键来将它定位得更好,但都没有成功,因为我不知道幕后发生了什么......