8

I'm trying to get a nice tree without the overlaps, can anyone help me to improve my code?

\documentclass{standalone}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{dsfont}
\usepackage{tikz}
\usetikzlibrary{shapes}
\usetikzlibrary{graphs}

\begin{document}  
    \begin{tikzpicture}[baseline,level/.style={level distance=20mm, sibling distance =35mm/#1},scale=1]
        \node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
            child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                    child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                    child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                        child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                        child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                            child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                            child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                        }
                    }
                }   
                child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                    child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                        child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                        child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                            child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                            child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                                child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                                child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                            }
                        }
                    }
                    child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                        child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                        child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                            child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                            child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                                child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                                child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                            }
                        }
                    }
                }
            };
    \end{tikzpicture}
\end{document}
1

2 Answers 2

8

You can change the sibling distance for some of the levels:

\documentclass[border=5pt]{standalone}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{dsfont}
\usepackage{tikz}
\usetikzlibrary{shapes,trees}
\usetikzlibrary{graphs}

\begin{document}  
    \begin{tikzpicture}[
  baseline,
  level distance=20mm,
  text depth=.1em,
  text height=.8em,
  level 1/.style={sibling distance=10em},
  level 2/.style={sibling distance=40em},
  level 3/.style={sibling distance=20em},
  level 4/.style={sibling distance=10em}]
        \node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
            child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                    child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                    child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                        child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                        child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                            child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                            child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                        }
                    }
                }   
                child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                    child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                        child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                        child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                            child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                            child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                                child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                                child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                            }
                        }
                    }
                    child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                        child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                        child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                            child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                            child{ node [ellipse split, draw] {$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                                child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                                child{ node [rectangle, draw] {$(s_1,s_2,s_3,s_4)$}}
                            }
                        }
                    }
                }
            };
    \end{tikzpicture}
\end{document}

enter image description here

0
11

Instead of manually adjusting the sibling distance you can use forest package which does it for you.

Next it's the abstract from the manual.

Forest is a pgf/TikZ-based package for drawing linguistic (and other kinds of) trees. Its main features are (i) a packing algorithm which can produce very compact trees; (ii) a user-friendly interface consisting of the familiar bracket encoding of trees plus the key–value interface to option- setting; (iii) many tree-formatting options, with control over option values of individual nodes and mechanisms for their manipulation; (iv) the possibility to decorate the tree using the full power of pgf/TikZ; (v) an externalization mechanism sensitive to code-changes.

And next is your code adapted to forest syntax which is similar to the one used with qtree. As you can see no level or sibling distance is declared, the package computes them to produce a compact tree. Of course, you can change them with options.

\documentclass[landscape]{article}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{dsfont}
\usepackage{forest}
\usetikzlibrary{shapes}

\begin{document}  
\centering
\begin{forest}
    rounded/.style={ellipse split, draw},
    squared/.style={rectangle,draw}
    [{$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}, for tree=rounded
        [{$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
            [{$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                [{$(s_1,s_2,s_3,s_4)$},squared]
                [{$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                    [{$(s_1,s_2,s_3,s_4)$},squared]
                    [{$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                        [{$(s_1,s_2,s_3,s_4)$},squared]
                        [{$(s_1,s_2,s_3,s_4)$},squared]
                    ]
                ]
            ]   
            [{$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                [{$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                    [{$(s_1,s_2,s_3,s_4)$},squared]
                    [{$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                        [{$(s_1,s_2,s_3,s_4)$},squared]
                        [{$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                            [{$(s_1,s_2,s_3,s_4)$},squared]
                            [{$(s_1,s_2,s_3,s_4)$},squared]
                        ]
                    ]
                ]
                [{$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                    [{$(s_1,s_2,s_3,s_4)$},squared]
                    [{$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                        [{$(s_1,s_2,s_3,s_4)$},squared]
                        [{$s_1<s_2$\nodepart{lower}\tiny $(s_1,s_2,s_3,s_4)$}
                            [{$(s_1,s_2,s_3,s_4)$},squared]
                            [{$(s_1,s_2,s_3,s_4)$},squared]
                        ]
                    ]
                ]
            ]
       ]
    ]
\end{forest}
\end{document}

enter image description here

You must log in to answer this question.

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