1

I have the following code:

\begin{tikzpicture}[grow=right, sloped]
\tikzstyle{level 1}=[level distance=1.3cm, sibling distance=1cm]
\tikzstyle{level 2}=[level distance=1.2cm, sibling distance=1cm]
\node[bag] {$S_i$}
    child[grow=left] {
        node[bag] {$S_{i+1}^{(1)}$}        
            child {
                node[end, label=left:
                    {$S$}] {}
                edge from parent[-latex]
                node[above] {$W$}
            }
            child {
                node[end, label=left:
                    {$S$}] {}
                edge from parent[-latex]
                node[above] {$B$}
            }
            child {
                node[end, label=left:
                    {$S$}] {}
                edge from parent[-latex]
                node[above] {$B$}
            }
            edge from parent[-latex]
            node[above] {$W$}
    }
    child[grow=right] {
        node[bag] {$A$}        
        child {
                node[bag] {$ \dots S_{i+1}^{(k_1)}$}
                edge from parent[-latex]
                node[above] {$w$}
        }
        child {
                node[bag] {$S_{i+1}^{(2)}$}
                child {
                     node[bag] {$S_{i+2}^{(2)(k_{\dots})}$}
                     edge from parent[-latex]
                     node[above] {u}
                }
                child {
                     node[bag] {$S_{i+2}^{(2)(2)}$}
                     edge from parent[-latex]
                     node[above] {u}
                }
                edge from parent[-latex]
                node[above] {$w$}
        }
        child {
                node[bag] {$S_{i+1}^{(1)}$}
                child {
                     node[bag] {$S_{i+2}^{(1)(k_{\dots})}$}
                     edge from parent[-latex]
                     node[above] {u}
                }
                child {
                     node[bag] {$S_{i+2}^{(1)(2)}$}
                     edge from parent[-latex]
                     node[above] {u}
                }
                child {
                     node[bag] {$S_{i+2}^{(1)(1)}$}
                     edge from parent[-latex]
                     node[above] {u}
                }
                edge from parent[-latex]
                node[above] {$w$}
        }
        edge from parent[-latex]    
            node[above] {$B$}
    };
\end{tikzpicture}

Having in preamble

\tikzstyle{bag} = [text width=1.5em, text centered]
\tikzstyle{end} = [circle, minimum width=3pt,fill, inner sep=0pt]

My problem is that the branch on the right generates collisions and the tree does not expand automatically.

Basically speaking nodes do overlap. How can i avoid this???

Thanks

  • 2
    Please complete your code to make it compilable; as it is now, no one knows how you defined the bag and end styles, for example. – Gonzalo Medina Apr 2 '12 at 3:10
  • Very sorry, forgot about it – Andry Apr 2 '12 at 3:58
  • 1
    @Andry This is still not compilable code: put everything into a \documentclass{article}\usepackage{tikz}...\begin{document}...\end{document} This way people can play with your code more easily. – Alan Munn Apr 2 '12 at 4:03
3

