3

I am trying to draw following merge sort algorithm picture. Please note that answer for How to draw a list that its items point to a structure? helpful for a starting point.

enter image description here


Following section is related to main question where how can I put a caption on top of each array node, simple example on the main part of the question would be helpful:

enter image description here

5

Here the tree option is useful. In order to have everything well aligned, you need to draw two trees, one growing down and one growing up. I used rectangle split option from shapes.multipart library but you can also do it with matrices of nodes.

Merge sort alg

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{shapes.multipart}

\tikzset{block/.style={
        font=\sffamily,
        draw=black,
        thin,
        fill=pink!50,
        rectangle split,
        rectangle split horizontal,
        rectangle split parts=#1,
        outer sep=0pt},
        %
        gblock/.style={
            block,
            rectangle split parts=#1,
            fill=green!30}
        }

\begin{document}
    
    \def\lvld{1.2}                  % Choose level distance
    \pgfmathsetmacro\shft{-6*\lvld} % Calculate the yshift for the green tree
    
    \begin{tikzpicture}[level distance=\lvld cm,
                        level 1/.style={sibling distance=4cm},
                        level 2/.style={sibling distance=2cm},
                        level 3/.style={sibling distance=1cm},
                        edgedown/.style={edge from parent/.style={draw=red,thick,-latex}},
                        edgeup/.style={edge from parent/.style={draw=green!50!black,thick,latex-}}
                        ]
  
        % GREEN TREE (drawn first to let the middle line filled in pink)

        \node[gblock=7,yshift=\shft cm] (A') {3 \nodepart{two} 9 \nodepart{three} 10 \nodepart{four} 27 \nodepart{five} 39 \nodepart{six}43 \nodepart{seven}82}
            [grow=up,edgeup]
            child {node[gblock=3] (B2') {9 \nodepart{two} 10 \nodepart{three} 82}
                child {node[gblock=1] (C4') {10}
                    child {node[gblock=1] (D7') {10}}
                }
                child {node[gblock=2] (C2') {9 \nodepart{two} 82}
                    child {node[gblock=1] (D3') {82}}
                    child {node[gblock=1] (D4') {9}}
                    }
                }
            child {node[gblock=4] (B1') {3 \nodepart{two} 27 \nodepart{three} 39 \nodepart{four} 43}
                child {node[gblock=2] (C3') {3 \nodepart{two} 43}
                    child {node[gblock=1] (D5') {3}}
                    child {node[gblock=1] (D6') {43}}
                }
                child {node[gblock=2] (C1') {27 \nodepart{two} 39}
                    child {node[gblock=1] (D1') {27}}
                    child {node[gblock=1] (D2') {39}}
                    }
            };
            
            
        % PINK TREE
        
        \node[block=7] (A) {39 \nodepart{two} 27 \nodepart{three} 43 \nodepart{four} 3 \nodepart{five} 9 \nodepart{six}82 \nodepart{seven}10}
            [grow=down,edgedown]
            child {node[block=4] (B1) {39 \nodepart{two} 27 \nodepart{three} 43 \nodepart{four} 3}
                child {node[block=2] (C1) {39 \nodepart{two} 27}
                    child {node[block=1] (D1) {39}}
                    child {node[block=1] (D2) {27}}
                    }
                child {node[block=2] (C2) {43 \nodepart{two} 3}
                    child {node[block=1] (D3) {43}}
                    child {node[block=1] (D4) {3}}
                    }
                }
            child {node[block=3] (B2) {9 \nodepart{two} 82 \nodepart{three} 10}
                child {node[block=2] (C3) {9 \nodepart{two} 82}
                    child {node[block=1] (D5) {9}}
                    child {node[block=1] (D6) {82}}
                }
                child {node[block=1] (C4) {10}
                    child {node[block=1] (D7) {10}}
                }
            };
\end{tikzpicture}
\end{document}

You can also see that every array is a specific node and so has its own name. You can now do about everything you want in TikZ around those nodes, by pointing to or from them.

5
  • Is it possible to label array linked by a line like on bottom picture on my question?
    – alper
    Apr 9 at 17:59
  • Yes, it is. As I said in the bottom of the post, one may add anything using the nodes names. Say, you want to label the bottom array (named (A')), you can by typing something like \draw (A') --++ (1,-1) node {foo};. Just give it a try.
    – SebGlav
    Apr 9 at 18:12
  • I did but when I put a new line after foo ex \draw (mat2-1-1.north) --++ (0,0.5) node[align=center] {\tiny foo\\\tiny doo}; the new line distance is pretty long , is it possible to make ti shorter ?/
    – alper
    Apr 9 at 20:26
  • 1
    Since what you write above is not in any relation with my answer (I did not use matrices), I suggest that you ask another question regarding your new issue.
    – SebGlav
    Apr 9 at 20:48
  • @SebGlab Please see: tex.stackexchange.com/questions/592325/… : you can also observe that middle lines in between the boxes are double bolded which I couldn't fix
    – alper
    Apr 10 at 21:38

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.