1

I would like to add extra edges between nodes that aren't specifically related as child and parents in a binary tree and also to change the styling of the edges.

Currently I have the following code:

\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\title{}

\begin{document}
\maketitle

\begin{center}
\begin{tikzpicture}[
  every node/.style = {minimum width = 2em, draw, circle},
  level/.style = {sibling distance = 30mm/#1}
  ]
  \node{u.p}
  child {node {u}
    child{node {u.e}}
    child {edge from parent[draw = none]}
  }
  child {node {u.p.d}};
\end{tikzpicture}
\end{center}

\end{document}

And this is giving me the following output:

enter image description here

Now, my desired output would be to have dashed edges between u and u.p and between u and u.e and also an extra edge connecting u.e to u.p, if possible in red, i.e., something like this:

enter image description here

Thanks for any help in advance.

1 Answer 1

2

I suggest you use the forest package, which makes these modifications relatively easy.

enter image description here

If you want the nodes to be different sizes you can get rid of the minimum width setting. For the red arc, if you want it to come out of the "west" of the u.p node, you can try \draw[red](up.west) to[bend right] (ue);. Otherwise you can adjust the looseness as in the code below, or use out= and in= as for ordinary tikz lines.

In forest, sibling distance is controlled by s sep=, generation distance is controlled by l sep=. Individual nodes can be moved from their calculated position by adjusting their true x and y coordinates as I did for the node u.e. Otherwise it would be placed directly below the u node.

To draw additional edges, name the nodes and then use ordinary tikz drawing commands.

Here is the code:

\documentclass{article}

\usepackage{forest}

\begin{document}

\begin{forest}
    for tree={draw, circle, inner sep=2pt, minimum width=1cm, font=\small, s sep=1cm}
    [u.p, name=up
        [u,edge=dashed
            [u.e, edge=dashed, before drawing tree={x-=1cm}, name=ue]]
        [u.p.d]]
    \draw[red](up) to[bend right, looseness=1.5] (ue);
\end{forest}

\end{document}
1
  • Thanks! I had a look at forest package and it seems to do the work better! The code worked for me, exactly what I wanted.
    – Rodrigo
    Commented Apr 9, 2022 at 15:10

You must log in to answer this question.

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