4

I have the following forest:

\documentclass[tikz,border=5pt]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\tikzset{every label/.style = {font=\footnotesize,  text=red,
                               inner sep=1pt }}

\begin{document}    

\begin{forest}
    for tree = {
        draw, 
        s sep=5mm, 
        l sep=5mm,
        edge={thick,-Latex},
                }
[{(5,5,6)}, label=right:{(1,2)}
    [{(4,4,4)}, label=left:{(0,0)}
        [{(2,3,3)}, label=left:{(2,2)}, name=c
            [{(1,1,2)}, label=left:{(1,0)}, name = d    
                [{(0,0,0)}, label=below:{(0,1)}]
            ]
    ]        
]
    [{(3,4,5)}, label=right:{(0,1)}, name=f
        [{(2,2,4)}, label=below:{(2,2)}, name = g]
    [{(1,3,4)}, label=right:{(1,0)}
            [{(0,2,2)}, label=below:{(0,1)}, name = h]
            [{(0,1,3)}, label=below:{(0,1)}, name = j]
            ]        
        ]    
    ]
]
\draw[thick, -Latex] (f) -- (c);
\draw[thick, -Latex] (c) -- (h);
\draw[thick, -Latex] (g) -- (d);
\draw[thick, -Latex] (g) -- (j);
\end{forest}
\end{document}

Now I need to add a few more nodes to the left from the forest. In particular, I want a node (5, 5, 5) to the left from (4, 4, 4) (at the same level) from which there would be 2 arcs: a left one to a new node (1, 1,1) and right one to the existing node (2, 3, 3).

I know how to connect nodes by adding names, so I believe adding arc from (5, 5, 5) to (2, 3, 3) is not an issue, but I don't know how to add these new nodes (5, 5, 5) and (1, 1,1) to the existing forest.

1 Answer 1

7

You can add a node that's unconnected to its parent by using the no edge key. This will allow you to place the (5,5,5) node at the same level as the (4,4,4) node. Then the rest is just like you've done with the other nodes. I've increased the sibling separation at the top level to allow the extra node to fit nicely.

\documentclass[tikz,border=5pt]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\tikzset{every label/.style = {font=\footnotesize,  text=red,
                               inner sep=1pt }}

\begin{document}    

\begin{forest}
    for tree = {
        draw, 
        s sep=5mm, 
        l sep=5mm,
        edge={thick,-Latex},
                }
[{(5,5,6)}, label=right:{(1,2)},s sep=1cm
    [{(5,5,5)},no edge,name=k,
        [{(1,1,1)} ]]
    [{(4,4,4)}, label=left:{(0,0)}
        [{(2,3,3)}, label=left:{(2,2)}, name=c
            [{(1,1,2)}, label=left:{(1,0)}, name = d    
                [{(0,0,0)}, label=below:{(0,1)}]
            ]
    ]        
]
    [{(3,4,5)}, label=right:{(0,1)}, name=f
        [{(2,2,4)}, label=below:{(2,2)}, name = g]
    [{(1,3,4)}, label=right:{(1,0)}
            [{(0,2,2)}, label=below:{(0,1)}, name = h]
            [{(0,1,3)}, label=below:{(0,1)}, name = j]
            ]        
        ]    
    ]
]
\draw[thick, -Latex] (f) -- (c);
\draw[thick, -Latex] (c) -- (h);
\draw[thick, -Latex] (g) -- (d);
\draw[thick, -Latex] (g) -- (j);
\draw[thick, -latex] (k) -- (c);
\end{forest}
\end{document}

output of code

0

You must log in to answer this question.

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