1

I want to draw a code tree like that.enter image description here

I'm relatively new to Latex, currently I'm here ans stuck. Can someone help me to proceed further. Regards

\documentclass{standalone}
\usepackage{forest}
\usetikzlibrary{backgrounds,fit}
\begin{document}
\begin{forest} 
for tree={draw, circle, l sep+=0.1em, inner sep=0.01em}
[,fill
    [{$.$}, edge label={node[midway,left]{$11$}}
        [$.$, edge label={node[midway,left]{$01$}}
            [$.$, edge label={node[midway,left]{$10$}}
                [$.$, edge label={node[midway,left]{$10$}}]
                [$.$, edge label={node[midway,right]{$0.18$}}]
            ]
            [$.$, edge label={node[midway,right]{$01$}}
                [$.$, edge label={node[midway,left]{$0$}}]
                [$.$, edge label={node[midway,right]{$09$}}]
            ]
        ]
        [$.$, edge label={node[midway,right]{$50$}}
            [$.$, edge label={node[midway,left]{$00$}}
                [$.$, edge label={node[midway,left]{$00$}}]
                [$.$, edge label={node[midway,right]{$00$}}]
            ]
            [$.$, edge label={node[midway,right]{$00$}}
                [$.$, edge label={node[midway,left]{$0$}}]
                [$.$, edge label={node[midway,right]{$5$}}]
            ]
        ]
    ]
    [{$.$}, edge label={node[midway,right]{$00$}}
        [$.$, edge label={node[midway,left]{$00$}}
            [$.$, edge label={node[midway,left]{$6$}}
                [$.$, edge label={node[midway,left]{$7$}}]
                [$.$, edge label={node[midway,right]{$8$}}]
            ]
            [$.$, edge label={node[midway,right]{$9$}}
                [$.$, edge label={node[midway,left]{$05$}}]
                [$.$, edge label={node[midway,right]{$07$}}]
            ]
        ]
        [$.$, edge label={node[midway,right]{$00$}}
            [$.$, edge label={node[midway,left]{$00$}}
                [$.$, edge label={node[midway,left]{$5$}}]
                [$.$, edge label={node[midway,right]{$7$}}]
            ]
            [$.$, edge label={node[midway,right]{$10$}}
                [$.$, edge label={node[midway,left]{$01$}}]
                [$.$, edge label={node[midway,right]{$00$}}]
            ]
        ]
    ]
]
\end{forest}
\end{document}
3
  • What do you want help with exactly?
    – cfr
    Aug 23, 2017 at 1:11
  • What's the rule for the numbers? I get that 1 is left and 0 right, but why always exactly two numbers, however much the tree grows?
    – cfr
    Aug 23, 2017 at 1:13
  • Why do you have such different numbers in your code from those in the image? I can't make sense of how that's meant to map onto the image you say you're trying to draw.
    – cfr
    Aug 23, 2017 at 1:25

1 Answer 1

1

I don't understand the number pattern in the target picture and I don't understand how the numbers in the code correspond to that target. For these reasons, I left the content of the labels as is and only altered the appearance of the tree.

The tree is gray. The highlighted path through the tree is darkgray in a thick line.

\documentclass[border=10pt]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{forest}
  for tree={
    draw=gray,
    circle,
    fill=gray,
    s sep'+=5pt,
    inner sep=.75pt,
  },
  label me/.style={
    edge label={node [midway,inner sep=1pt,above,sloped,font=\footnotesize] {$#1$}},
  },
  tikz+={
    \draw [-Latex, gray] (.north west) ++(-15pt,15pt) -- ++(-25pt,0) node [midway, above, anchor=west, font=\footnotesize, inner sep=1pt, rotate=90] {1};
    \draw [-Latex, gray] (.north east) ++(15pt,15pt) -- ++(25pt,0) node [midway, above, anchor=east, font=\footnotesize, inner sep=1pt, rotate=-90] {0};
  },
  [
    [, label me=11
        [, label me=01
            [, label me=10
                [, label me=10]
                [, label me=0.18]
            ]
            [, label me=01
                [, label me=0]
                [, label me=09]
            ]
        ]
        [, label me=50
            [, label me=00
            [, label me=00, before typesetting nodes={for current and ancestors={fill=darkgray, draw=darkgray, edge+={darkgray, thick} } }]
                [, label me=00]
            ]
            [, label me=00
                [, label me=0]
                [, label me=5]
            ]
        ]
    ]
    [, label me=00
        [, label me=00
            [, label me=6
                [, label me=7]
                [, label me=8]
            ]
            [, label me=9
                [, label me=05]
                [, label me=07]
            ]
        ]
        [, label me=00
            [, label me=00
                [, label me=5]
                [, label me=7]
            ]
            [, label me=10
                [, label me=01]
                [, label me=00]
            ]
        ]
    ]
  ]
\end{forest}
\end{document}

reformatted tree

You must log in to answer this question.

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