代码

代码

我想创建一个倒置的树,其边缘集中,节点内部的标签作为其上方的标签。

期望结果

现在我已经走了这么远。但我仍然需要集中/合并边缘并将标签+*放在重叠位旁边。我需要将额外的标签xy和添加z到顶部节点。

现在的情况

我如何从第一张图片中获取树?(角不需要圆化。)不一定非要使用 tikz-qtree。

\tikzstyle{var} = [draw,shape=rectangle,minimum size=2em,
                        inner sep=2pt,fill=white!20]
\tikzstyle{operator} = [draw=none,fill=none,minimum size=2em,
                        inner sep=2pt,fill=white!20]
\begin{tikzpicture}[grow'=up,level distance=1.25cm,sibling distance=1cm,]
  \tikzset{every node/.style={var}}
  \Tree [.7 \edge node[auto=right,style={operator}] {$+$};
            [.6 \edge node[auto=right,style={operator}] {$*$};
                [.2 ] [.3 ] ]
                      [.1 ] ]
\end{tikzpicture}

答案1

以下有两个版本:不带和带tikz-qtree

代码

\documentclass[tikz, border=2mm]{standalone}
\usepackage{tikz-qtree}
\usetikzlibrary{trees}

\begin{document}

\begin{tikzpicture}
[   numbers/.style={draw,rounded corners=1mm,minimum width=0.7cm,minimum height=0.7cm},
    operatorr/.style={draw=none,minimum width=0cm,minimum width=0cm,above=4mm},
    label/.style={above,font=\bf},
]
\node[numbers] (7) {7}
[   edge from parent fork up,
    grow=up,
    sibling distance=1.4cm,
    level distance=1.5cm,
]
    child {
        child {node[numbers] (1) {1}}
    }
    child[missing]{}
    child {node[numbers] (6) {6}
        child {node[numbers] (2) {2}}
        child {node[numbers] (3) {3}}
    };
\node[operatorr] at (7.90) {+};
\node[operatorr] at (6.90) {*};
\node[label] at (3.90) {x};
\node[label] at (2.90) {y};
\node[label] at (1.90) {z};

\end{tikzpicture}

\tikzstyle{var} = [draw,shape=rectangle,minimum size=2em,rounded corners=1mm]
\tikzstyle{operator} = [draw=none,fill=none,above,pos=0]

\begin{tikzpicture}
[   grow'=up,
    level distance=1.5cm,
    sibling distance=1cm,
    edge from parent fork up,
    edge from parent/.style={draw,rounded corners=1mm}
]
  \tikzset{every node/.style={var}}
  \Tree [.7 \edge node[operator] {$+$};
            [.6 \edge node[operator] {$*$};
            [.\node[label=90:\textbf{x}] {2}; ] [.\node[label=90:\textbf{y}] {3}; ]
          ]
          [ [.\node[label=90:\textbf{z}] {1};] 
          ]
        ]
\end{tikzpicture}

\end{document}

输出

在此处输入图片描述

答案2

Aforest(基于 TikZ 的包)解决方案:

代码

\documentclass[border=2pt]{standalone}
\usepackage{forest}

\begin{document}

\begin{forest}
  for tree={
    grow=north,draw,minimum width=3em,
    l+=1cm, s sep=1cm,
    parent anchor=north, child anchor=south,
    edge path={
      \noexpand\path[\forestoption{edge},rounded corners](!u.parent anchor)--+(0,.75)-|(.child anchor)\forestoption{edge label};
    }
  }
  [7,label={80:$+$}
    [6,label={80:$*$}
      [1,label={north:$z$},tier=top]
      [2,label={north:$y$}]
    ]
    [1,label={north:$x$},tier=top]  
  ]
\end{forest}

\end{document}

输出

在此处输入图片描述

相关内容