Tikz,无重叠的树

Tikz,无重叠的树

我正在尝试获得一棵没有重叠的漂亮树,有人能帮我改进我的代码吗?

\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

您可以更改sibling distance某些级别:

\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}

在此处输入图片描述

答案2

sibling distance可以使用forest可以为您完成此操作的包。

接下来是手册的摘要。

Forest 是一个基于 pgf/TikZ 的软件包,用于绘制语言树(和其他类型的树)。其主要特点是:(i) 可以生成非常紧凑的树的打包算法;(ii) 用户友好的界面,由熟悉的树括号编码以及用于选项设置的键值界面组成;(iii) 多种树格式选项,可控制各个节点的选项值及其操作机制;(iv) 可以使用 pgf/TikZ 的全部功能来装饰树;(v) 对代码更改敏感的外部化机制。

接下来是适应森林语法的代码,它与 中使用的语法类似qtree。正如您所见,没有声明levelsibling distance,包会计算它们以生成一个紧凑树。当然,您可以使用选项更改它们。

\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}

在此处输入图片描述

相关内容