2

How to produce this figure: enter image description here

I need help in producing this graph in tikz. The color and shading does not matter.

I can only produce the 3 levels with circle in the second level. But I do not know how to make the level 2 circle disappear and draw a dotted line. Also I do not know how to add edge label.

\documentclass[border=10pt,varwidth]{standalone}
\usepackage{tikz}
\usepackage{comment}
\usetikzlibrary{calc, shapes, backgrounds}
\usepackage{amsmath, amssymb}
\pagecolor{olive!50!yellow!0!white}
\begin{document}
\begin{tikzpicture}
 \node [circle,draw]{A} [level distance=10mm,sibling distance=25mm]
child { node [circle,draw]{B} [level distance=10mm ,sibling distance=15mm]
child {node [circle,draw]{D}}
}
child {node [circle,draw] {E} [level distance=10mm ,sibling distance=15mm]
child {node [circle,draw] {F}}
child {node [circle,draw]{G}}
};
\end{tikzpicture}
\end{document}

enter image description here

1
  • @CarLaTeX I know how to do this without the 2nd hidden level and produce a tree. But the dotted line and the branch from it really makes me puzzled.
    – Ka Wa Yip
    Dec 16, 2016 at 5:24

1 Answer 1

5

You can use the node style coordinate for the empty nodes, Name them and use the coordinates to draw the dotted line.

As for the Labels: edge from parent node[left]{label}

\documentclass[border=10pt,varwidth]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\begin{scope}[every child node/.style={circle,draw}]
 \node [every child node] {A} [level distance=15mm,sibling distance=30mm]
child { node [coordinate] (i) {}
        child {node {B}}
        edge from parent node[left] {1}
}
child {node [coordinate] (j) {} [sibling distance=3.5em,
                                 every child/.style={->}]
       child {node {C} edge from parent node[left] {3}}
       child {node {D} edge from parent node[left] {4}}
       child {node {E} edge from parent node[left] {5}}
       child {node {F} edge from parent node[left] {6}}
       edge from parent node[right] {2}
};
\draw[dotted] (j) +(-15mm,0) -- +(15mm,0);
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

Also, by using the style every child node instead of giving the sytle Options draw, circle to every node, you can now, easily Change all nodes style whitin the tree, which is quite useful if you decide, for instance, that the nodes should be shaded...


EDIT (for the OP)

\node [every child node] {A} [level distance=15mm,sibling distance=30mm]
child[level distance=30mm] {node {B}
        edge from parent node[left] {1}
}
8
  • thanks. is there a way to make the kinked line 1 to a straight line?
    – Ka Wa Yip
    Dec 16, 2016 at 9:20
  • Yes, but number "1" won't be aligned with "2" then (you can align them, but manually). Just replace the node[coordinate] (i) {} and child{node{B}} for node {B} and give the option [level distance=30mm] to the child path command before the node {B} (the magical number 30 is just two times the "normal" level distance). But, to be honest, I like this layout better. Dec 16, 2016 at 9:29
  • but this way B does not end up on the same level of C,D,E,F?
    – Ka Wa Yip
    Dec 16, 2016 at 11:14
  • Not in the same logical level, no. But visually, it will be on the same level. Dec 16, 2016 at 11:15
  • but how to make them appear on the same level as in the drawn figure myself?
    – Ka Wa Yip
    Dec 16, 2016 at 11:28

You must log in to answer this question.

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