2

I need to draw such diagram

I started but lost after few steps, could anyone help me? I was trying to learn from https://latexdraw.com/draw-trees-in-tikz/, but was somehow not able to generate after 2 steps.

\documentclass[border=0.2cm]{standalone}
 
\usepackage{tikz}
 
\begin{document}
\begin{tikzpicture}
    [
        level 1/.style = {teal, sibling distance = 4cm},
        level 2/.style = {teal, sibling distance = 4cm},
        level 3/.style = {teal, level distance = 1cm},
        edge from parent fork down
    ]
    \node {\textcolor{teal}{1}}
        child {node [draw]{2}
        edge from parent [left]} 
        child {node [draw] {3}
        child {node [draw] {4}
        child {node [draw] {5}}}
        child {node [draw] {6}}
        edge from parent node [right] {}};
    \end{tikzpicture}
\end{document}
2
  • 2
    Can you please complete your code, so it will work when we copy, paste and compile it? (\documentclass, \usepackage etc.) Thanks
    – MS-SPO
    Sep 2, 2021 at 23:54
  • Suggestion: take a look at the package forest.
    – Fran
    Sep 3, 2021 at 17:07

1 Answer 1

3

Like this?

enter image description here

With forest package is easy:

\documentclass[border=0.2cm]{standalone}
\usepackage[edges]{forest}

\begin{document}
\begin{forest}
for tree={          
% style of nodes in the tree
draw, semithick,
       minimum height = 3ex,
        minimum width = 2em,
% style of tree (edges, distances, direction)
     grow = south,
forked edge,        % for forked edge
    s sep = 3mm,    % "sibling" distance
    l sep = 6mm,    % "level" distance
 fork sep = 3mm,    % distance from parent to branching point
           }
[1
    [2]
    [3,fit=band
        [1
            [5
                [9]
                [10]
            ]
            [6
                [11]
                [12]
            ]
        ]
        [5
            [7
                [13]
                [14]
            ]
            [8
                [15]
                [16]
            ]
        ]
    ]
]
    \end{forest}
\end{document}

Edit: If you like to have more colorful diagram, you only need to add desired colors to node elements (like draw=<desired color>, fill=` etc. You can also change appearance of conected lines (edges). See MWE below:

enter image description here

\documentclass[border=0.2cm]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta}    % <---

\begin{document}
    \begin{forest}
for tree={
% style of nodes in the tree
draw=blue,      % <---
thick,
fill=blue!10,   % <---
text=red,       % <---
       minimum height = 3ex,
        minimum width = 2em,
% style of tree (edges, distances, direction)
edge={semithick, -{Straight Barb[scale=0.8]},draw=teal},    % <---
forked edge,        % for forked edge
    l sep = 9mm,    % "level" distance
    s sep = 3mm,    % "sibling" distance
    where level = 4{s sep=1mm}{},
 fork sep = 3mm,    % distance from parent to branching point
           }
[1
    [2]
    [3,fit=band
        [1
            [5
                [9]
                [10]
            ]
            [6
                [11]
                [12]
            ]
        ]
        [5
            [7
                [13]
                [14]
            ]
            [8
                [15]
                [16]
            ]
        ]
    ]
]
    \end{forest}
\end{document}
7
  • Thanks Zarko Thank you very much.
    – Myshkin
    Sep 3, 2021 at 7:55
  • Is there any way the boarder could be made colored?
    – Myshkin
    Sep 3, 2021 at 8:19
  • @Marso, of course. In % style of nodes in the tree change draw to `draw=<desired color>. You can use all TikZ options and libraries. For example I will change nodes border to blue ASAP.
    – Zarko
    Sep 3, 2021 at 8:25
  • Thanks Zarko.....
    – Myshkin
    Sep 3, 2021 at 11:06
  • @Marso, sorry, what you like to tell me? Can be less cryptic?
    – Zarko
    Sep 3, 2021 at 13:54

You must log in to answer this question.

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