-2

I cannot figure out how to add the boxes with numbers in them.

enter image description here

I've tried using forest and tikz but was not able to do this. I was able to draw a tree structure easily, but that's it.

3 Answers3

3

Possible solution with forest:

\documentclass[margin=3mm]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta,
                shapes.multipart}
\newcommand\mpn[2]{\nodepart{one}  #1
                   \nodepart{two}  #2}
\newcommand\rsp{rectangle split parts}

\begin{document} \tikzset{every label/.style = {font=\footnotesize\sffamily\bfseries}} \begin{forest} for tree = { rectangle split, draw, %math content, % parent anchor=south, child anchor=north, edge = {semithick}, l sep=12mm, s sep=11mm, } [,coordinate [, coordinate [123,\rsp=1, label=below:L1] [,coordinate [123,\rsp=1,label=below:L2] [{\mpn{126}{346}},\rsp=2,label=below:L3] ] ] [, coordinate [{\mpn{126}{346}},\rsp=2,label=below:L4] [245,\rsp=1, label=below:L5] ] ] \end{forest} \end{document}

enter image description here

Zarko
  • 296,517
3

This time I decided to go for a more "minimalist" way and keep the tikz code to a minimum.

Output

output code for forest

Code

\documentclass[margin=10pt, tikz]{standalone}
\usepackage{forest}

\tikzset{ fornode/.style={% node definition draw, text width=1.2cm, line width=.5pt, inner xsep=0, align=center, execute at begin node=\setlength{\baselineskip}{10pt}}, mlabel/.style={% our label label={[font=\sffamily\bfseries, anchor=north]below:#1} } }

\forestset{% empty nodes/.style={% allows for straight edges when the node is empty for tree={calign=fixed edge angles}, delay={where content={}{shape=coordinate,for siblings={anchor=north}}{}} } }

\newcommand\msep{% So we don't have to write it every time \\vspace{-6pt}\rule{\textwidth}{.5pt}\ }

\begin{document} \begin{forest} for tree={% fornode, parent anchor=south, child anchor=north, s sep=1cm } [,draw=none [,empty nodes [134, mlabel=L1] [,empty nodes [123, mlabel=L2] [126\msep 346, mlabel=L3] ] ] [,empty nodes [234\msep 456, mlabel=L4] [245, mlabel=L5] ] ] \end{forest} \end{document}

Alenanno
  • 37,338
3

You can implement the terminal nodes as simple tables. Depending on how you want the nodes to look you can align the tables using the [b] parameter (as shown on the L2/L3 nodes) or not (as in the L4/L5 nodes). If you do use the [b] parameter, you must wrap the entire tabular environment in {...} to protect the [ and ] from forest's tree parsing algorithm. The forest package doesn't deal with unlabelled nodes in trees, so if you have such nodes as in your tree, there's always a bit of a compromise. The built-in style nice empty nodes gives an unappealing result, so I've used a fairly nice empty nodes style instead.

\documentclass{article}
\usepackage[linguistics]{forest}

\forestset{fairly nice empty nodes/.style={ delay={where content={}{shape=coordinate,for siblings={ {anchor=north}}}{}} } } \begin{document} \begin{forest}fairly nice empty nodes [ [[\begin{tabular}{|c|}\hline1 3 4\\hline\multicolumn{1}{c}{\textbf{L1}}\end{tabular} ] [ [{\begin{tabular}[b]{|c|}\hline1 2 3\\hline\multicolumn{1}{c}{\textbf{L2}}\end{tabular}} % if you use the [b] argument you must wrap the whole tabular in {} ] [{\begin{tabular}[b]{|c|}\hline1 2 6\\hline3 4 6\\hline\multicolumn{1}{c}{\textbf{L3}}\end{tabular}} ] ] ] [[\begin{tabular}{|c|}\hline1 2 6\\hline3 4 6\\hline\multicolumn{1}{c}{\textbf{L4}}\end{tabular} ] [\begin{tabular}{|c|}\hline2 4 5\\hline\multicolumn{1}{c}{\textbf{L5}}\end{tabular} ] ] ] \end{forest} \end{document}

output of code

Alan Munn
  • 218,180
  • +1, in the first thought about using of the table too, but then I remembered the question https: //tex.stackexchange.com/questions/184497/tikz-nodepart-children – Zarko Oct 06 '20 at 20:39