0

I'm working on a paper with several operations on Binary Tree, and I make a lot of posts related to the tree in the last weeks. At the moment I'm stuck because I need to customize one of the tree template proposed in one of the previous answers, In particular this answer

I result that I want to achieve is the following tree effect.

enter image description here

This is the tree where I want to put the previous effect before

\documentclass[12pt, margin=3mm]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta,
                backgrounds,
                fit}

\begin{document}
    \begin{forest}
for tree = {
% nodes
    circle, draw, fill=gray!30,
    minimum size=1.5em, inner sep=0pt, outer sep=0pt,
/tikz/every label/.style = {font=\tiny},
/tikz/FIT/.style = {draw, very thick, rounded corners, fill=gray!30,
                    inner sep=4pt, inner ysep=8pt, yshift=-4pt,
                    fit=#1},
% tree
/tikz/every edge/.style={draw=gray, -Straight Barb, 
                         -Straight Barb, shorten >=3pt, shorten <=3pt,
                         bend angle = 15},% style for bended arrows
    edge=semithick,
    l sep=4mm,
    s sep=12mm,
    where level=3{l sep=11mm}{},% at level 3 increase l sep
    where level=4{no edge, very thick, fill=white}{},
            }% level 4 is the lowest
[11, label=below:{1:[0,7]},  name=t0
    [13, label=below:{2:[0,3]},  name=t11
        [17, label=below:{4:[0,1]},  name=t21
            [18, thick, label=below:{8:[0,0]}
                [18, label=below:0]
            ]
            [17, draw=none, fill=gray,thick, label=below:{9:[1,1]},  name=t31
                [17, label=below:1, name=n1]
            ]
        ]
        [13, draw=none,fill=gray, label=below:{5:[2,3]}
            [13, very thick, label=below:{10:[2,2]}
                [13, draw=none, fill=gray, label=below:2]
            ]
            [19, very thick, label=below:{11:[3,3]}
                [19, label=below:3, name=n3]
            ]
        ]
    ]
    [11, very thick,edge=thick, label=below:{3:[4,7]}
        [11, thick,edge=thick,
            [15, thick,edge=thick,
                [15, label=below:4]
            ]
            [11, very thick,edge=thick,
                [13, label=below:5]
            ]
        ]
        [20, very thick,edge=thick,
            [20, very thick,edge=thick,
                [13, label=below:6]
            ]
            [99, very thick,edge=thick,,
                [13, label=below:7]
            ]
        ]
     ]
]
\end{forest}
\end{document}

In other words, I need to make a separate tree (yellow) tree in the picture that gives me the possibility to attach some nodes to some nodes of the previous tree (blue).

I'm trying to simulate a persistent data structure, and this data structure reuse some node of the old tree to make a new tree (partially new tree)

My idea is to make one single tree and join the node as an arch but it looks very bad with my actual code and I have difficulties to customize to active my object.

4
  • (i) your MWE is not related to showed image (ii) in MWE is code from answer on one of yours previous asked question ... at least you should remove from it all what is not needed for new tree (for example all nodes labels and nodes at bottom of tree). – Zarko Jan 22 at 8:09
  • You have right, it is the same as before, but it is not true that the things inside the code are not necessary, In other words, I need to create the effect on the picture with the tree in the example. Maybe I need to refactor my question. – vincenzopalazzo Jan 22 at 10:14
  • Wow, my question was horrible, I'm sorry for that, there is some the physical limit that sometimes gives me some problem to express with a text my problem. Sorry I hope now is more clear – vincenzopalazzo Jan 22 at 10:25
  • 1
    I will upload some instruction how to make desired tree from provided MWE, ASAP – Zarko Jan 22 at 11:02
2
  • What is the problem? How to reproduce showed image?
  • Do you like that MWE (which is from answer on your previous question) would serve as starting point? if so, than you need to do the following:
    • remove all labels of nodes
    • remove definition for every label style
    • remove definition for FIT style
    • remove definition for every edge style
    • remove nodes in the lowest (4) levels
    • remove definition for 4 level
    • existing root node replace with [,phantom
  • After above taken measures obtained MWE should produce the following result:

enter image description here

  • The next step let be redesign of the left tree:
    • in the left branch replace numbers 13, 17 18 with 1', 2' and 3', which are in yellow nodes on your images.
    • add new fill color to this nodes
    • After this the first four lines of tree body are:
[,phantom
    [1', fill=yellow, name=t11
        [2', fill=yellow, name=t21
            [3',fill=yellow]
  • remove nodes with numbers 13 and 19 at bottom of tree. Now tree should looks as:
  • for finish redesign of left tree you need to make nodes with number 17 and 13 as phantom nodes:
[,phantom
    [1', fill=yellow, name=t11
        [2', fill=yellow, name=t21
            [3',fill=yellow]
            [,phantom]
        ]
        [,phantom]
    ]% end of left tree
  • Now tree should looks as:

enter image description here

  • for right tree you only need to do the following:
    • replace existing contents with new, showed on your image
    • give names to node with number 5 and number 3:
    ]% end of left tree
    [1
        [2
            [4]
            [5,name=t32]
        ]
        [3, name=t22
            [6]
            [7]
        ]
     ]% end of right tree
  • On the end you need first to draw lines between left and right tree:
]% end of tree
\draw[very thick, orange]   (t11) -- (t22)
                            (t21) -- (t32);
  • After all described actions you should obtain the following result:

enter image description here

  • If the result obtained is satisfactory, the you are done with the work. Of course, you can beautify the resulting tree a wee bit by following changes in the preamble \for tree:
    • changing s sep=12 to for example to s sep=3 (that horizontal (sibling) distances between nodes become smaller)
    • to right tree add option fit=band (for increasing distances between trees)
  • Considering aforementioned changes, the the complete MWE is:
\documentclass[12pt, margin=3mm]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta,
                backgrounds,
                fit}

\begin{document}
    \begin{forest}
for tree = {
% nodes
    circle, draw, fill=gray!30,
    minimum size=1.5em, inner sep=0pt, outer sep=0pt,
% tree
    edge=semithick,
    l sep=4mm,
    s sep=3mm,      % <------- changed
            }
[,phantom
    [1', fill=yellow, name=t11
        [2', fill=yellow, name=t21
            [3',fill=yellow]
            [,phantom]
        ]
        [,phantom]
    ]% end of left tree
    [1,fit=band     % <------- added
        [2, 
            [4]
            [5,name=t32]
        ]
        [3, name=t22, 
            [6]
            [7]
        ]
     ]% end of right tree
]% end of tree
% connection between left and right tree
\draw[very thick, orange]   (t11) -- (t22)
                            (t21) -- (t32); 
\end{forest}
\end{document}

enter image description here

1
  • Thanks for the fantastic answer, I will study it thanks again – vincenzopalazzo Jan 22 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.