4

how do I embed a binary tree inside of a child node of an already existing binary tree, like such enter image description here

I've been using tikzpicture to draw the trees this way:

\documentclass[12pt, letterpaper, oneside]{article}
\usepackage{tikz}
\begin{document}
    \[
    \begin{tikzpicture}[nodes={draw, circle, fill=gray!20}, --]
        \node{19}
            child {node{25}
                child[missing]
                child{node{28}
                    child {node{32}}
                    child {node{93}}
                }
            }
            child [missing]
            child[missing]
            child {node{20}
                child{node{89}
                    child{node{99}}
                    child[missing]
                }
                child[missing]
            };
    \end{tikzpicture}
    \]
\end{document}

2 Answers 2

3

This shows how to put an entire tikzpicture into a node. Not sure how to put that node into a tree.

\documentclass[12pt, letterpaper, oneside]{article}
\usepackage{tikz}
\begin{document}
\begin{center}
\sbox0{\begin{tikzpicture}[nodes={draw, circle, fill=gray!20}]
        \node{19}
            child {node{25}
                child[missing]
                child{node{28}
                    child {node{32}}
                    child {node{93}}
                }
            }
            child [missing]
            child[missing]
            child {node{20}
                child{node{89}
                    child{node{99}}
                    child[missing]
                }
                child[missing]
            };
    \end{tikzpicture}}
    \begin{tikzpicture}
        \node[draw, rectangle, rounded corners] {\usebox0};
    \end{tikzpicture}
\end{center}
\end{document}
6
  • Can we apply this approach to draw forest inside tikzpicture?
    – alper
    Jul 23, 2022 at 23:36
  • It should. If you have a forest sitting around, try it. Jul 24, 2022 at 18:07
  • I was not able to find any example that has \begin{tikzpicture} \begin{forest} ... \end{forest} \end{tikzpicture}
    – alper
    Jul 24, 2022 at 18:46
  • The forest goes into a savebox, and \usebox goes into a tikzpicture node. They aren't nested. Jul 25, 2022 at 14:15
  • Can we draw an arrow from node inside \usebox into forest inside \savebox?
    – alper
    Jul 25, 2022 at 14:53
0

Through the help of a friend, I've managed to come up with a not so hard solution for this tree, I hope it helps someone:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}
    \flushleft
    \begin{tikzpicture}
        [square/.style={regular polygon, regular polygon sides=4}]
            \node at (0,0) [circle,draw] (a) {4};
            \node at (5,-3) [rounded corners, draw] (c) {
                \begin{minipage}{0.4\textwidth}
                    \begin{tikzpicture}
                        \node at (0,0) [circle,draw] (a1) {8};
                        \node at (1,-1) [circle,draw] (a2) {16};
                        \node at (-1,-1) [circle,draw] (a3) {50};
                        \node at (-2,-2) [circle,draw] (a4) {55};
                        \draw (a1) -- (a2);
                        \draw (a1) -> (a3);
                        \draw (a3) -> (a4);
                    \end{tikzpicture}
                \end{minipage}
                \begin{minipage}{0.4\textwidth}
                    \begin{tikzpicture}
                    \node at (0,0) [circle,draw] (a1) {19};
                    \node at (2,-1) [circle,draw] (a2) {20};
                    \node at (-2,-1) [circle,draw] (a3) {25};
                    \node at (-1,-2) [circle,draw] (a4) {28};
                    \node at (-1.5,-3) [circle,draw] (a5) {32};
                    \node at (-0.5,-3) [circle,draw] (a6) {93};
                    \node at (1,-2) [circle,draw] (a7) {89};
                    \node at (0.5,-3) [circle,draw] (a8) {99};
                    \draw (a1) -> (a2);
                    \draw (a1) -> (a3);
                    \draw (a3) -> (a4);
                    \draw (a4) -> (a5);
                    \draw (a4) -> (a6);
                    \draw (a2) -> (a7);
                    \draw (a7) -> (a8);
                    \end{tikzpicture}
                \end{minipage}
            };
            \node at (-3,-1) [circle,draw] (b) {9};
            \node at (-4,-2) [circle,draw] (b1) {17};
            \node at (-5,-3) [circle,draw] (b2) {19};
            \node at (-2,-2) [circle,draw] (b3) {26};
            \draw (a) -- (c);
            \draw (a) -- (b);
            \draw (b) -- (b1);
            \draw (b1) -- (b2);
            \draw (b) -- (b3);
    \end{tikzpicture}
\end{document}

This yields precisely

You must log in to answer this question.

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