Tikz 树的边缘始终连接到元素顶部中心

Tikz 树的边缘始终连接到元素顶部中心
\documentclass[]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,fit,shapes.geometric}
\usepackage{amssymb}

\begin{document}
\begin{tikzpicture}[]
\tikzstyle{tree_op}=[rectangle,draw=black,fill=gray!30,thick,minimum size=6mm,inner sep=0pt]
\tikzstyle{tree_subtree}=[rectangle,draw = black,densely dotted,fill=gray!30,thick,minimum size=6mm,inner sep=0pt]
\tikzstyle{triangle}=[inner sep=0pt,text=red,draw, regular polygon, regular polygon sides=3, align=center]

\tikzstyle{level 1}=[sibling distance=14mm,level distance=10mm]
\tikzstyle{level 2}=[sibling distance=14mm]
\tikzstyle{level 3}=[sibling distance=20mm]
\tikzstyle{level 4}=[sibling distance=11mm,level distance=11mm]

\node [tree_op] (root){$\rightarrow $}
  child {node [triangle] (t1) {$\triangle_1$}}
  child {node [triangle] (ti) {$\triangle_i$}}
  child {node [triangle] (tj) {$\triangle_j$}}
  child {node [triangle] (tn) {$\triangle_n$}}
;

\end{tikzpicture}
\end{document}

在此处输入图片描述

我对当前结果有两个疑问:

  1. 三角形有不同的尺寸
  2. 边应该连接到三角形的顶点

答案1

为了解决您的问题,我添加了

  1. 一种equal size样式(依赖于eqparbox),确保节点具有相同的大小,并且
  2. child anchor=north

请注意,\tikzstyle已弃用,这就是我用相应\tikzset语法替换它的原因。(我还增加了,level distance但当然,如果您不喜欢,您可以撤消它。)

\documentclass[]{standalone}
\usepackage{tikz}
\usepackage{eqparbox}
\newbox\nodebox
\usetikzlibrary{trees,shapes.geometric}
\usepackage{amssymb}

\begin{document}
\begin{tikzpicture}[]
\tikzset{tree_op/.style={rectangle,draw=black,fill=gray!30,thick,minimum size=6mm,inner sep=0pt},
tree_subtree/.style={rectangle,draw = black,densely dotted,fill=gray!30,thick,minimum size=6mm,inner sep=0pt},
triangle/.style={inner sep=0pt,text=red,draw, regular polygon, regular polygon
sides=3, align=center,equal size=T},
level 1/.style={sibling distance=14mm,level distance=14mm},
level 2/.style={sibling distance=14mm},
level 3/.style={sibling distance=20mm},
level 4/.style={sibling distance=11mm,level distance=11mm},
equal size/.style={execute at begin
 node={\setbox\nodebox=\hbox\bgroup},
 execute at end
 node={\egroup\eqmakebox[#1][c]{\copy\nodebox}}}}

\node [tree_op] (root){$\rightarrow $}
[child anchor=north]
  child {node [triangle] (t1) {$\triangle_1$}}
  child {node [triangle] (ti) {$\triangle_i$}}
  child {node [triangle] (tj) {$\triangle_j$}}
  child {node [triangle] (tn) {$\triangle_n$}}
;

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容