4

I would like to create an upside down tree with concentrate edges and labels inside the nodes as labels above them.

desired result

Right now I've come so far. But I still need the edges to concentrate/combine and put the labels + and * next to the overlapping bit. And I need to add the extra labels x, y, and z to the top nodes.

current situation

How do I get the tree from the first image? (The corners do not need to be rounded.) It is not absolutely necessary to use 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

2 Answers 2

4

Here are both a version without and with tikz-qtree.

Code

\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}

Output

enter image description here

1
  • I like your improvement of placing the operators above the joins! Dec 5, 2013 at 21:28
2

A forest (a TikZ-based package) solution:

Code

\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}

Output

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .