4

I want to draw a tree with some edges. See my code given below.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[level distance=1.5cm,
 level 1/.style={sibling distance=3cm},
 level 2/.style={sibling distance=1.5cm}]
\node {$1$}
  child {node {$2$}
  child {node {$4$}}
  child {node {$5$}}
}
child {node {$3$}
child {node {$6$}}
  child {node {$7$}}
};

 \end{tikzpicture}

  \end{document}

Question : I want to make nodes as small circles and How to insert an edge between node 4 and node 5?

1 Answer 1

7

Like this:

enter image description here

\documentclass{article}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}[
 level distance=1.5cm,
 level 1/.style={sibling distance=3cm},
 level 2/.style={sibling distance=1.5cm},
 every node/.style = {circle, draw, minimum size=1.2em,  inner sep=2pt}
                    ]
\node {$1$}
  child {node {$2$}
  child {node (n4) {$4$}}
  child {node (n5) {$5$}}
}
child {node {$3$}
child {node (n6) {$6$}}
  child {node {$7$}}
};
\draw (n4) -- (n5);
    \end{tikzpicture}
\end{document}

Addendum: With package forest code is more simple:

\documentclass{article}
\usepackage{forest}

\begin{document}
    \begin{forest}
for tree = {
    circle, draw, minimum size=1.2em,  inner sep=2pt,
    l sep = 12mm,
    s sep = 12mm,
    math content
            }
[1
    [2
        [4, name=n4]
        [5, name=n5]
    ]
    [3
        [6, name=n6]
        [7]
    ]
]
\draw (n4) -- (n5);
    \end{forest}
\end{document}

enter image description here

Addendum (2): Connection node 4 and 6 which avoid node 5:

enter image description here

for this you need ad name for node "6" and draw line as:

\draw (n4) to [bend right] (n6);
2
  • Thanks. But drawing edge between 4 and 6 is not looking good. It is going through node $5$. I am using method 1.
    – nice guy
    Commented Sep 8, 2019 at 15:36
  • @niceguy, in question you mentioned only between 4 and 5 :-). line between 4 and 6 can avoid node 5 by bending it.
    – Zarko
    Commented Sep 8, 2019 at 16:03

You must log in to answer this question.

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