You just have to give appropriate values to sibling distance for the appropriate levels; in your example, the problematic levels are the second and the third:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[grow=right, sloped,bag/.style={draw},end/.style={draw}]
\tikzstyle{level 2}=[level distance=1.3cm, sibling distance=4cm]
\tikzstyle{level 3}=[level distance=2.2cm, sibling distance=1.5cm]
\node[bag] {$S_i$}
    child[grow=left,level 2/.style={sibling distance=1.5cm}] {
        node[bag] {$S_{i+1}^{(1)}$}        
            child {
                node[end, label=left:
                    {$S$}] {}
                edge from parent[-latex]
                node[above] {$W$}
            }
            child {
                node[end, label=left:
                    {$S$}] {}
                edge from parent[-latex]
                node[above] {$B$}
            }
            child {
                node[end, label=left:
                    {$S$}] {}
                edge from parent[-latex]
                node[above] {$B$}
            }
            edge from parent[-latex]
            node[above] {$W$}
    }
    child[grow=right] {
        node[bag] {$A$}        
        child {
                node[bag] {$ \dots S_{i+1}^{(k_1)}$}
                edge from parent[-latex]
                node[above] {$w$}
        }
        child {
                node[bag] {$S_{i+1}^{(2)}$}
                child {
                     node[bag] {$S_{i+2}^{(2)(k_{\dots})}$}
                     edge from parent[-latex]
                     node[above] {u}
                }
                child {
                     node[bag] {$S_{i+2}^{(2)(2)}$}
                     edge from parent[-latex]
                     node[above] {u}
                }
                edge from parent[-latex]
                node[above] {$w$}
        }
        child {
                node[bag] {$S_{i+1}^{(1)}$}
                child {
                     node[bag] {$S_{i+2}^{(1)(k_{\dots})}$}
                     edge from parent[-latex]
                     node[above] {u}
                }
                child {
                     node[bag] {$S_{i+2}^{(1)(2)}$}
                     edge from parent[-latex]
                     node[above] {u}
                }
                child {
                     node[bag] {$S_{i+2}^{(1)(1)}$}
                     edge from parent[-latex]
                     node[above] {u}
                }
                edge from parent[-latex]
                node[above] {$w$}
        }
        edge from parent[-latex]    
            node[above] {$B$}
    };
\end{tikzpicture}

\end{document}

enter image description here

Perhaps you could also be interested in the edge from parent path option to control where the arrows will connect the nodes:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[grow=right, sloped,bag/.style={draw},end/.style={draw},
edge from parent path={(\tikzparentnode.east) -- (\tikzchildnode.west)}
]
\tikzstyle{level 2}=[level distance=2cm, sibling distance=4cm]
\tikzstyle{level 3}=[level distance=2.2cm, sibling distance=1.5cm]
\node[bag] {$S_i$}
    child[grow=left,level 2/.style={sibling distance=1.5cm},edge from parent path={(\tikzparentnode.west) -- (\tikzchildnode.east)}] {
        node[bag] {$S_{i+1}^{(1)}$}        
            child {
                node[end, label=left:
                    {$S$}] {}
                edge from parent[-latex]
                node[above] {$W$}
            }
            child {
                node[end, label=left:
                    {$S$}] {}
                edge from parent[-latex]
                node[above] {$B$}
            }
            child {
                node[end, label=left:
                    {$S$}] {}
                edge from parent[-latex]
                node[above] {$B$}
            }
            edge from parent[-latex]
            node[above] {$W$}
    }
    child[grow=right] {
        node[bag] {$A$}        
        child {
                node[bag] {$ \dots S_{i+1}^{(k_1)}$}
                edge from parent[-latex]
                node[above] {$w$}
        }
        child {
                node[bag] {$S_{i+1}^{(2)}$}
                child {
                     node[bag] {$S_{i+2}^{(2)(k_{\dots})}$}
                     edge from parent[-latex]
                     node[above] {u}
                }
                child {
                     node[bag] {$S_{i+2}^{(2)(2)}$}
                     edge from parent[-latex]
                     node[above] {u}
                }
                edge from parent[-latex]
                node[above] {$w$}
        }
        child {
                node[bag] {$S_{i+1}^{(1)}$}
                child {
                     node[bag] {$S_{i+2}^{(1)(k_{\dots})}$}
                     edge from parent[-latex]
                     node[above] {u}
                }
                child {
                     node[bag] {$S_{i+2}^{(1)(2)}$}
                     edge from parent[-latex]
                     node[above] {u}
                }
                child {
                     node[bag] {$S_{i+2}^{(1)(1)}$}
                     edge from parent[-latex]
                     node[above] {u}
                }
                edge from parent[-latex]
                node[above] {$w$}
        }
        edge from parent[-latex]    
            node[above] {$B$}
    };
\end{tikzpicture}

\end{document}

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.