2

I'd like to clean up my tree by drawing an edge between parent nodes. In the following, I'd like to have the 0 nodes (which are children now) adjacent to the numbered nodes (ie on the "parent" level). How might I do this? Am I missing something simple? Thanks!

\documentclass{article}
\usepackage{forest}
\begin{document}

\begin{forest}
for tree={circle, draw, l sep=20pt, s sep=5 mm}
[0, green
    [0, green]
    [1, red
        [0, green]
        [2, red
            [0, green]
            [3, red]
        ]
        [3, red
            [0, green]
            [2, red]
        ]
    ]
    [2, red
        [0, green]
        [1, red
            [0, green]
            [3, red]
        ]
        [3, red
            [0, green]
            [1, red]
        ]
    ]
    [3, red
        [0, green]
        [1, red
            [0, green]
            [2, red]        
        ]
        [2, red
            [0, green]
            [1, red]            
        ]
    ]
]
\end{forest}
\end{document}

by letting each 0 node be on the same level as the parents and connecting the parent.

That is, I'd like it to look something like

0 ----- 0
       / \
0 --- 1   2 --- 0
     / \
    2   3
5
  • Normally you can achieve this by adding appropriate tiers but in your tree the 0 nodes occur at all levels.
    – user121799
    Feb 13, 2019 at 1:22
  • @marmot: would it be possible to do that just with the first 0 node, for example? As in adding two 0 nodes at the top level, one with the remaining full tree, and one which is the final node?
    – rrrrr
    Feb 13, 2019 at 1:28
  • You could try \documentclass{article} \usepackage{forest} \begin{document} \begin{forest} for tree={circle, draw, l sep=20pt, s sep=5 mm} [0, green [0, green,tier=murmel] [1, red [0, green,tier=murmel] [2, red [0, green] [3, red] ] [3, red [0, green] [2, red] ] ] (rest of your tree start with [2, red) \end{forest}\end{document} but this doesn't look convincing to me. This is not because forest cannot do it, but because I do not understand your request.
    – user121799
    Feb 13, 2019 at 3:04
  • @marmot: thanks, edited question to be more clear
    – rrrrr
    Feb 13, 2019 at 3:10
  • To attract more users to your question, please add a preamble: documentclass{article} \usepackage{forest} \begin{document} and \end{document}, show the current output and then the sketch of what you want to achieve.
    – user121799
    Feb 13, 2019 at 3:15

2 Answers 2

2

This not really an answer but more to clarify if you want something of this sort.

\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={circle, draw, l sep=20pt, s sep=15mm}
[0, green,alias=L1
%    [0, green]
    [1, red,alias=L2
%        [0, green]
        [2, red,alias=L3
%            [0, green]
            [3, red]
        ]
        [3, red,alias=L4
%            [0, green]
            [2, red]
        ]
    ]
    [2, red,alias=L5
%        [0, green]
        [1, red,alias=L6
%            [0, green]
            [3, red]
        ]
        [3, red,alias=L7
%            [0, green]
            [1, red]
        ]
    ]
    [3, red,alias=L8
%        [0, green]
        [1, red,alias=L9
%            [0, green]
            [2, red]        
        ]
        [2, red,alias=L10
%            [0, green]
            [1, red]            
        ]
    ]
]
\foreach \X in {1,...,10}
{\ifnum\X=1
\draw (L\X) -- ++ (-1.5cm,0) node[circle,left,draw,green]{0};
\else
\draw (L\X) -- ++ (-1cm,0) node[circle,left,draw,green]{0};
\fi}
\end{forest}
\end{document}

enter image description here

2
  • As a follow-on, how might I annotate the aliased edges to from nonzero to 0 nodes?
    – rrrrr
    Feb 13, 2019 at 3:34
  • 1
    @rrrrr What do you mean by "annotate"? Like putting labels on the edges? If so, always the same? (One can put labels on the path with \draw (L\X) -- ++ (-1.5cm,0) node[circle,left,draw,green]{0} node[midway,above]{text};.)
    – user121799
    Feb 13, 2019 at 4:10
3

Not a tikz-tree solution: I just try to replicate your graph using normal TikZ

\documentclass[tikz,margin=3mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node [circle,draw] (0c) at (0,0) {0};
\node [circle,draw] (1) at ($(0c)+(-120:1.5)$) {1};
\node [circle,draw] (2r) at ($(0c)+(-60:1.5)$) {2};
\node [circle,draw] (2l) at ($(1)+(-120:1.5)$) {2};
\node [circle,draw] (3) at ($(1)+(-60:1.5)$) {3};
\node [circle,draw] (0al) at ($(0c)+(-1.5,0)$) {0};
\node [circle,draw] (0bl) at ($(1)+(-1.5,0)$) {0};
\node [circle,draw] (0r) at ($(2r)+(1.5,0)$) {0};
\draw (0c)--(1)
    (0c)--(2r)
    (1)--(2l)
    (1)--(3)
    (0c)--(0al)
    (1)--(0bl)
    (2r)--(0r);
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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